# calibrating lm35 analog value to degree Celsius using 8051

Discussion in 'Embedded Systems and Microcontrollers' started by bobparihar, Dec 9, 2014.

1. ### bobparihar Thread Starter Member

Jul 31, 2014
93
0
i am working on a project which objective is to measure the temperature of surroundings..using AT89s52 micro-controller and ADC0804 for A to D conversion.. the temperature meant to be shown on LCD..
after doing some research i managed to read ADC values coming from the LM35 and displaying it on LCD..

now... finally the tricky thing which i fails to understand...( may be because i won't get any useful material to study)..
so i want to know...
- how to convert the ADC value to corresponding Degree Celsius
-how the step change get affected by varying the Vref/2 and how this affect my code..

My circuit Diagram and CODE( iam getting the varying ADC value on LCD as i change the temperature) is given under the attachment

File size:
1.8 KB
Views:
36
File size:
650 bytes
Views:
37
File size:
24.5 KB
Views:
133
2. ### shteii01 AAC Fanatic!

Feb 19, 2010
3,505
512
Normal people use equation. LM35 output is linear, so the formula for conversion is in the form: y=mx+b (equation of a line).
y is temperature in degrees C
m is slope of the line
b is y-intercept
Check the datasheet for the graphs. Pick two points on the line and solve for the equation of the line.

Or copy this: http://www.embedidea.com/temperature-monitor-with-8051/
http://www.circuitstoday.com/thermometer-using-8051

Last edited: Dec 9, 2014
JWHassler and bobparihar like this.
3. ### JohnInTX Moderator

Jun 26, 2012
2,394
1,051
To expand on shetiio1's description..

As long as what you want and what you get are both linear, its pretty easy to fix it up in a program. First, ignore the ADC counts part of it..
If you plot what you get vs. what you want you get two lines. Graph them. At the lower end of both, there will be an offset. This is a simple additive value that moves the first endpoint of what you have to what you want. If the lines diverge from that point, you need to scale the input i.e. change the slope of what you get to what you want. This is a simple multiplication by a fraction - the fraction may or may not be >1 depending on the circumstances. After applying the offset, multiplying every point on the line by the scale factor will cause the two lines to coincide. That's what you want: a 1:1 relationship between the sensor reading and what you want to display.

Once you have this in mind, going from ADC counts to something useful is really the same thing. The ADC gives you another linear function, likely with offset and gain (span, slope) differences. The way I do it is to combine all the factors on paper, figuring the gain-offset functions of all of the stages (ADC, sensor etc.) and combine them into one linear function that well.. changes what you have into what you want. A few jabs at a calculator will get the net offset and gain (span, slope - whatever you want to call it) that you need.

I usually do the offset first to nail the zero then multiply by the gain. For fixed point, multiply the numerator by some factor (1000 etc) then divide. The fixed point result will be scaled by the 1000 (or whatever you pick) so that you don't lose precision. For example, if you need a gain of 3/2, multiply the numerator as 3*1000 and divide by 2.

bobparihar likes this.
4. ### bobparihar Thread Starter Member

Jul 31, 2014
93
0
thanks for your help sir!
i am a noob here and this graph doesn't make me any sense.. its from LM35 Data sheet

i did read some tutorials.. but those are not complete tutorial.. they don't explain what should be the voltage at Vref/2 pin of ADC0804 . and how the step change changes by varying that Vref/2 voltage..
all i get to know that LM35 output voltage changes about 10mV per degree rise in temprature...

all this Vref thing confusing me

5. ### bobparihar Thread Starter Member

Jul 31, 2014
93
0
somebody answer plzz

6. ### MrChips Moderator

Oct 2, 2009
12,636
3,455
The graph shown has nothing to do with temperature conversion.

You have already stated the temperature conversion, 10mV per degree C.

What is the conversion in your ADC?
That is, what is Vref and how many bits of data?

7. ### JohnInTX Moderator

Jun 26, 2012
2,394
1,051
Vref/2 is a feature of this particular ADC. Whatever you want Vref to be, you apply half of that to Vref/2 (most ADCs just use Vref). So if you use a 1.250 bandgap reference, the converter will convert between 0 and 2.500V on its differential inputs.

For an 8 bit ADC this means that the input range is chopped up into 256 (2^8) values, one for each LSB's (each ADC count's) worth. With a 2.500 Vref, each LSB represents (2.500V/256LSBs = ) .0098volts/LSB.
So here your transfer function (temperature->ADC code) is 10mV per degC / .0098V per LSB = 1.024 counts/degC.
At 25degC the ADC code would be 25*1.024 = 25.6 which rounds to 26 decimal or 1A hex.

1.024 counts/degC is not a real convenient number since it does not exactly correspond to 1 count per degree or something handy. The SPAN (gain, calibration) is off. You can fix this in a few ways
1) adjust Vref so that each LSB is exactly 1 ADC count. The datasheet shows several examples of how to adjust the span(full-scale). Note that V reference's are available that exactly map to binary ADC's i.e. 4.096V for 12 bit ones, etc. For this one, Vref = 2.56V makes each ADC count 10mv which corresponds nicely to the sensor output. Since you need Vref/2, look for a 1.28V reference.
2) fix the SLOPE of the linear function in firmware by multiplying each reading by 1/1.024.
There are good arguments for each method - it depends on how you want to do it.

A few final observations:
This example does not make good use of the full ADC range i.e. at the full 150degC of the LM35, the ADC code is 154 decimal, wasting almost 1/2 of the ADC's range. You can fix this by
1)amplifying the input signal (analog scaling - the datasheet covers this)
2)lowering Vref so that the ADC's range is spread over a smaller voltage range.
3)use a sensor with more V/degC

Note that in any case, the ADC reading is a simple code representing what fraction of Vref the input is - expressed in BINARY. Your code will have to convert that into whatever format you need from there.

Also note that what I have described is by no means all you have to consider in designing an ADC system but these are the high points.

And MrChips is right about the graph. All it is saying is that the minimum supply voltage the thing will work at is affected by temperature and the current you are drawing from it.

Have fun.

Last edited: Dec 10, 2014
bobparihar likes this.
8. ### bobparihar Thread Starter Member

Jul 31, 2014
93
0
great explanation sir.. thakyou very much

if u have time please take a look at this problem http://forum.allaboutcircuits.com/threads/interfacing-gsm-sim900-modem-with-8051.104775/