The LM35 temperature sensor is three pin device (VCC, OUT and GND) with an output voltage linearly related to Centigrade temperature. Since the LM35 output varies with dependent to the temperature we need ADC (Analog-to-Digital Converter) module to measure this voltage. The ADC module converts analog data into digital data.
The LM35 output has linear +10mV/°C scale factor means the following:
If the output voltage = 10mV —> temperature = 1°C
If the output voltage = 100mV —> temperature = 10°C
If the output voltage = 200mV —> temperature = 20°C
If the output voltage = 370mV —> temperature = 37°C
and so on.
LM35 Futures (from datasheet):
- Calibrated Directly in ° Celsius (Centigrade)
- Linear + 10 mV/°C Scale Factor
- 0.5°C Ensured Accuracy (at +25°C)
- Rated for Full −55°C to +150°C Range
- Suitable for Remote Applications
- Low Cost Due to Wafer-Level Trimming
- Operates from 4 to 30 V
- Less than 60-μA Current Drain
- Low Self-Heating, 0.08°C in Still Air
- Nonlinearity Only ±¼°C Typical
- Low Impedance Output, 0.1 Ω for 1 mA Load
This topic shows how to interface the microcontroller PIC16F877A with LM35 analog temperature sensor.
Hardware Required:
- PIC16F877A microcontroller
- LM35 temperature sensor — datasheet
- 1602 LCD screen
- 8MHz crystal
- 2 x 22pF ceramic capacitor
- 10K ohm variable resistor
- Breadboard
- 5V voltage source
- Jumper wires
Interfacing PIC16F877A with LM35 sensor circuit:
The output of the LM35 temperature sensor is connected to analog channel 0 (AN0) of the PIC16F877A.
In this example the microcontroller runs with crystal oscillator @ 8MHz.
Interfacing PIC16F877A with LM35 temperature sensor C code:
The C code below was tested with CCS PIC C compiler version 5.051.
Reading voltage quantity using the ADC gives us a number between 0 and 1023 (10-bit resolution), 0V is represented by 0 and 5V is represented by 1023. Converting back the ADC digital value is easy and we can use the following equation for that conversion:
Voltage (in Volts) = ADC reading * 5 / 1023
Multiplying the previous result by 100 (LM35 scale factor is 10mV/°C = 0.01V/°C) will gives the actual temperature:
Temperature(°C) = ADC reading * 0.489
where 0.489 = 500 / 1023
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | /* * Interfacing PIC16F877A microcontroller with 1602 LCD and * LM335 analog temperature sensor. * C Code for CCS C compiler. * This is a free software with NO WARRANTY. * http://simple-circuit.com/ */ // LCD module connections #define LCD_RS_PIN PIN_D0 #define LCD_RW_PIN PIN_D1 #define LCD_ENABLE_PIN PIN_D2 #define LCD_DATA4 PIN_D3 #define LCD_DATA5 PIN_D4 #define LCD_DATA6 PIN_D5 #define LCD_DATA7 PIN_D6 // End LCD module connections #include <16F877A.h> #fuses HS,NOWDT,NOPROTECT,NOLVP #device ADC=10 #use delay(clock = 8MHz) #include <lcd.c> char message1[] = "Temp = 00.0 C"; char message2[] = "= 00.0 K"; signed int16 Kelvin, Celsius; void main(){ setup_adc(ADC_CLOCK_INTERNAL); // ADC Module uses its internal oscillator setup_adc_ports(AN0); // Configure AN0 pin as analog set_adc_channel(0); // Select channel 0 (AN0) lcd_init(); // Initialize LCD module lcd_putc('\f'); // Clear LCD while(TRUE){ delay_ms(1000); Kelvin = read_adc() * 0.489; // Read analog voltage and convert it to Kelvin (0.489 = 500/1023) Celsius = Kelvin - 273; // Convert Kelvin to degree Celsius if(Celsius < 0){ Celsius = abs(Celsius); // Absolute value message1[7] = '-'; // Put minus '-' sign } else message1[7] = ' '; // Put space ' ' if (Celsius > 99) message1[7] = 1 + 48; // Put 1 (of hundred) message1[8] = (Celsius / 10) % 10 + 48; message1[9] = Celsius % 10 + 48; message1[12] = 223; // Degree symbol message2[2] = (Kelvin / 100) % 10 + 48; message2[3] = (Kelvin / 10) % 10 + 48; message2[4] = Kelvin % 10 + 48; lcd_gotoxy(1, 1); // Go to column 1 row 1 printf(lcd_putc, message1); // Display message1 lcd_gotoxy(6, 2); // Go to column 6 row 2 printf(lcd_putc, message2); // Display message2 } } // end of code. |
Interfacing PIC16F877A with LM35 videos:
The video below shows a simple hardware circuit of our example.
And the following video shows Proteus simulation.
PIC16F877A + LM35 Proteus simulation file download
Discover more from Simple Circuit
Subscribe to get the latest posts sent to your email.