Measure temperature with the LM35

FunduinoPublished on

Temperaturen bis 150 °C messen – auch in Flüssigkeiten

Der LM35 ist einer der einfachsten Temperatursensoren überhaupt: Er braucht keine Bibliothek und keinen Bus, sondern gibt die Temperatur direkt als Spannung aus – genau 10 Millivolt je Grad Celsius. Du liest diese Spannung an einem analogen Eingang ein und rechnest sie um.

In dieser Anleitung lernst du beide Rechenwege kennen: den schnellen mit dem map-Befehl und den genaueren, der die Nachkommastellen behält. Zwischendurch baust du ein Warnsignal ein, das ab 30 °C ertönt.

Arduino with screw terminal shield and waterproof LM35 probe in a glass of water
Measuring water temperature with the waterproof LM35 and a screw terminal shield
LM35 temperature sensor with red housing and three-pin socket
The LM35 variant with an XH2.54-3P connector
1 / 2

You need this

LM35 temperature sensor - waterproof, -40 to +110 °C, analog LM35 temperature sensor - waterproof, -40 to +110 °C, analog 4,85 € *Item no. F23107318About the product — LM35 temperature sensor - waterproof, -40 to +110 °C, analog
Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino 11,12 € *Item no. F23107480About the product — Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino
Screw terminal shield - 20 I/O pins, for Arduino UNO R3 Screw terminal shield - 20 I/O pins, for Arduino UNO R3 4,00 € *Item no. F23108559About the product — Screw terminal shield - 20 I/O pins, for Arduino UNO R3

All 3 parts together 19,97 € *

* All prices include statutory VAT, plus shipping costs.

Material

You will need for this project:

  • 1× microcontroller with a suitable USB cable
  • 1× LM35 temperature sensor - either the waterproof version with cable or the version with an XH2.54-3P socket
  • 1× screw terminal shield for the Arduino UNO (only for the waterproof version)
  • 3× male-to-male jumper wires (only for the version with a socket)

For the warning signal add-on, you will also need:

  • 1× piezo buzzer

How the LM35 measures

The LM35 has three connections: Red goes to 5V, black to GND, and yellow is the signal wire. The sensor outputs a voltage on this wire that directly corresponds to the temperature - exactly 10 millivolts per degree Celsius.

The measurable range is from 0 °C to 150 °C, so the output voltage ranges from 0 V to 1.5 V. The microcontroller reads this voltage at an analog input and converts it into a temperature value. The measurement accuracy is about ±1.5 °C.

The waterproof version is housed in a metal sleeve with a cable. This also makes it possible to measure the temperature of liquids.

Typical applications for these sensors:

  • Air conditioning in cars and homes
  • Household appliances: dishwashers and washing machines maintain their temperature, and kettles switch off when they reach the target temperature
  • Heating control: switch off once the desired temperature is reached
  • Overheating protection, for example in smartphones

If the sensor is connected the wrong way around, it can be permanently damaged. Check the wire color mapping before switching it on.

For more accurate measurements, it’s worth using an external power supply via the barrel jack (9 V power adapter or 9 V battery) instead of USB power.

LM35 temperature sensor with red housing and three-pin socket
The LM35 variant with an XH2.54-3P connector
Close-up of the three-pin socket with red, black, and yellow contacts
The three connections of the LM35
1 / 2

Structure

The same assignment always applies, regardless of the version:

  • Red → 5V
  • Black → GND
  • Yellow → A0

For the waterproof LM35 with exposed wire ends, it’s best to connect the three wires to a screw terminal shield that you plug into the Arduino. For the version with an XH2.54 socket, connect the socket directly to the board using three male-to-male wires.

Arduino with screw terminal shield and waterproof LM35 probe in a glass of water
Measuring water temperature with the waterproof LM35 and a screw terminal shield
Arduino with connected LM35 sensor
Connection to the Arduino: Yellow to A0, red to 5V, black to GND
1 / 2

The map command

At the analog input, the Arduino does not return a voltage value, but a number between 0 and 1023 for the range from 0 to 5 volts. The LM35’s 1.5 volts correspond to a number of approximately 307.

The map command converts this number back into a temperature. The general syntax is map(a, b, c, d, e):

  • a = the number to be converted
  • b = the minimum of the measurement range
  • c = the maximum of the measurement range
  • d = the minimum of the output range
  • e = the maximum of the output range

In our case, the range from 0 to 307 is converted to the range from 0 to 150 degrees Celsius. After uploading, open the Serial Monitor at 9600 baud.

map() calculates with integers. The displayed temperature therefore has no decimal places and changes in steps of one. The step after next shows you how to calculate more precisely.

Serial monitor with temperature readings in degrees Celsius
This is what the output looks like in the Serial Monitor.
Sketch 1: Convert temperature using map()
int LM35 = A0;      // The sensor is connected to analog pin A0. From now on, we'll call the pin "LM35".
int sensorwert;     // The raw measurement is stored here.
int temperatur = 0; // The converted temperature value will be stored here later.
int t = 500;        // Time interval between measurements in milliseconds.

void setup()
{
  // Start serial communication so the measurements are sent to the computer.
  // In the Arduino IDE, open Tools -> Serial Monitor.
  Serial.begin(9600);
}

void loop()
{
  // Read the sensor value
  sensorwert = analogRead(LM35);

  // Convert the sensor value using the map command
  temperatur = map(sensorwert, 0, 307, 0, 150);

  // Send the value to the Serial Monitor
  Serial.print(temperatur);
  Serial.println(" degrees Celsius");

  // Pause until the next measurement
  delay(t);
}

Extension: warning signal from 30 degrees

