How to wire a DS18B20 to an Arduino Uno R4
To wire a DS18B20 to an Arduino Uno R4, connect VDD to 5V, DQ to pin 2 and GND to GND. The sensor and its 4.7 kilohm pull-up both go to 5 V, so the one data line idles at the board’s own level. The sketch below uses the DallasTemperature (DallasTemperature.h) library. It counts the sensors on the one-wire bus and prints the temperature.
- 5 V logic
- Library: DallasTemperature.h
- 3 wires, 1 helper parts
DS18B20 to Arduino Uno R4 wiring table
Every wire in the circuit above, with the part’s pin names and the board’s own labels. The wire colors are the ones the circuit uses; on a real bench any colors work.
| DS18B20 pin | Arduino Uno R4 pin | Wire color | Why |
|---|---|---|---|
| VDD (right leg) | 5V | red | The DS18B20 runs from 3.0 to 5.5 V. |
| DQ (middle leg) | pin 2 | amber | The one data wire, shared by every sensor on the bus. |
| GND (left leg) | GND | blue | Ground, with the flat face toward you. |
| 4.7 k resistor | DQ to 5V | n/a | The pull-up the bus needs: the sensor only ever pulls the line low. |
Helper parts
The extra parts in the circuit above, and where their wires go:
| From | To | Wire color |
|---|---|---|
| R1 (4.7 kΩ) pin 1 | DQ | amber |
| R1 (4.7 kΩ) pin 2 | VDD | red |
Power and logic levels for DS18B20 and Arduino Uno R4
Logic level
The Arduino Uno R4’s pins are 5 V logic. A 5 V part can drive them directly; a part whose signal pins are limited to 3.3 V needs a level shifter.
Supply
The DS18B20 takes 5 V from 5V. That is the Arduino Uno R4’s 5 V rail, so the part shares the current the board can supply.
This pair
On the Arduino Uno R4 the sensor and its pull-up go to 5V. A long cable may need a stronger pull-up, 2.2 k, to keep the edges sharp.
Pins to leave alone
An Uno sketch’s source builds for the R4, but a compiled Uno program will not run on it: different chip. The R4’s pins source less current than an Uno’s, about 8 mA each, so do not drive a relay coil or a motor straight from one.
The working code for the Arduino Uno R4
Library: DallasTemperature (DallasTemperature.h). This is the sketch the circuit above runs.
#include <OneWire.h>
#include <DallasTemperature.h>
const int ONE_WIRE_PIN = 2; // DQ, pin 2, with 4.7 k to 5V
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 conversion on every sensor
float celsius = sensors.getTempCByIndex(0);
if (celsius == DEVICE_DISCONNECTED_C) {
Serial.println("Sensor not found");
} else {
Serial.print("Temperature: ");
Serial.print(celsius, 2);
Serial.println(" C");
}
delay(1000);
}
What you should see
Open the serial monitor at 9600 baud. The sketch prints how many sensors it found and then the temperature in degrees C once a reading is ready; the sensor starts at 22 degrees.
A reading of -127 C, or no sensor found, means the data line has no pull-up or is on the wrong pin.
The serial monitor prints this, recorded from the circuit above left at its defaults:
Sensors found: 1
Temperature: 22.00 CCommon mistakes wiring a DS18B20 to an Arduino Uno R4
No pull-up resistor
Without the 4.7 k from DQ to the supply, the line never goes high, sensors.begin() finds nothing and every reading is -127. In the simulator, delete the resistor and you get exactly that.
The sensor in backwards
With the flat side facing you, the legs are GND, DQ, VDD from left to right. Reversed, a real DS18B20 gets hot fast. Waterproof probes use red for VDD, black for GND and yellow or white for DQ.
Expecting an Uno’s pin current
The R4’s pins are 5 V like an Uno’s, but each one sources only about 8 mA where an Uno’s gives 20. A signal line to the 1-Wire bus is fine; an LED or a coil hung straight off the pin is not.
Build your own DS18B20 project
Open this circuit in the editor, change the wiring or the code, and keep your version in a free account.