An LM35 on an Arduino: temperature from one pin
Beginner
Or see every lesson.
- 505parts on the bench
- 45boards running now
- 1.00xreal time, on every board
The LM35 is the classic analog temperature sensor: three legs, no library, and an output of 10 millivolts for every degree Celsius, starting from 0 volts at 0 degrees. One analogRead() and one line of math give you the temperature.
The circuit above is an Uno with an LM35 on A0, printing the temperature twice a second. Press Run, then drag the slider under the sensor and watch the reading follow. The sketch is the short one below, and further down is a version with no floating point.
What you need
- An Arduino Uno
- An LM35 (LM35DZ) temperature sensor
- Three jumper wires
Wiring
| Part and pin | Goes to | Note |
|---|---|---|
| LM35 VS (left leg) | 5 V | Flat face toward you; it needs 4 V or more |
| LM35 VOUT (middle leg) | A0 | 10 mV a degree |
| LM35 GND (right leg) | GND |
From millivolts to degrees
The LM35 puts out 10 millivolts for each degree: 250 mV at 25 degrees and 1 volt at 100. The Uno’s analogRead() turns 0 to 5 volts into a number from 0 to 1023, so each step is 5000 / 1024, about 4.9 millivolts, and a degree is about two steps.
To get degrees, turn the reading into millivolts and divide by ten. That is reading x 5000 / 1024 / 10, which is the same as reading x 500 / 1024. That one line is the whole conversion.
Because each step is half a degree, the reading moves in steps of about 0.49 degrees. At 25 degrees the input is 250 mV, which is step 51, and the sketch prints "24.90 C". At 100 degrees it is step 205 and the sketch prints "100.10 C". At the default 22.5 degrees it prints "22.46 C".
void setup() {
Serial.begin(9600);
Serial.println("Mokxi Uno: LM35 on A0");
}
void loop() {
int reading = analogRead(A0);
float c = reading * 500.0 / 1024.0;
Serial.print(c);
Serial.println(" C");
delay(500);
}Open this sketch in the editorThe code alone: the editor places the board and the parts it can read from the sketch.
The same thing in whole numbers
You can skip floating point entirely. 5000 / 1024 is exactly 625 / 128, and one millivolt is a tenth of a degree, so reading x 625 / 128 is the temperature in tenths. Print it as the whole part, a dot and the last digit.
With the sensor at 25 degrees this version prints "24.9 C", the same reading with one decimal. Whole-number math is smaller and faster on an Uno, and it is how the built-in that reads an LM35 beside a hall sensor and a slotted wheel does it, with sixteen readings averaged.
void setup() {
Serial.begin(9600);
}
void loop() {
int reading = analogRead(A0);
long tenths = (long)reading * 625 / 128;
Serial.print(tenths / 10);
Serial.print(".");
Serial.print(tenths % 10);
Serial.println(" C");
delay(500);
}Open this sketch in the editorThe code alone: the editor places the board and the parts it can read from the sketch.
LM35 or TMP36
The LM35 and the TMP36 look the same: the same black TO-92 case and the same three legs in the same order. The difference is the offset. A TMP36 reads 500 mV at 0 degrees, so its code subtracts 500 first. An LM35 reads 0 V at 0 degrees, so there is nothing to subtract. Use TMP36 code on an LM35 and every reading comes out 50 degrees low.
The other difference is the supply. The LM35 needs at least 4 volts, so it suits a 5 V Uno. On a 3.3 V board such as an ESP32 or a Pico, use a TMP36, which runs from 2.7 V.
On one supply the LM35 cannot go below 0 volts, so it cannot show temperatures below zero. Drag the slider to -5 degrees and the sketch prints "0.00 C". The datasheet’s full -55 degree range needs a second, negative supply.
Steadier readings
On a real board the reading jumps by a step or two from noise on the 5 V supply and the wires. The simple fix is to take several readings and average them. The sensor-lab built-in adds up sixteen readings, divides by sixteen with a shift, and then converts. Sixteen is a good number on an Uno: the sum of sixteen readings of 1023 still fits in an unsigned int.
Accuracy is a separate question. The LM35D that most kits ship is rated at 0.6 degrees typical at 25 degrees, and 1.5 degrees at worst. Averaging hides the noise but cannot fix that. In Mokxi the sensor gives exactly the number on its slider, so what you see is the conversion alone.
If you need steps finer than half a degree, the Uno’s 1.1 V internal reference helps: analogReference(INTERNAL) makes each step about a millivolt, a tenth of a degree, for temperatures up to about 110 degrees.
Common mistakes
Powering it from 3.3 V. Move the LM35’s supply wire to the Uno’s 3V3 pin here and its output stops being driven: the readings wander, all well over 100 degrees, and none of them is the temperature.
Subtracting 500 mV as if it were a TMP36.
The legs backwards. With the flat face toward you, the left leg is the supply and the right is ground. Check before you power it.
Doing the math in int. reading * 500 is bigger than an int can hold on an Uno once the reading passes 65, so use 500.0, or a long as in the whole-number version.
A long cable with no resistor. The datasheet warns that more than 50 pF on the output wants a 75 ohm resistor in series. Keep the wires short or add it.
Open this circuit in the editor. Arduino Uno: an LM35 on A0, wired as it runs at the top of this page. Change a part or a value and run it again.
Open the circuitQuestions
How do I convert LM35 readings to Celsius on Arduino?
With a 5 V reference, celsius = analogRead(A0) * 500.0 / 1024.0. Each step of the reading is about 0.49 degrees.
Does the LM35 need a library?
No. It is an analog sensor, so analogRead() and one line of math are all it needs.
Can I use an LM35 with a 3.3 V board?
Not within its datasheet: it needs at least 4 V. Use a TMP36 on 3.3 V boards.
What is the difference between the LM35 and the TMP36?
Both give 10 mV a degree. The LM35 starts at 0 V at 0 degrees, the TMP36 at 500 mV, and the TMP36 runs from 2.7 V where the LM35 needs 4 V.
Why does my LM35 reading jump in steps of half a degree?
With the 5 V reference each step of analogRead() is about 4.9 mV, which is about 0.49 degrees on an LM35. Average several readings, or switch to the 1.1 V internal reference for steps of about a tenth of a degree.
Keep going
Where the facts on this page come from
Build this for real
Open the editor, change a value and watch the number move with it. Nothing to install, and no account needed.