Referring to ADC module of PIC18F4550, there are many analog output sensor to interfaces with this MCU. LM35 is a simple analog temperature sensor, creating analog voltage output representing its temperature conversion in degree Celsius. The step temperature from this device is 10 mV per degree Celsius. I don't want to put all its details here. For more information about using this device, please see this post.
In this post, LM35 connects to AN2 analog channel of the ADC. The MCU reads the analog voltage, converting it to temperature value in both degree Celsius and degree Fahrenheit.
![]() |
A temperature reading of 31.2 degree Celsius. |
A very simple LM35 comes in a transistor-like TO-92 package.
![]() |
LM35DZ in TO-92 package. |
C source code fed from GitHub.
#include<18f4550.h> | |
#device ADC=10 | |
/*Use high speed clock no PLL prescaler | |
no system clock post scaler */ | |
#fuses HS,PLL1,CPUDIV1 | |
#use delay(clock=20M) | |
#define LCD_ENABLE_PIN PIN_B2 | |
#define LCD_RS_PIN PIN_B0 | |
#define LCD_RW_PIN PIN_B1 | |
#define LCD_DATA4 PIN_B4 | |
#define LCD_DATA5 PIN_B5 | |
#define LCD_DATA6 PIN_B6 | |
#define LCD_DATA7 PIN_B7 | |
#include<lcd.c> | |
void adcConfig(void){ | |
//RA3 As Input | |
set_tris_a(0x04); | |
//PORTD as output | |
set_tris_d(0x00); | |
//Set RA0...RA2 To Analog | |
setup_adc_ports(AN0_TO_AN2); | |
//Select ADC internal RC Clock | |
setup_adc(ADC_CLOCK_INTERNAL); | |
} | |
float getTemperature(void){ | |
set_adc_channel(2); | |
delay_ms(1); | |
int16 analogValue=read_adc(); | |
while(!adc_done()); | |
float temperature = (analogValue*5.0)/1024; | |
temperature*=100; | |
return temperature; | |
} | |
void main(){ | |
float temp; | |
lcd_init(); | |
adcConfig(); | |
while(1){ | |
lcd_gotoXy(1,1); | |
printf(LCD_PUTC,"LM35 Temperature"); | |
lcd_gotoXy(1,2); | |
temp=getTemperature(); | |
printf(LCD_PUTC,"%0.1f%cC %0.1f%cF ",temp,223,((temp*1.8)+32),223); | |
delay_ms(250); | |
} | |
} |
Click here to download the zip file of this example.
![]() |
Schematic Diagram |
No comments:
Post a Comment