Built-in project · Arduino Uno R3

LM35 temperature sensor on A0, on an Arduino Uno

An LM35 on A0 gives 10 millivolts a degree; the sketch turns the reading into degrees Celsius once a second. The whole build, an Arduino Uno and 1 more part, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Beginner About 10 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $29. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • Reading an analog temperature sensor
  • Converting an analogRead number into real units
  • How 10 mV per degree turns into a formula
  • Printing a float with Serial.print
  • Difficulty: Beginner
  • Time: About 10 minutes
  • Board: Arduino Uno R3
  • Parts: 2

How it works

The LM35 puts out 10 millivolts for every degree Celsius, starting from zero. At 25 degrees it outputs 250 mV. VOUT goes to A0 on the Uno.

The sketch reads A0, then turns the reading into degrees with reading * 500.0 / 1024.0. That works because the 5 V reference gives about 4.9 mV per step, so one degree is about two steps. It prints the result with a C twice a second.

Parts list

2 parts, plus the jumper wires. They retail for about $29; on Mokxi they're free.

Kits that can build it

This starter kit has every part in the circuit. Each kit's page lists what else it can build.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Temperature sensor, LM35 pin VSArduino Uno R3 pin 5V
Temperature sensor, LM35 pin GNDArduino Uno R3 pin GND
Temperature sensor, LM35 pin VOUTArduino Uno R3 pin A0

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// LM35 temperature sensor on A0.
//
//   LM35 VS -> 5V   VOUT -> A0   GND -> GND
//
// 10 mV a degree from zero: 250 mV is 25 C. With a 5 V reference each step of
// analogRead is 4.9 mV, so a degree is about two steps.
//
// Drag the sensor's temperature.

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);
}

Code walkthrough

The key functions of the sketch, and what each one does.

setup sketch.ino

Runs once. Serial.begin(9600) opens serial output at 9600 baud and prints a title line, so you know the LM35 sketch is running.

void setup() {
  Serial.begin(9600);
  Serial.println("Mokxi Uno: LM35 on A0");
}

loop sketch.ino

Reads A0, then converts it: the float c line multiplies by 500.0 and divides by 1024.0, since 5 V is 500 degrees at 10 mV each. Serial.print and Serial.println show the number and the unit. Then delay(500) waits.

void loop() {
  int reading = analogRead(A0);
  float c = reading * 500.0 / 1024.0;
  Serial.print(c);
  Serial.println(" C");
  delay(500);
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The temperature is far too high or low This project
    Check the pin order: VS to 5V, VOUT to A0, GND to ground. A swapped pin gives odd values.
  • The result is a whole number off by a few tenths This project
    The Uno's analog input only has 1024 steps, so each step is about half a degree. That is normal.
  • Readings are about 0 V or make no sense on a 3.3 V board. Temperature sensor
    The LM35 needs at least 4 V supply to work. Use a 5 V supply, or choose another sensor like the TMP36 for 3.3 V boards.
  • The temperature is noisy or jumps around. Temperature sensor
    It outputs 10 mV per degree C. Use the matching ADC reference, average several readings, and add a 0.1 uF capacitor near the output.
  • Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
    Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected.
  • Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
    Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.