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

| 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.
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);
}
How it works
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.
- The reading is always -127 °C: that's the "disconnected" value. 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.