Friday, September 8, 2017

Interfacing PIC12F1822 with 1602 LCD and LM35 temperature sensor


This post shows a simple interfacing of PIC16F1822 microcontroller with 16x2 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:
Interfacing PIC12F1822 microcontroller with LM35 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.
/* Interfacing PIC12F1822 with LM35 temperature sensor C code.
   The temperature result is displayed on 1602 LCD screen with
   the help of a shift register (74HC595, 74HC164 ....).
   Serial LCD driver for CCS C must be added to the project.
   http://ccspicc.blogspot.com/
   electronnote@gmail.com
*/

// Serial LCD module connections
#define LCD_DATA_PIN  PIN_A5
#define LCD_CLOCK_PIN PIN_A4
#define LCD_EN_PIN    PIN_A2
// End serial LCD module connections

#include <12F1822.h>
#device ADC = 10
#fuses NOMCLR INTRC_IO PLL_SW
#use delay(clock=32000000)
#use fast_io(A)
#include <3WireLCD.c>                            // 3-wire serial LCD driver source file

float temp;
void main() {
  setup_oscillator(OSC_8MHZ | OSC_PLL_ON);       // Set internal oscillator to 8MHz with PLL enabled (32MHz)
  lcd_initialize();                              // Initialize LCD module
  lcd_cmd(LCD_CLEAR);                            // LCD Clear
  setup_vref(VREF_ADC_1v024);                    // Configure FVR to supply ADC positive reference with 1.024V
  setup_adc(ADC_CLOCK_INTERNAL);                 // ADC Module uses its internal oscillator
  setup_adc_ports(sAN0 | VSS_FVR);               // Configure AN0 pin as analog - Voltage reference: VSS - FVR(1.024V)
  set_adc_channel(0);                            // Select channel 0 (AN0)
  lcd_goto(3, 1);                                // Go to column 3 row 1
  printf(lcd_out, "Temperature:");
 while(TRUE){
    delay_ms(1000);
    temp = read_adc() * 0.1;                     // Read analog voltage and convert it to Kelvin (0.1 = 100*1.024/1024)
    lcd_goto(6, 2);                              // Go to column 6 row 2
    printf(lcd_out, "%4.1fßC", temp);            // Display LM35 temperature result (float format)
  }
}
The Result:
PIC12F1822 with LM35 temperature sensor hardware circuit