LM35 is an analog temperature sensor that can convert environment temperature between -55 to 150 degree Celsius. It need a stable DC supply voltage between 4 and 30V. It generates a linear analog output voltage that's 10mV per degree Celsius. We can interpret this analog signal with an ICL7107 analog to BCD seven-segment display converter, or even a micro-controller's analog to digital converter (ADC).
On-board LM35 16x2 LCD and PIC16F887 micro-controller |
Using a micro-controller with ADC module is common we can select any output display type, a seven segment display or character or graphical LCD. In this example I use a PIC16F887 micro-controller and a 16x4 character LCD from Tinsharp.
I use the internal ADC module of PIC16F887. It has 10-bit resolution. Its voltage reference pins are wired internally by software to GND and VDD (+5VDC). LM35 connects to RA5(AN4) pin of PIC16F887.
The LCD operates in 4-bit data transfer mode. I connect the on-board LCD as follow,
- RS -> RB5
- RW-> RB6
- EN -> RB7
- DB4...DB7 -> RD0...RD3
PORTB has an alternative analog inputs function. So we have to clear ANSELH SFR first.
/* * File: main.c * Author: Admin * * Created on December 22, 2023, 2:59 PM */ #include <xc.h> #include "config.h" #define _XTAL_FREQ 8000000UL #include "lcd.h" #include <stdio.h> void main(void) { /*8MHz Internal OSC*/ OSCCONbits.IRCF=7; /*Internal ADC FOSC*/ ADCON0bits.ADCS=3; /*Select AN4*/ ADCON0bits.CHS=4; __delay_ms(10); /*Turn On ADC*/ ADCON0bits.ADON=1; /*Right Justify*/ ADCON1bits.ADFM=1; /*VDD and VSS VFRE*/ ADCON1bits.VCFG1=0; ADCON1bits.VCFG0=0; lcdInit(); PORTA=0; TRISA5=1; lcdString("Temperature:"); double temp=0; char message[16]; lcdCommand(0x0C); __delay_ms(1000); while(1){ ADCON0bits.GO=1; while(ADCON0bits.GO==0); temp=((ADRESH<<8)+ADRESL); temp=100*(5.0*temp/1023); sprintf(message,"%3.2f%cC ",temp,223); lcdXY(1,2); lcdString(message); lcdXY(1,3); lcdString("On-Board LM35DZ"); lcdXY(1,4); lcdString("Connects To RA5"); __delay_ms(250); } return; }
I use MPLABX IDE v6.15 and XC8 C compiler v2.36 free edition without code optimization. So it requires a lot of program memory space up to 91% due to C floating point and sprintf function usage.
MPLABX IDE v6.15 and XC8 v2.36 free edition resource usage |
Proteus is popular for most of electronics hobbyist here but we have to pay for license.
Proteus Simulation |
Click here to download its source file.
PIC16F887 Prototyping Board |
No comments:
Post a Comment