Sei sulla pagina 1di 7

4/7/2014

PIC16F877A and LM35 Based Temperature Monitor | eProject

20th January 2012

PIC16F877A and LM35 Based Temperature Monitor

This is a simple project showing you how to read LM35 [http://www.ti.com/lit/ds/symlink/lm35.pdf] analog temperature sensor using a PIC microcontroller and to switch a certain load at a certain temperature.

Software in use:
CCS PIC-C Compiler Labcenter Proteus VSM

Some concepts you need to Know:


ADC is an internal module used to read analog voltages in the form of a digital representation, in this project I've used 16F877a which includes a 10Bit resolution ADC module having 8 channels A0-A5 and E0-E2. One important parameter of the ADC module is it's reference voltage (Vref), which is the maximum voltage an ADC can read, in our case Vref = 5V which is the supply voltage. Another important parameter is the ADC resolution, which determines the minimum value of analog voltage can read (ADC Step size). For example, our ADC is 10Bit resolution with a 5V reference, the range of voltages starting at 0V and ended by 5V is to be divided into equal steps starting at 000 and ended by 1023 (2^10 -1). i.e. if the input voltage is 5V which is the max value, the ADC will read it as 1023, if the input was 2.5V, the reading would be 512 and so on. The ADC step is simply calculated using the equation : Step = Vref/1024 , in our case its 4.883 mV, that's the minimum voltage our ADC can read, so: an input of 4.883mV would give us a reading of 001 an input of 9.766mV would give us a reading of 002, and so on.

LM35 Temperature Sensor:


LM35 is a Three-Pins (Vcc,Output,GND) high precision temperature sensor having a resolution of 10mV/C starting at 0V (i.e. an output of 0V represents a temperature of 0C). So, 10mV ---> 1C 20mV ---> 2C 370mV ---> 37.0C and so on.

Converting ADC Reading to Celsius degrees:


Knowing that our ADC has a step size of 4.883mV, converting our digital reading back to voltage is simply done by multiplying the digital reading by the step size: Vin (in Volts) = DigitalReading * 0.004883 Now, knowing our sensor's sensitivity is 10mV/C, converting this voltage to Celsius is simply done by dividing the input voltage by 0.01, So: Temperature (C) = Vin/0.01 = DigitalReading * 0.4883

The Schematic Design:

http://blog.ghatasheh.com/2012/01/pic16f877a-and-lm35-based-temperature.html

1/7

4/7/2014

PIC16F877A and LM35 Based Temperature Monitor | eProject

[http://2.bp.blogspot.com/-8xC4RgF8scM/TxlLYKaSFaI/AAAAAAAAACk/7FODBf0jvRo/s1600/Schematic.png]

T h e

C-Code:

/ * P I C 1 6 F 8 7 7 Aa n dL M 3 5B a s e dT e m p e r a t u r eM o n i t o r B a d rG h a t a s h e h 0 7 9 @ l i v e . i t * / # i n c l u d e< 1 6 f 8 7 7 a . h > # d e v i c ea d c = 1 0 / /S e tA D Cr e s o l u t i o nt o1 0 B i t # f u s e sX T , N O L V P , N O W D T , N O P R O T E C T # u s ed e l a y ( c l o c k = 4 0 0 0 0 0 0 ) # u s er s 2 3 2 ( b a u d = 9 6 0 0 , x m i t = P I N _ C 6 , r c v = P I N _ C 7 , E R R O R S ) # i n c l u d e" l c d _ f l e x . c " # d e f i n e L O A D P I N _ B 7 # d e f i n e T H R E S3 0 . 0

/ /l o a ds w i t c h i n gt h r e s h o l di nC e l s i u s

i n t 1 6d i g i t a l _ r e a d i n g ; / /A D Cr e s o l u t i o ni s1 0 B i t ,a n8 B i ti n t e g e ri sn o te n o u g ht oh o l dt h e r e a d i n g f l o a tt e m p ; v o i dm a i n ( ) {
http://blog.ghatasheh.com/2012/01/pic16f877a-and-lm35-based-temperature.html 2/7

4/7/2014

PIC16F877A and LM35 Based Temperature Monitor | eProject

/ *A D CI n i t i a l i z a t i o n* / s e t u p _ a d c ( A D C _ C L O C K _ I N T E R N A L ) ;/ /i n i t i a l i z eA D Cw i t has a m p l i n gr a t eo fC r y s t a l / 4M H z s e t u p _ a d c _ p o r t s ( R A 0 _ A N A L O G ) ; / /s e tP I N _ A 0a sa n a l o gi n p u tc h a n n e l s e t _ a d c _ c h a n n e l ( 0 ) ; / /p o i n tA D Ct oc h a n n e l0f o rA D Cr e a d i n g d e l a y _ m s ( 1 ) ; / /A D Cm o d u l ei ss l o w ,n e e d ss o m et i m et oa d j u s t . / *P e r i p h e r a l sC o n f i g u r a t i o n s* / l c d _ i n i t ( ) ; / /T u r nL C DO N ,a l o n gw i t ho t h e ri n i t i a l i z a t i o nc o m m a n d s o u t p u t _ l o w ( L O A D ) ; / /t h el o a di si n i t i a l l yO F F l c d _ g o t o x y ( 1 , 1 ) ; / /p o i n tL C Dc u r s o rt oc o l 1r o w 1 l c d _ p u t c ( " T e m p e r a t u r ei s : " ) ; / /p r i n to nL C D w h i l e ( 1 )/ /i n f i n i t el o o p { d i g i t a l _ r e a d i n g=r e a d _ a d c ( ) ; / /c a p t u r ec u r r e n tt e m p e r a t u r er e a d i n g d e l a y _ u s ( 1 0 0 ) ; / /0 . 1 m sd e l a yf o rA D Cs t a b i l i z a t i o n t e m p=d i g i t a l _ r e a d i n g*0 . 4 8 8 3 ;/ /c o n v e r tr e a d i n gt oC e l s i u s l c d _ g o t o x y ( 1 , 2 ) ; / /p o i n tL C Dc u r s o rt oc o l 1r o w 2 p r i n t f ( l c d _ p u t c , " % 2 . 1 fC " , t e m p ) ;/ /p r i n tv a l u eo nL C D i f ( t e m p > = T H R E S ) o u t p u t _ h i g h ( L O A D ) ; e l s e o u t p u t _ l o w ( L O A D ) ; d e l a y _ m s ( 1 0 0 0 ) ; } } / /C o n t r o lL o a d

/ /1s e c o n dd e l a yb e t w e e nr e a d i n g s

For any questions, do not hesitate to ask :) Badr Ghatasheh 079@live.it [mailto:079@live.it] Posted 20th January 2012 by Badr Ghatasheh Labels: ADC, CCS, LM35, PIC
66

View comments

65 comments

Add a comment as Muhammad Nadeem Najam ud Din

Top comments

http://blog.ghatasheh.com/2012/01/pic16f877a-and-lm35-based-temperature.html

3/7

4/7/2014

PIC16F877A and LM35 Based Temperature Monitor | eProject

hasan AL-omari 5 months ago - Shared publicly can you upload the proteus file ?
1 Reply

Kaiser Mili 4 months ago mmmmmmm neil paloma 2 months ago can you also send me the proteus file or hex file? thanks, nepal_300@yahoo.com

neil paloma 3 months ago - Shared publicly is it possible to send the read data to the mobile phone? and put condition if the temperature exceeds the limit the device will automatically update to the mobile phone... thank you.. :)
1 Reply

