Measure temperature precisely with the DS18B20

Read the DS18B20 digital temperature sensor over the 1-Wire bus for readings accurate to about half a degree.
  • Level:Intermediate
  • Duration: 40 min
  • Ages 12+
  • Platform: Arduino

What you'll need

Tools for this lesson

Helpful for this build. Open a tool to see tips and more projects that use it.

Measure temperature precisely with the DS18B20 – EEMENTOR STEM™ lesson

The DS18B20 is a digital temperature sensor that talks over a single data wire, called the 1-Wire bus. It's accurate to about ±0.5 °C, much better than simple analog sensors, so it's great for aquariums, fridges and weather stations.

What you'll learn

  • How the 1-Wire bus works
  • How to use two libraries together
  • How to print temperatures with decimals

What you need

  • UNO R3 board (or any Arduino-compatible board) and USB cable
  • DS18B20 temperature sensor module from the 45-in-1 kit
  • 3 jumper wires

Step 1: Install the libraries

In Tools → Manage Libraries, install OneWire (by Paul Stoffregen) and DallasTemperature (by Miles Burton).

Step 2: Wiring

Breadboard wiring diagram: Measure temperature precisely with the DS18B20
Breadboard view. Check each connection against the wiring table below.

Build it step by step

Plug in all the parts first, then add the wires in this order. In each picture the new wires are bright and the ones from earlier steps are faded. Click a picture to see it larger.

Step 1 of 2: Power rails: 5V and GND from the UNO
Step 1. Power rails: 5V and GND from the UNO
Step 2 of 2: DS18B20 module: S → pin 2
Step 2. DS18B20 module: S → pin 2
Module pin UNO pin
S (signal) 2
Middle (+) 5V
- (ground) GND

The 1-Wire bus needs a pull-up resistor (usually 4.7 kΩ) on the data line. The module has one on board, so you don't need to add it.

FREE

Build this circuit in EEMENTOR Lab

Wire it yourself in your browser before you touch a real wire. No sign-up, nothing to install.

The parts for this circuit laid out in EEMENTOR Lab, ready to wireBuild it yourself
  1. Wire the parts using the wiring table above. Pins with no wire show red.
  2. Press Run, change the Temperature slider under Selected, and watch the reading.

EEMENTOR Lab is free and in beta. Results are a guide; a real build can differ slightly. About the Lab

Step 3: The sketch

#include <OneWire.h>
#include <DallasTemperature.h>

const int ONE_WIRE_PIN = 2;

OneWire oneWire(ONE_WIRE_PIN);
DallasTemperature sensors(&oneWire);

void setup() {
  Serial.begin(9600);
  sensors.begin();
  Serial.print("Sensors found: ");
  Serial.println(sensors.getDeviceCount());
}

void loop() {
  sensors.requestTemperatures();            // start a measurement
  float celsius = sensors.getTempCByIndex(0); // first sensor on the bus

  if (celsius == DEVICE_DISCONNECTED_C) {
    Serial.println("Sensor not found, check the wiring");
  } else {
    Serial.print("Temperature: ");
    Serial.print(celsius, 2); // 2 decimals
    Serial.println(" °C");
  }
  delay(1000);
}

What you should see

Serial Monitor at 9600 baud: one DS18B20 found, room temperature around 21.5 °C, then climbing towards body heat while the sensor is held between your fingers.

How it works

The 1-Wire bus: DS18B20 sensors sharing one data wire on pin 2 with a 4.7 kΩ pull-up, and a timeline of one loop with 750 ms measuring and 1 s delay.

Every DS18B20 has a unique 64-bit address, so many sensors can share one data wire. requestTemperatures() tells all sensors to measure, and getTempCByIndex(0) reads the first one found. A measurement at full resolution takes up to 750 ms, so one reading per second is a good rhythm.

Try this next

  • Hold the sensor between your fingers and watch it climb towards body temperature.
  • Compare it with the analog temperature sensor from the kit. Which one reacts faster, and which is steadier?

Troubleshooting

  • "Sensors found: 0": check the wiring, especially that S is on pin 2.
  • It prints "Sensor not found, check the wiring": the library gave back its "disconnected" value (-127 °C), so the UNO can't hear the sensor. Check the power and ground pins.
  • The reading is always 85 °C: that's the sensor's power-on value. Make sure requestTemperatures() is called before reading.
  • Other strange readings (127.94 °C, −250 or −251), or want to calibrate it? See DS18B20 shows −127 °C? Wiring, error fixes and calibration.