Arduino DS18B20 with DallasTemperature: Wiring, Code and Simulator

To build this Arduino DS18B20 with DallasTemperature project you need an Arduino Uno and a temperature sensor DS18B20 and a 4.7 kΩ resistor. OneWire + DallasTemperature read a DS18B20 thermometer on one pin, with its 4.7 kΩ pull-up. Make the 5 connections in the wiring table, upload the Arduino C++ code, and select the sensor and move its Temperature slider while it runs.

Open this circuit in the free Arduino Uno simulator

Parts list

QtyPartWhat it does
1Arduino Unothe microcontroller board
1Temperature Sensor DS18B201-Wire digital thermometer: DQ to a pin with a 4.7 kΩ pull-up to 5 V, VDD to 5 V, GND. Read it with OneWire + DallasTemperature. Legs GND, DQ, VDD.
14.7 kΩ resistorLimits current. 220 Ω in series with an LED; 10 kΩ as a pull-up or pull-down.

Wiring

#FromTo
1Arduino Uno GNDTemperature Sensor DS18B20 GND
2Arduino Uno 5VTemperature Sensor DS18B20 VDD
3Arduino Uno pin 2Temperature Sensor DS18B20 DQ
44.7 kΩ resistor leg 1Temperature Sensor DS18B20 DQ
5Arduino Uno 5V4.7 kΩ resistor leg 2

Step by step

  1. Open the simulator. Open the BitForBytes Embedded Lab in your browser and start from the Arduino Uno. No install or account is needed.
  2. Add the parts. Add a temperature sensor DS18B20 and a 4.7 kΩ resistor from the parts palette.
  3. Wire 1. Connect Arduino Uno GND to Temperature Sensor DS18B20 GND.
  4. Wire 2. Connect Arduino Uno 5V to Temperature Sensor DS18B20 VDD.
  5. Wire 3. Connect Arduino Uno pin 2 to Temperature Sensor DS18B20 DQ.
  6. Wire 4. Connect 4.7 kΩ resistor leg 1 to Temperature Sensor DS18B20 DQ.
  7. Wire 5. Connect Arduino Uno 5V to 4.7 kΩ resistor leg 2.
  8. Paste the Arduino C++ code. Paste the Arduino C++ code below into the code editor.
  9. Run it. Press Run. Select the sensor and move its Temperature slider while it runs.

Arduino C++ code

// OneWire + DallasTemperature: read a DS18B20 digital thermometer on one data pin.
// The DQ line needs a 4.7 kOhm pull-up to 5 V, as on a real board.
#include <OneWire.h>
#include <DallasTemperature.h>

OneWire bus(2);
DallasTemperature sensors(&bus);

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

void loop() {
  sensors.requestTemperatures();
  float c = sensors.getTempCByIndex(0);
  Serial.print("Temperature: ");
  Serial.print(c, 2);
  Serial.println(" C");
  delay(1000);
}

What to try

Select the sensor and move its Temperature slider while it runs.

Frequently asked questions

How do I wire the DS18B20 with DallasTemperature project on an Arduino Uno?

Make 5 connections: Arduino Uno GND to Temperature Sensor DS18B20 GND; Arduino Uno 5V to Temperature Sensor DS18B20 VDD; Arduino Uno pin 2 to Temperature Sensor DS18B20 DQ; 4.7 kΩ resistor leg 1 to Temperature Sensor DS18B20 DQ; Arduino Uno 5V to 4.7 kΩ resistor leg 2.

What parts do I need for this Arduino project?

An Arduino Uno and a temperature sensor DS18B20 and a 4.7 kΩ resistor.

Can I simulate this Arduino project online for free?

Yes. The BitForBytes Embedded Lab runs this exact circuit and Arduino C++ code in your browser for free, with no install and no hardware. The sketch is compiled with the real Arduino toolchain and runs on an emulated ATmega328P.

What does the code do?

OneWire + DallasTemperature read a DS18B20 thermometer on one pin, with its 4.7 kΩ pull-up.

More Arduino Uno projects