Arduino Uno R4 · DS18B20

Arduino Uno R4 DS18B20 Simulator: Wiring, Code and a Live Circuit

This is a DS18B20 digital temperature sensor wired to an Arduino Uno R4 Minima with its 4.7 k pull-up, read live with the OneWire and DallasTemperature libraries: one reading a second, to a hundredth of a degree.

Three legs and one resistor: VDD to 5V, GND, and DQ to pin 2 with the pull-up from DQ to 5V. In the editor, drag the sensor’s temperature slider and the next reading follows.

  • 5 V logic
  • Library: DallasTemperature.h
  • DQ on pin 2
  • 4.7 k pull-up
Arduino Uno R4 · DS18B20live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running on the simulator with the sketch below. Press what can be pressed; click anything else to open it in the editor.

Wiring the DS18B20 to the Arduino Uno R4

Every connection in the circuit above, with the board’s own pin names.

DS18B20 pinArduino Uno R4 pinWhy
VDD (right leg)5VThe DS18B20 runs from 3.0 to 5.5 V.
DQ (middle leg)pin 2The one data wire, shared by every sensor on the bus.
GND (left leg)GNDGround, with the flat face toward you.
4.7 k resistorDQ to 5VThe pull-up the bus needs: the sensor only ever pulls the line low.

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 on the Arduino Uno R4

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.

On a real bench

The Uno R4 Minima fits every Uno shield and every Uno breadboard layout, so wire the DS18B20 exactly as you would on an Uno R3. It has a USB-C socket, and its VIN accepts up to 24 V where an Uno R3 stops at 12.

The code

The sketch the circuit above runs. Open it in the editor, change a line and press Run: it compiles in your browser.

DS18B20 on Arduino Uno R4 pin 2
#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);
}

OneWire oneWire(ONE_WIRE_PIN) runs the 1-Wire protocol on the pin, and DallasTemperature sensors(&oneWire) speaks to DS18B20s over it. sensors.begin() searches the bus, and getDeviceCount() says how many sensors answered.

requestTemperatures() starts a conversion on every sensor at once and waits for it: 750 ms at the default 12 bits. getTempCByIndex(0) then reads the first sensor found. DEVICE_DISCONNECTED_C, -127, means no sensor answered.

Each DS18B20 has a unique 64-bit address, so several can share this one pin. Serial.begin(9600) opens the serial monitor. Serial is the USB port; Serial1 is on pins 0 and 1, which on the R4 are free for other serial devices.

Common mistakes with the DS18B20 on the 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.

Questions and answers

Can I simulate a DS18B20 with an Arduino Uno R4 Minima?

Yes. The circuit at the top of this page is an Arduino Uno R4 Minima with a DS18B20, running in your browser on a simulated Renesas RA4M1 (Cortex-M4). The simulated sensor speaks real 1-Wire, timing and addresses included, so the libraries run unchanged. Press Run it in the editor to change the wiring or the code; it is free and needs no account.

Why do I get -127?

That is DEVICE_DISCONNECTED_C: no sensor answered. The pull-up is missing, DQ is on a different pin from the sketch, or the sensor is wired backwards.

Can I connect several DS18B20s to one pin?

Yes. Wire them all in parallel to pin 2, with one pull-up for the bus, and read each with getTempCByIndex(0), (1) and so on, or by its address.

What is parasite power?

A mode where VDD is tied to GND and the sensor draws power from the data line through the pull-up. It saves a wire but needs a strong pull-up during conversions; three wires, as here, is more reliable.

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.