Interfacing PIC12F1822 with 1602 LCD and LM35 sensor – CCS C

This post shows a simple interfacing of PIC12F1822 microcontroller with 16×2 LCD and LM35 analog temperature sensor.

The LM35 temperature sensor is a 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 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

The ADC module converts analog data into digital data. The PIC12F1822 MCU has a 10-bit ADC module and a built-in fixed voltage reference (FVR) which makes it a good choice for this application. With the fixed voltage reference we get approximately an exact result. Normally negative and positive references of the ADC module are VSS and VDD respectively, but VDD is not exactly equal to 5.00V and here we should use the fixed voltage reference as a positive reference of the ADC module.

The PIC12F1822 has 3 fixed voltage references: 1.024V, 2.048V and 4.096V. For example if we set the fixed voltage reference to 4.096V and the ADC module is configured so that the negative and the positive references are VSS and FVR (Fixed Voltage Reference) respectively, in this case the equivalent 10-bit digital value of 4.096 is 1023 and 3.00V is 3.00 * 1023/4.096 = 749 , and so on.
In this project I used FVR = 1.024V because the LM35 output is generally less than 1V and also it gave me better result (let’s say higher resolution). Now the ADC module works in the interval between 0 and 1.024V.

The temperature value is displayed on 1602 LCD display. This LCD is interfaced with the microcontroller using 74HC595 (74HC164 …..) shift register as what was done in this post:
Interfacing PIC12F1822 microcontroller with LCD display

Hardware Required:

  • PIC12F1822 microcontroller
  • LM35 temperature sensor  — datasheet
  • 1602 LCD screen
  • 74HC595 shift register
  • 10K ohm variable resistor
  • Breadboard
  • 5V voltage source
  • Jumper wires

Interfacing PIC12F1822 with LM35 sensor circuit:

PIC12F1822 LM35 temperature sensor circuit

The output of the LM35 temperature sensor is connected to analog channel 0 (RA0) of the PIC12F1822 microcontroller.
The 1602 LCD display pins are connected to 74HC595 shift register except the Enable pin (E) which is connected directly to the PIC12F1822 MCU. With the help of the shift register 74HC595, the LCD uses only 3 data lines: clock, data and enable. Other types of serial-in parallel-out shift registers can be used such as 74HC164 and CD4094 (74HC4094).
In this example the PIC12F1822 MCU uses its internal oscillator and MCLR pin function is disabled.

Interfacing PIC12F1822 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 1.024V is represented by 1023 (ADC positive reference comes from FVR which is set to 1.024V) . Converting back the ADC digital value is easy, we can use the following equation for that conversion:
Voltage (in Volts) = ADC reading * 1.024 / 1024
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.1
where 0.1 = 100 * 1.024 / 1024
The complete C code is the one below.

The Result:

PIC12F1822 LM35 temperature sensor interface circuit

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.