# LM35 to PIC16F877A

Status
Not open for further replies.

#### manba

##### New Member
hi, there..

I want to interface the LM35 temperature sensor with 16f877a. It's my first time that i use the ADC of 16f877a.

LM35 gives 10mv per 1 Celsius change and it's rang (-55C to +150C). In the program i wanna to calculate the positive and negative degrees.

I know the minimum voltage for the ADC of 16f877a 8 bit is 39mv and 4.8mv for 10bit, i have read it from Nigel's tutorials.

can anyone help me:

1- How to connect the LM35 to the 16f877a, i mean how amplify the mv of LM35.

2- How to calculate the negative and positive degrees (to send them to the LCD).

As i said it's my first time i use the ADC of the pic16f877a and it is difficult to do it from the first time.

#### Gayan Soyza

##### Active Member
You can use hamradio project & get many details how to do a thermo using LM35 + PIC16F877

Note it uses positive temperatures only.

Attached contains the source.

#### Attachments

• 6.7 KB Views: 3,107

#### manba

##### New Member
thank you Gayan for your help...

i still need more details and informations how to calculate the positive and negative degrees?

#### Diver300

##### Well-Known Member
Use an LM74 or an LM71. You can read the temperature digitally.

You don't need any other components.

Negatives are handled in Two's complement - Wikipedia, the free encyclopedia

Here is the code that reads it.

You can slow the code down as much as you want if you need to debug with LEDs or a voltmeter.

Code:
readtemp

bcf	    tncs_port, tncs	;starts on high to low edge

movlw	0x0e			;14 bits are clocked in
movwf	count1

bcf 	temp_lsb, 1		;clear bit 1
btfsc	tsio_port, tsio	;check data from LM74?LM71
bsf 	temp_lsb, 1		;set bit 1 if data is set

bsf 	tsc_port, tsc	;raise clock

rlf 	temp_lsb, f
rlf 	temp_msb, f	;rotates both bytes left

bcf 	tsc_port, tsc	;lower clock

decfsz	count1, f	;decrement counter

bsf 	tncs_port, tncs	;raise Not Chip Select = stop LM74/LM71

retlw	0x00

Last edited:

#### manba

##### New Member
hi, all

thank you Diver300 for your explain but the idea still not clear

#### Diver300

##### Well-Known Member
Temperature is an analogue value. You want to:-

1)Converted temperature to a voltage
2)Amplify voltage to correct range
3)Converted to digital

The LM35 will do (1) and convert the temperature to a voltage.
The voltage it gives out is too small to be read accurately, so you need an op-amp or something to do (2)
Then you do (3) and (4) inside the PIC

Instead, I am suggesting that you buy an LM71 or an LM74.

They do (1), (2) and (3)

All you need to do with the PIC is (4), and the code that I have sent you will do that.

An LM74 is about the same price as an LM35

So the advantages of using an LM74 are:-
Fewer components.
No analogue electronics to worry about.
No timing problems in the code.
No calibration needed

The advantages of using an LM35 are:-
Less pins needed on the PIC
Faster response
Easier to tell what is happening with a voltmeter

If you look up the datasheet for the LM74 at National Semiconductor, you should be able to see how to use one.

#### manba

##### New Member
hi, Diver300

thanks a lot again. I don't have LM74 in our markets we have only LM34-LM35, that why i have to use LM35.

I have a question about the result of the ADC, how to calculate it?
Is it like this calculation:

Vref+ = 5V
Vref- = GND(0V)
minimum output of LM35 = 10mv at 1 Celsius (= 0.01 v)
if the ADC works in 10bit the result will be like this:

5V ------------> 1024
0.01V --------> X

X = 2.048 ( 2 in hex appro)

So each change in temperature will give 2 in the (ADRESL-ADRESH) registers.

is my calculation true?

another question, Can i use to analog channels in the same time?

Last edited:

#### Diver300

##### Well-Known Member
Are you sure you can't get an LM74?

Farnell Export

If you want to measure negative temperatures the LM74 is much easier to use. The LM35 will read negative temperatures, but you need a negative supply voltage if you want to read negative temperatures.

If you only want to read positive temperatures, and you don't need the best accuracy, you can just connect the output of the LM35 to and analogue input of the PIC16F877A.

1 degree change in temperature will give about 2 counts on ADRESH:ADRESL. The LM35 is analogue so the output goes up smoothly, not in steps.

The maximum output of the LM35 is 1.55 V so you can get more resolution if you connect Vref to a lower voltage. You can reduce it to 2.5 V (or 2 V if the supply voltage is less than 4.5 V) and that will give you more counts for each degree change of temperature.

If Vref is 2.5 V, 1 count is 2.5 / 1024 = 2.44 mV so you get just over 4 counts for 1 degree C.

The PIC16F877A has 8 Analogue input channels. You can only use one at a time but reading a channel takes much less than 1 ms so you can read one channel, change to the second channel, read the second channel, and so on.

#### manba

##### New Member
hi, all

Thank you Diver300 alot again. I got it and i have finshed designing the program using the LM35 but the program still need calbiration. I will use the LM74 in future with 16f84.

#### Attachments

• 56.8 KB Views: 5,425
Last edited:

#### snehas

##### New Member
hi manba..........
pls can u give ur code for temp sensor...even v r using LM35-PIC16f877a-lcd combination oly... so if ur code s working pls can u give it...

thank u

Last edited:

#### iwly

##### New Member
Can u also post the source code here, please?

#### arlabstech

##### New Member
You can find details of connecting 16F877A to LM35 on this link:

cheers...

Last edited by a moderator:
Status
Not open for further replies.