View all 6 replies Adison Mhanna 2 months ago Well yes it is simple. But u should know which module you want to use. You are welcome at any time neil paloma 2 months ago can you send me your code about the project you made with this sensor and pic? if it is ok? thanks.. :)

Anonymous 5 months ago please send me the hex file at phhool.fairy@hotmail.com

jen taiji 5 months ago please also send me hex file in jentaiji@gmail.com

jen taiji 5 months ago please also send me hex file in shougiyen@yahoo.com

Wanderley Silva 6 months ago hello badr , just saying that i am one more person who build this circuit with great success , congratulations for your job and patience with the newbies . i made some changes to add a 20x4 lcd and add 4 dvm for use it in my test jig from amplifiers ,, really very helpful

vignesh vishal 8 months ago http://blog.ghatasheh.com/2012/01/pic16f877a-and-lm35-based-temperature.html

4/7

4/7/2014

vignesh vishal 8 months ago

PIC16F877A and LM35 Based Temperature Monitor | eProject

Dude, please send the components required,coding and schematic design for PIC16F877A and LM35 Based Temperature Monitor My mail id is: mail2vicky.7@gmail.com Urgent dude ....Please send it!!!!!!!!!!

Badr Ghatasheh 8 months ago Although it's posted, you just need to read it, dude! :) Adison Mhanna 8 months ago i did send it to u

