12F675 Tutorial 4 :
Making an LM35 temperature recorder.
Measuring temperature is easy using an LM35. This page shows you how
you can make an LM35 an temperature recorder by using the 12F675 PIC
microcontroller as the controller and data store.
It generates serial output so that you can view the results on a PC and it also
calculates the temperature reading in Fahrenheit sending both to the serial
port at half second intervals.
The project uses the code from the previous tutorials to report the temperature
to the PC using the serial port so the serial RS232 data format is generated in
Jump to circuit.
Jump to Solderless breadboard.
Jump to Circuit Diagram.
Jump to Software.
The LM35 is a
precision temperature sensor. It is guaranteed accurate to ±¼°Cat 25°C (At different
temperatures it is less accurate! but it is never more than 2°C inaccurate and it
probably is not this inaccurate anyway it's just the manufacturers maximum
limits that may apply).
Typically is stays accurate to within ±¾°C over its temperature
range so this is a good general purpose sensor and it's easy to
It generates a linear output voltage using a centigrade scale - generating 10mV
of output voltage for every degree centigrade change and there are several
versions for operation over different temperature ranges:
Note: The project code calculates the
temperature in Fahrenheit and generates both Centigrade
and Fahrenheit outputs to the
Temperature recorder :
recorder : pinout for theLM35DZ (from the
The LM35 is
connected to analogue input AN0 which is also the data input line for
programming the 12F675 using ICSP so you
need a way of connecting the sensor and the programming input at the same time
with the programming input overriding the sensor output (and not damaging the
This is done
here by using 1k resistor that reduces the current flowing back into the sensor
and at the same time is not too large (so that the ADC can easily convert the
sensor output value - the impedance must be equal to or smaller than 10k Ohm
from the sensor).
reference for the circuit is taken from pin 6 using a resistor divider giving a
2.5V reference. This is simply done to increase the resolution of the ADC as
for the LM35 only 0-1V is generated so you loose ADC range when using a 5V
reference. You could use a lower reference value but this value gives
Alternatively you could use an amplifier to scale the LM35 output up
which would make the ADC less sensitive to noise but for this project it is
simpler not to do so.
Note: The large decoupling capacitor on the supply
input of the 12F675. This reduces noise overall and gives a more consistent
reading. However using a plug block and ADC is not a very good idea as
there is no ground plane and no control over current paths which you would be
able control in a pcb.
In a commercial system the internal ADC is often not used at all as it is
essential to separate the noise introduced to the ADC using separate grounds
and shielding - some designs encase the ADC in a custom metal shield and along
with a ground plane connecting to the shield gives the best possible result.
To overcome noise problems on the ADC the software averages the input readings
so you get a better result.
Add the components
(at top right to) the temperature recorder - wires and R3,R4,R5 and the LM35
temperature sensor (U4) and the decoupling capacitor C4.
LM35 Temperature Recorder Circuit
The analogue reference for the ADC is taken from the power supply
resistive divider to the 12F675 input pin 6 and for the 7805 its accuracy is
specified as ±5% so the accuracy of the ADC is only 5% due to the reference
-the divider also introduces a 1% error giving a 6% error overall.
Note: Since the 7805 is
only accurate to ±5% the accuracy of the temperature reading will be accurate
(plus errors in the ADC and temperature sensor itself and any noise introduced
the the analogue input and the reference). However the reference source gives
you the biggest error - the overriding accuracy - if you used a more accurate
voltage supply then the ADC accuracy would become more important as well as the
temperature sensor accuracy etc.
The software uses the Soft USART (transmit only)
described in the previous tutorial and uses the built in MikroC routines to get
the data from analogue input pin AN0.
Source code files
To get the file software project files and c source code click here
|// Temperature recorder analogue
val = ADC_Read(0);
// more code adds up 10
readings of ADC
val = ((val/MAX_AVG)*122)/50;
// Convert to Fahrenheit x 5/9
// add 32 scaled to 3 digits
val = ((val*18)/10)+320;
interesting parts of the software are shown above. The variable val is an
unsigned int so the maximum value it can store is 65535
The reference in use is 2.5V so for the 10bit ADC each ADC bit is worth
2.5/1023 = 2.44mV
If you work out values generated for a maximum temperature of 100°C using the scale
factor 2.44mV (or 244/100)
100 * 10mV = 1.0V
1.0V/2.44mV = 410
410 * 244 = 100,040 which will not fit into an
So this scale factor does
not work for all input values
By using a little maths it can be made to fit -you need to reduce the top
number to fit. e.g.
410 * 122 = 50,020 which does fit.
Dividing by 50 gets back to the correct scale factor of 244.
So the scale 122/50 works
for all input values.
This is an
example of avoiding the use of floating point variables which take up too much
resources. You can still make the system work but you have to be careful when
using fixed types and you have to check all input values and outputs to make
sure they fit.
Averaging would be
better done in the PC as it has more resources - the same goes for calculating
and displaying the temperature in Fahrenheit but this gives a demonstration of
what you can do.
Note: The RAM is used up since a bug in
MikroC 22.214.171.124 puts strings int RAM - in future versions this will be
Typical output from the
The left most value
is the RAW ADC value, the next is the temperature sensor output in degrees
centigrade and the next is the temperature sensor output in degrees Fahrenheit.
Note: You have to put in the decimal point so the above readings are:
Learn to program in C
in easy steps.
Essential & EASY:
PIC C Programming Course:
Claim Your: Useful
"Arduino Software Guide"
| About Me
Jump from 12F675 Tutorial 2 : LM35 Temperature
Best-Microcontroller-Projects Home Page