Now a tone should sound as soon as the temperature reaches 30 °C. To do this, connect a piezo buzzer: the positive terminal to pin 5 and the other terminal to GND.

In the sketch, an IF condition checks on every iteration whether the measured temperature has reached or exceeded 30. If it has, tone() produces a continuous 1000 Hz tone; if the value drops again, noTone() turns it off.

There are two types of buzzers. An active buzzer has its own tone generator built in and produces a sound even with a simple digitalWrite(pin, HIGH). A passive piezo buzzer, on the other hand, needs an alternating signal - digitalWrite leaves it silent, which is why this sketch uses tone(). Both types work with tone().

Sketch 2: Warning signal from 30 degrees
int LM35 = A0;
int sensorwert;
int temperatur = 0;
int t = 500;
int piezo = 5;      // Das Wort "piezo" steht jetzt fuer die Zahl 5, der Summer haengt also an Pin 5.

void setup()
{
  Serial.begin(9600);

  // Der Pin fuer den Summer wird als Ausgang festgelegt.
  pinMode(piezo, OUTPUT);
}

void loop()
{
  sensorwert = analogRead(LM35);
  temperatur = map(sensorwert, 0, 307, 0, 150);

  Serial.print(temperatur);
  Serial.println(" Grad Celsius");

  // IF-Bedingung: Wenn die Temperatur 30 Grad erreicht oder ueberschreitet ...
  if (temperatur >= 30)
  {
    // ... soll ein Ton mit 1000 Hertz erklingen.
    tone(piezo, 1000);
  }
  // Und wenn das nicht so ist ...
  else
  {
    // ... bleibt es still.
    noTone(piezo);
  }

  delay(t);
}

Calculate more precisely instead of mapping

Instead of using map(), you can also calculate the temperature directly - this preserves the decimal places. The calculation consists of two steps.

First, the analog-to-digital converter reading is converted back into a voltage. The Arduino divides its 5 volts - or 5000 millivolts - into 1024 possible values. The measured value is therefore multiplied by 5000 and divided by 1024.

In the second step, the voltage is converted into a temperature. Since 10 millivolts correspond to exactly one degree, the result is then divided by 10.

Serial monitor with temperature values to two decimal places
Output of the calculated temperature in the Serial Monitor
Sketch 3: Calculate temperature directly
// LM35 am analogen Eingang A0
int LM35 = A0;

// Der vom Analog-Digital-Wandler gelieferte Zahlenwert (0 bis 1023)
float digitalerWert;

// Daraus berechnete Spannung in Millivolt
float gemesseneSpannung;

// Daraus berechnete Temperatur in Grad Celsius
float gemesseneTemperatur;

void setup()
{
  // Seriellen Monitor starten
  Serial.begin(9600);
}

void loop()
{
  // Zahlenwert am Eingang A0 lesen
  digitalerWert = analogRead(LM35);

  // Verhaeltnis zwischen 5V (= 5000 mV am Arduino) und der
  // Anzahl der moeglichen Werte (1024) herstellen -> Spannung in Millivolt
  gemesseneSpannung = digitalerWert * 5000 / 1024;

  // Der LM35 liefert 10 mV je Grad, also durch 10 teilen -> Grad Celsius
  gemesseneTemperatur = gemesseneSpannung / 10;

  // Temperatur anzeigen
  Serial.print(gemesseneTemperatur);
  Serial.println(" Grad C");

  // 2 Sekunden Pause bis zur naechsten Messung
  delay(2000);
}

Change number format: period to comma

Arduino uses a period as the decimal separator. With a small addition, you can convert it to the comma notation commonly used in German.

To do this, first convert the number to a string and use the replace command to replace the period with a comma. Calculations still use the original number - only the display changes.

Sketch 4: Replace period with comma
// LM35 am analogen Eingang A0
int LM35 = A0;

// Der vom Analog-Digital-Wandler gelieferte Zahlenwert (0 bis 1023)
float digitalerWert;

// Daraus berechnete Spannung in Millivolt
float gemesseneSpannung;

// Daraus berechnete Temperatur in Grad Celsius
float gemesseneTemperatur;

void setup()
{
  // Seriellen Monitor starten
  Serial.begin(9600);
}

void loop()
{
  // Zahlenwert am Eingang A0 lesen
  digitalerWert = analogRead(LM35);

  // Verhaeltnis zwischen 5V (= 5000 mV am Arduino) und der
  // Anzahl der moeglichen Werte (1024) herstellen -> Spannung in Millivolt
  gemesseneSpannung = digitalerWert * 5000 / 1024;

  // Der LM35 liefert 10 mV je Grad, also durch 10 teilen -> Grad Celsius
  gemesseneTemperatur = gemesseneSpannung / 10;

  // Zahlenwert in eine Zeichenkette (String) umwandeln
  String AnzeigeTemperatur = String(gemesseneTemperatur);

  // replace: Punkt durch Komma ersetzen
  AnzeigeTemperatur.replace(".", ",");

  // Temperatur anzeigen
  Serial.print(AnzeigeTemperatur);
  Serial.println(" Grad C");

  // 2 Sekunden Pause bis zur naechsten Messung
  delay(2000);
}

Output in the Serial Monitor

Open in the Arduino IDE Tools → Serial Monitor and set the baud rate to 9600 in the bottom right. A new line with the measured temperature will then appear every two seconds.

To test it, hold the probe in your hand briefly or, if it’s waterproof, immerse it in a glass of warm water. The reading will reflect the change within a few seconds.

Serial monitor with temperature values to two decimal places
Output of the calculated temperature in the Serial Monitor
Rate the instructions

Comments

Ask a question or share an experience

Contact us if something doesn’t work. Please include your board and the error message.

We review every comment before publication. Your email address remains with us.