Adison Mhanna 8 months ago here is the code I am using: #include <16F877A.h> #fuses XT,NOLVP,NOWDT,NOPROTECT #use delay(clock=4000000) #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7) void main() { int16 digital_reading; float temp; setup_adc(ADC_CLOCK_INTERNAL); setup_adc_ports(RA0_ANALOG); set_adc_channel(0); delay_ms(500); printf("setup done\n"); output_high(PIN_C3); while(1){ digital_reading = read_adc(); delay_us(100); temp = digital_reading * 0.4883; printf("%2.1f C\n",temp); delay_ms(1000); output_toggle(pin_c3); } } Badr Ghatasheh 8 months ago Just set the ADC resolution to 10 bits, add this "#device adc=10" to the second line of your code.

Adison Mhanna 8 months ago Mr. Badr, very good job. Yet I am working on a similar project, but instead of displaying the temperature value on the LCD, I am sending it to the PC using MAX232 and serial port to save it on a database for logging purpose. I have a problem that I am getting wrong and different values. here is an example of some values that I am getting: 3.4 C 10.2 C 9.7 C 7.8 C 8.3 C 7.8 C 8.7 C 9.2 C 10.7 C 8.7 C 5.3 C 8.7 C can you tell me what might I doing wrong? I can send you the code with the diagram that I am using, but I don't know where to post them. thank you Badr Ghatasheh 8 months ago Email the code to badr.ghatasheh@gmail.com

sabrine 11 months ago please i just want to know what is the function of : relay,diode,resistor2,transistor? i compiled the program without those devices and it works.

Badr Ghatasheh 8 months ago It's simply a load that gets switched ON/OFF on a certain temperature threshold.

Anonymous 1 year ago Sorry! I need to connect 4 temperature sensors LM35, and looking the temperatures separatly with a switch buttons at 100m of wire (the LM35 is instaled in greenhouses). How it I make?

Leo Sotto 1 year ago


http://blog.ghatasheh.com/2012/01/pic16f877a-and-lm35-based-temperature.html

leo_sotto14@yahoo.com thats my email add tnx.. :D

5/7

4/7/2014

leo_sotto14@yahoo.com thats my email add tnx.. :D

PIC16F877A and LM35 Based Temperature Monitor | eProject

Leo Sotto 1 year ago hi please send me the hex code for this program because it can't run on my css it said "Lcd.c fle cannot found

Badr Ghatasheh 1 year ago This is a formatting string, 2.1 means two digits before the decimal point and ine after it (00.0), f means float, and C will be printed as is

Anonymous 1 year ago what does this command mean? "%2.1f C"..thanks for your answer.

student 1 year ago do you have the codes for a price computing digital weighing scale? i am also using a pic16f877a.. please help :| thanks.

Badr Ghatasheh 1 year ago I've replied to your email.

Anonymous 1 year ago Hi,can the circuit drives a 12V relay? For that , i need to change my power supply? But problem is the ic can only take 5VDC right? please help. Thanks.

Badr Ghatasheh 1 year ago Thats why they sell 5V relays! Why use a 12v relay when you can use a 5v one?

Momen Elwan 1 year ago hi badr i wanna use pic16f877a and i want to write with MicroC Pro ... can u help me on the Lm35 and how can i read it and convert the data to the pic and display it on lm016 lcd ??

Badr Ghatasheh 1 year ago Am not that familiar with MikroC, I myself used CCS compiler. What I can say is that the logic behind both is the same, understand how to do it in CCS it wouldn't be hard to migrate the code to MikroC.

Anonymous 1 year ago Hi,can the circuit drives a 12V relay? For that , i need to change my power supply? But problem is the ic can only take 5VDC right? please help. Thanks.
http://blog.ghatasheh.com/2012/01/pic16f877a-and-lm35-based-temperature.html 6/7

4/7/2014

PIC16F877A and LM35 Based Temperature Monitor | eProject

Anonymous 1 year ago can you tell me how to compile the code , what do i need to run the code and connect it to the design ? sorry but i'm new to this, so i need help thanks

Show more

http://blog.ghatasheh.com/2012/01/pic16f877a-and-lm35-based-temperature.html

7/7

Potrebbero piacerti anche