Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

How to Read Sensor Data Using Microchip Microcontrollers

Sensor reading is a critical task in embedded systems, enabling devices to interact with their environment by collecting data such as temperature, humidity, light intensity, and more. In the context of microchip microcontrollers, understanding how to effectively read sensor data is essential for creating responsive and intelligent applications. This article will guide you through the process of reading sensor data using Microchip's PIC microcontrollers, with practical examples and sample code to illustrate the concepts.

Examples:

  1. Reading Temperature Data from an LM35 Sensor Using PIC16F877A

    The LM35 is a popular temperature sensor that provides an analog output proportional to the temperature. Below is an example of how to read data from an LM35 sensor using a PIC16F877A microcontroller.

    Hardware Setup:

    • Connect the Vout pin of the LM35 to the AN0 pin of the PIC16F877A.
    • Connect the Vcc and GND pins of the LM35 to the 5V and GND of the microcontroller, respectively.

    Code Example:

    #include <xc.h>
    
    // Configuration bits
    #pragma config FOSC = HS
    #pragma config WDTE = OFF
    #pragma config PWRTE = OFF
    #pragma config BOREN = ON
    #pragma config LVP = OFF
    #pragma config CPD = OFF
    #pragma config WRT = OFF
    #pragma config CP = OFF
    
    void ADC_Init() {
       ADCON0 = 0x41; // Turn on the ADC module and select channel 0 (AN0)
       ADCON1 = 0x80; // Configure the result to be right-justified
    }
    
    unsigned int ADC_Read(unsigned char channel) {
       ADCON0 &= 0xC5; // Clear the channel selection bits
       ADCON0 |= (channel << 3); // Select the required channel
       __delay_ms(2); // Acquisition time to charge the hold capacitor
       GO_nDONE = 1; // Start the conversion
       while(GO_nDONE); // Wait for the conversion to complete
       return ((ADRESH << 8) + ADRESL); // Return the result
    }
    
    void main() {
       unsigned int adc_result;
       float temperature;
    
       ADC_Init(); // Initialize the ADC module
    
       while(1) {
           adc_result = ADC_Read(0); // Read the ADC value from channel 0 (AN0)
           temperature = (adc_result * 5.0 / 1024.0) * 100; // Convert the ADC value to temperature
           // Now you can use the temperature value in your application
           __delay_ms(1000); // Wait for a second before the next reading
       }
    }
  2. Reading Light Intensity Using an LDR and PIC16F877A

    An LDR (Light Dependent Resistor) can be used to measure light intensity. The resistance of the LDR changes with the light intensity, which can be read using the ADC of the microcontroller.

    Hardware Setup:

    • Connect one end of the LDR to the 5V supply.
    • Connect the other end of the LDR to the AN1 pin of the PIC16F877A and a 10kΩ resistor to GND, forming a voltage divider.

    Code Example:

    #include <xc.h>
    
    // Configuration bits
    #pragma config FOSC = HS
    #pragma config WDTE = OFF
    #pragma config PWRTE = OFF
    #pragma config BOREN = ON
    #pragma config LVP = OFF
    #pragma config CPD = OFF
    #pragma config WRT = OFF
    #pragma config CP = OFF
    
    void ADC_Init() {
       ADCON0 = 0x41; // Turn on the ADC module and select channel 0 (AN0)
       ADCON1 = 0x80; // Configure the result to be right-justified
    }
    
    unsigned int ADC_Read(unsigned char channel) {
       ADCON0 &= 0xC5; // Clear the channel selection bits
       ADCON0 |= (channel << 3); // Select the required channel
       __delay_ms(2); // Acquisition time to charge the hold capacitor
       GO_nDONE = 1; // Start the conversion
       while(GO_nDONE); // Wait for the conversion to complete
       return ((ADRESH << 8) + ADRESL); // Return the result
    }
    
    void main() {
       unsigned int adc_result;
       float light_intensity;
    
       ADC_Init(); // Initialize the ADC module
    
       while(1) {
           adc_result = ADC_Read(1); // Read the ADC value from channel 1 (AN1)
           light_intensity = adc_result * (5.0 / 1024.0); // Convert the ADC value to voltage
           // Now you can use the light intensity value in your application
           __delay_ms(1000); // Wait for a second before the next reading
       }
    }

To share Download PDF

Gostou do artigo? Deixe sua avaliação!
Sua opinião é muito importante para nós. Clique em um dos botões abaixo para nos dizer o que achou deste conteúdo.