Skip to content

Arduino - Temperature Sensor

Measuring temperature is a fundamental task in countless applications, from weather stations to safety systems. This tutorial will guide you through using two different types of temperature sensors with your Arduino: a classic analog sensor (LM35) and a modern digital sensor (DS18B20).

Method 1: The LM35 Analog Sensor (A Classic Approach)

Section titled “Method 1: The LM35 Analog Sensor (A Classic Approach)”

The LM35 is a precision integrated-circuit temperature sensor whose output voltage is linearly proportional to the Celsius (Centigrade) temperature. It’s simple to use but, as an analog sensor, its accuracy can be affected by noise and voltage fluctuations.

  • Output: Linear +10mV per °C scale factor.
  • Accuracy: Typically ±0.5°C at room temperature.
  • Range: −55°C to 150°C.
  • 1 × Arduino Uno R3
  • 1 × Breadboard
  • 1 × LM35 sensor
  • Jumper wires

The LM35 has three pins. When viewing the flat face of the sensor, the pins are (from left to right):

  • Pin 1 (Vs) → Arduino 5V
  • Pin 2 (Vout) → Arduino Analog Pin A0
  • Pin 3 (GND) → Arduino GND
/**
* @file lm35_analog_temp_sensor.ino
* @brief Reads temperature from an LM35 analog sensor and prints it to the Serial Monitor.
*/
// --- Constants ---
const int TEMP_SENSOR_PIN = A0; // The analog pin connected to the LM35's Vout
void setup() {
// Initialize serial communication for outputting the results
Serial.begin(9600);
Serial.println("LM35 Temperature Sensor Example");
}
void loop() {
// Read the raw analog value from the sensor (0-1023)
int sensorValue = analogRead(TEMP_SENSOR_PIN);
// Convert the analog reading to a voltage (in millivolts)
// The Arduino's ADC has 1024 steps (0-1023) and the reference voltage is 5V (5000mV).
// Voltage (mV) = (sensorValue / 1024.0) * 5000.0
float voltage = sensorValue * (5000.0 / 1024.0);
// Convert the voltage to temperature in Celsius
// The LM35 has a scale factor of 10mV per degree Celsius.
// Temperature (°C) = Voltage (mV) / 10
float temperatureC = voltage / 10.0;
// Print the temperature to the Serial Monitor
Serial.print("Temperature: ");
Serial.print(temperatureC);
Serial.println(" °C");
// Wait for a second before the next reading
delay(1000);
}

Older tutorials often use a ‘magic number’ like 0.48828125. Our code avoids this by showing the calculation explicitly, which is better practice. The conversion relies on two steps:

  1. Analog to Voltage: We convert the 0-1023 reading from analogRead() into a voltage, knowing the Arduino’s reference is 5V (5000mV).
  2. Voltage to Temperature: We use the LM35’s datasheet specification (10mV/°C) to convert the voltage into a temperature reading.

Method 2: The DS18B20 Digital Sensor (A Modern Approach)

Section titled “Method 2: The DS18B20 Digital Sensor (A Modern Approach)”

The DS18B20 is a digital temperature sensor that is more accurate and robust against noise than the LM35. It communicates using the 1-Wire protocol, which allows you to connect multiple sensors to a single digital pin.

  • Digital Signal: Less susceptible to electrical noise over long wires.
  • High Accuracy: ±0.5°C accuracy.
  • Unique Address: Each sensor has a unique 64-bit address, allowing multiple sensors on the same data bus.
  • No External Calibration Needed: Factory calibrated.
  • 1 × DS18B20 temperature sensor
  • 1 × 4.7kΩ resistor (pull-up resistor)

The DS18B20 also has three pins. Be sure to check your sensor’s datasheet, but a common pinout is (from left to right, flat face towards you):

  • Pin 1 (GND) → Arduino GND
  • Pin 2 (DQ) → Arduino Digital Pin 2
  • Pin 3 (VDD) → Arduino 5V

Crucial Step: Just like the DHT sensor, the 1-Wire protocol requires a pull-up resistor. Connect the 4.7kΩ resistor between the DQ (data) and VDD (power) lines.

You’ll need two libraries: OneWire and DallasTemperature. Install them from the Arduino Library Manager.

/**
* @file ds18b20_digital_temp_sensor.ino
* @brief Reads temperature from a DS18B20 digital sensor and prints it.
*/
#include <OneWire.h>
#include <DallasTemperature.h>
// --- Constants ---
const int ONE_WIRE_BUS_PIN = 2; // Data wire is plugged into digital pin 2 on the Arduino
// --- Global Objects ---
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS_PIN);
// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);
void setup() {
Serial.begin(9600);
Serial.println("DS18B20 Digital Temperature Sensor Example");
// Start up the library
sensors.begin();
}
void loop() {
// Request temperatures from all devices on the bus
Serial.print("Requesting temperatures...");
sensors.requestTemperatures();
Serial.println("DONE");
// Fetch the temperature in Celsius for the first device (index 0)
float temperatureC = sensors.getTempCByIndex(0);
// Check if the data is valid. A value of -127.00 indicates a read error.
if (temperatureC == DEVICE_DISCONNECTED_C) {
Serial.println("Error: Could not read temperature data");
return;
}
// Print the data
Serial.print("Temperature: ");
Serial.print(temperatureC);
Serial.println(" °C");
delay(1000);
}

Combine either sensor with two LEDs (one red, one blue) and a buzzer to create a simple environmental monitor.

  • If temperature > 28°C, turn on the red LED.
  • If temperature < 20°C, turn on the blue LED.
  • For a more advanced version, make the buzzer sound if the temperature exceeds a critical safety threshold.