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

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.
| 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.
Build this circuit in EEMENTOR Lab
Wire it yourself in your browser before you touch a real wire. No sign-up, nothing to install.
Build it yourself
- Wire the parts using the wiring table above. Pins with no wire show red.
- Press Run, change the Temperature slider under Selected, and watch the reading.
Stuck? See the finished circuit →
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

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.
- 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.

