STANNUM
Open the simulator

Tutorials / Sensors

LM35 temperature sensor

Ten millivolts per degree, straight from the sensor: read the voltage, multiply by 100.

Parts in the example:
  • LM35
  • Active buzzer
The LM35 block as it appears on the canvas.

What it is

The LM35 is the simplest temperature sensor to use: its output pin carries 10 mV per degree Celsius, with 0 V at 0 °C. 25 °C is 0.25 V; 100 °C is 1.0 V. No calibration, no library — a voltage and one multiplication.

In the simulator

The block has a Temp slider from −10 to 100 °C and shows the output voltage and the ADC count. Below 0 °C the output sits at 0 V, as the basic LM35 circuit does (reading negative temperatures needs an extra resistor on the bench).

Pins

PinWhat it is
Soutput, 10 mV per °C — to a GPIO with an ADC
3V3power — to a 3V3 symbol
GNDground — to a GND symbol

Wiring

The circuit below is the example Thermometer with alarm — LM35 + buzzer from the lab — open it with Project → Open example… and it comes ready to run.

The wired circuit, as the lab draws it.
LM35 pinGoes to
SD32 on the board (GPIO 32)
3V3a 3V3 symbol
GNDa GND symbol

Code

sketch.cpp
#include <Arduino.h>

const int LM35 = 32;      // 10 mV per degree Celsius
const int BUZZER = 26;
const float LIMITE_C = 30.0;

void setup() {
  Serial.begin(115200);
  pinMode(BUZZER, OUTPUT);
}

void loop() {
  // 12-bit ADC: 0..4095 = 0..3.3 V -> temp = voltage * 100
  float temp = analogRead(LM35) * 330.0 / 4095;
  bool alarme = temp > LIMITE_C;
  digitalWrite(BUZZER, alarme ? HIGH : LOW);
  Serial.printf("temp=%.1f C %s\n", temp, alarme ? "ALARM!" : "ok");
  delay(300);
}

The conversion in one line: analogRead(LM35) * 330.0 / 4095 — 4095 counts are 3.3 V, and 3.3 V would be 330 °C. Above 30 °C the sketch sounds the buzzer.

Run it

Press Build and run. The first build of a project takes a while; after that, only what changed is rebuilt.

The simulation running: the canvas reacts and the serial monitor shows what the code prints.

Drag the slider past 30 °C and the buzzer sounds; the serial monitor prints the temperature with one decimal and “ALARM!”.

How the simulation models it

Try this

  1. Print the temperature in Fahrenheit as well.
  2. Average 32 readings — and notice that here it changes nothing, then think about why it would on a bench.

See also