In this tutorial, we are making a Digital Thermometer using PIC microcontroller and LM35 Temperature Sensor. In this project, we will sense the temperature using LM35 and display it on 16×2 LCD. LM35 Temperature Sensor is accurate and cheaper and doesn’t require any external calibration. The output voltage is proportional to Celsius temperature scale and changes by 10mV per °C.
Material Required
- PicKit 3
- LM35 Temperature Sensor
- 16*2 LCD
- PIC16F877A IC
- 40 – Pin IC holder
- Perf board
- 20 MHz Crystal OSC
- Female and Male Bergstick pins
- 33pf Capacitor – 2Nos, 100uf and 10uf cap.
- 680 ohm, 220 ohm, 10K and 560ohm Resistor
- Potentiometer 10k
- LED of any color
- 1 Soldering kit
- IC 7805
- 12V Adapter
- Connecting wires
- Breadboard
LM35 Temperature Sensor:
LM35 temperature sensor has zero offset voltage, which means at 0°C the output will be 0V. The maximum voltage it can handle is 1.5V which means it can be able to sense a maximum temperature of 150°C (1.5V / 10mV).
Pin No | Function | Name |
1 | Supply voltage; 5V (+35V to -2V) | Vcc |
2 | Output voltage (+6V to -1V) | Output |
3 | Ground (0V) | Ground |
We have already used LM35 with many other microcontrollers to measure the temperature:
- Digital Thermometer using LM35 and 8051 Microcontroller
- Temperature Measurement using LM35 and AVR Microcontroller
- Digital Thermometer using Arduino and LM35 Temperature Sensor
- Room Temperature Measurement with Raspberry Pi
As we already told that LM35 gives analog output, so first we need to read that analog values using PIC Microcontroller and then we will convert them into digital values using ADC (Analog to Digital Conversion). So we will learn ADC in PIC Microcontroller before going any further.
ADC in PIC Microcontroller PIC16F877A:
There are many types of ADC available and each one has its own speed and resolution. The most common types of ADCs are flash, successive approximation, and sigma-delta. The type of ADC used in PIC16F877A is called as the Successive approximation ADC or SAR in short. So let’s learn a bit about SAR ADC before we start using it.
Successive Approximation ADC: The SAR ADC works with the help of a comparator and some logic conversations. This type of ADC uses a reference voltage (which is variable) and compares the input voltage with the reference voltage using a comparator and difference, which will be a digital output, is saved from the Most significant bit (MSB). The speed of the comparison depends on the Clock frequency (Fosc) on which the PIC is operating.
Now that we know some basics on ADC, lets open our datasheet and learn how to use the ADC on our PIC16F877A MCU. The PIC we are using has 10-bit 8-channel ADC. This means the output value of our ADC will be 0-1024 (2^10) and there are 8 pins (channels) on our MCU which can read analog voltage. The value 1024 is obtained by 2^10 since our ADC is 10 bit. The eight pins which can read the analog voltage are mentioned in the datasheet. Lets look at the picture below.
The analog channels AN0 to AN7 are highlighted for you. Only these pins will be able to read analog voltage. So before reading an input voltage we have to specify in our code which channel has to be used to read the input voltage. In this tutorial we will use channel 4 with a potentiometer to read the analog voltage at this channel.
The A/D module has four registers which has to be configured to read data from the Input pins. These registers are:
• A/D Result High Register (ADRESH)
• A/D Result Low Register (ADRESL)
• A/D Control Register 0 (ADCON0)
• A/D Control Register 1 (ADCON1)
Code and Explanation
The complete code for this Digital Thermometer using LM35 and PIC microcontroller is given at the end. The code is self-explained with comment lines and just involves the concept of interfacing a LCD with PIC Microcontroller and Using ADC module in PIC Microcontroller which we have already covered in our previous tutorials of learning PIC Microcontrollers.
Here we are just showing the calculations done for reading the analog output voltage from LM35 and then converting it into temperature values. So here we are converting the ADC value from LM35 into the voltage and then voltage value into temperature. Therefore, after getting the value we have seperated every character for displaying on LCD.
adc = (ADC_Read(4)); // Reading ADC values volt = adc*4.88281; // Convert it into the voltage temp=volt/10.0; // Getting the temperature values temp1 = temp*100; c1 = (temp1/1000)%10; c2 = (temp1/100)%10; c3 = (temp1/10)%10; c4 = (temp1/1)%10;
Now in the below code, set the LCD cursor and then print the output value
Lcd_Clear(); Lcd_Set_Cursor(1,3); Lcd_Print_String("Temperature"); Lcd_Set_Cursor(2,5); Lcd_Print_Char(c1+'0'); Lcd_Print_Char(c2+'0'); Lcd_Print_String("."); Lcd_Print_Char(c3+'0'); Lcd_Print_Char(c4+'0'); Lcd_Print_Char(0xDF); Lcd_Print_String("C"); __delay_ms(3000);
Working of Digital Thermometer:
After uploading the code in the PIC micro-controller, power up the circuit using 12v adapter. The analog output of LM35 temperature sensor is fed to the analog input channel of the PIC controller. As the temperature increase the ADC value will also increase. That ADC value is further converted into voltage by multiplying it with 4.88281. Then the voltage value is converted to corresponding character for displaying it into 16*2 LCD.
Read more: Temperature sensor using PIC16F877A microcontroller