Built-in project · Arduino Uno R3

Thermometers on one wire, on an Arduino Uno

Two DS18B20s on pin 2 and one 4k7 pull-up. The 1-Wire search finds both by their serial numbers, and each is then read by name. The whole build, an Arduino Uno and 3 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Intermediate About 15 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $34. On Mokxi they're free.

Step by step, with the wiring and the common mistakes: Arduino DS18B20 Temperature Sensor: Wiring and Code

The circuit runs here once the page has loaded.

What you'll learn

  • The 1-Wire bus: many devices on one data pin
  • Why a pull-up resistor is needed
  • Unique device addresses
  • Using the OneWire and DallasTemperature libraries
  • Formatting a temperature with integer math
  • Difficulty: Intermediate
  • Time: About 15 minutes
  • Board: Arduino Uno R3
  • Parts: 5

How it works

Two DS18B20 thermometers share a single data wire on pin 2. One 4.7k resistor pulls the wire up to 5V for both. Every DS18B20 has its own unique 64-bit address, so the Uno can find each one and talk to it by name.

At startup, the sketch searches the wire, counts the sensors and prints each address. Then it asks both to measure at once and prints each temperature in degrees Celsius with two decimals. A reading takes about 750 ms. If a sensor stops answering, its line says gone.

Parts list

5 parts, plus the jumper wires. They retail for about $34; on Mokxi they're free.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Temperature sensor, DS18B20 (1) pin DQArduino Uno R3 pin 2
Temperature sensor, DS18B20 (2) pin DQArduino Uno R3 pin 2
Resistor, 4.7k Ω pin 1Arduino Uno R3 pin 2
Temperature sensor, DS18B20 (1) pin VDDArduino Uno R3 pin 5V
Temperature sensor, DS18B20 (2) pin VDDArduino Uno R3 pin 5V
Resistor, 4.7k Ω pin 2Arduino Uno R3 pin 5V
Temperature sensor, DS18B20 (1) pin GNDArduino Uno R3 pin GND
Temperature sensor, DS18B20 (2) pin GNDArduino Uno R3 pin GND

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// Two DS18B20 thermometers on one wire.
//
// Both sensors share pin 2, with a single 4k7 pull-up to 5 V for the whole
// bus. Each has its own 64-bit ROM code, and the 1-Wire search finds them one
// by one: every sensor still in the search sends its next bit and then its
// complement, the open-drain wire ANDs them together, and where they disagree
// the master picks a branch and the sensors on the other one drop out.
//
// The sketch finds them once, keeps their addresses, and after that asks each
// one by name. Drag the sliders on the two sensors to warm or cool them.
//
// getTemp() is the reading as the library keeps it, a whole number of 1/128ths
// of a degree, and the sketch prints it with integer sums. getTempC() hands
// back a float instead, which prints just as well, but the float printing
// alone would take this example past the 8 KB an Uno example is allowed.

#include <OneWire.h>
#include <DallasTemperature.h>

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

DeviceAddress found[2];
int count = 0;

void printAddress(const uint8_t *address) {
  for (int i = 0; i < 8; i++) {
    if (address[i] < 16) Serial.print('0');
    Serial.print(address[i], HEX);
  }
}

void setup() {
  Serial.begin(9600);
  sensors.begin();
  count = sensors.getDeviceCount();
  if (count > 2) count = 2;
  Serial.print(count);
  Serial.println(" sensors on the wire");
  for (int i = 0; i < count; i++) {
    sensors.getAddress(found[i], i);
    Serial.print("sensor ");
    Serial.print(i);
    Serial.print(" is ");
    printAddress(found[i]);
    Serial.println();
  }
}

void loop() {
  // One CONVERT T for everybody: both sensors measure at once, 750 ms.
  sensors.requestTemperatures();
  for (int i = 0; i < count; i++) {
    long raw = sensors.getTemp(found[i]);
    Serial.print("sensor ");
    Serial.print(i);
    Serial.print(": ");
    if (raw == DEVICE_DISCONNECTED_RAW) {
      Serial.print("gone");
    } else {
      // Hundredths of a degree, rounded, then the two halves either side of
      // the point. A negative reading carries its own minus sign.
      long hundredths = (raw * 100 + (raw < 0 ? -64 : 64)) / 128;
      if (hundredths < 0) {
        Serial.print('-');
        hundredths = -hundredths;
      }
      Serial.print(hundredths / 100);
      Serial.print('.');
      if (hundredths % 100 < 10) Serial.print('0');
      Serial.print(hundredths % 100);
      Serial.print(" C");
    }
    Serial.print(i + 1 < count ? "   " : "\n");
  }
}

Code walkthrough

The key functions of the sketch, and what each one does.

printAddress sketch.ino

Prints an eight-byte address as hex. It adds a leading zero to bytes under 16, so every byte takes two characters and addresses line up.

void printAddress(const uint8_t *address) {
  for (int i = 0; i < 8; i++) {
    if (address[i] < 16) Serial.print('0');
    Serial.print(address[i], HEX);
  }
}

setup sketch.ino

Starts serial and the sensors, then asks for the device count and limits it to 2. For each one it saves the address in found with getAddress and prints it. After this, the sketch always asks sensors by address.

void setup() {
  Serial.begin(9600);
  sensors.begin();
  count = sensors.getDeviceCount();
  if (count > 2) count = 2;
  Serial.print(count);
  Serial.println(" sensors on the wire");
  for (int i = 0; i < count; i++) {
    sensors.getAddress(found[i], i);
    Serial.print("sensor ");
    Serial.print(i);
    Serial.print(" is ");
    printAddress(found[i]);
    Serial.println();
  }
}

loop sketch.ino

Sends one requestTemperatures() so both sensors measure together. Then, for each saved sensor, it reads the raw value with getTemp, which is in 1/128ths of a degree, and prints gone if it is disconnected. Otherwise it rounds to hundredths and prints degrees with a decimal point.

void loop() {
  // One CONVERT T for everybody: both sensors measure at once, 750 ms.
  sensors.requestTemperatures();
  for (int i = 0; i < count; i++) {
    long raw = sensors.getTemp(found[i]);
    Serial.print("sensor ");
    Serial.print(i);
    Serial.print(": ");
    if (raw == DEVICE_DISCONNECTED_RAW) {
      Serial.print("gone");
    } else {
      // Hundredths of a degree, rounded, then the two halves either side of
      // the point. A negative reading carries its own minus sign.
      long hundredths = (raw * 100 + (raw < 0 ? -64 : 64)) / 128;
      if (hundredths < 0) {
        Serial.print('-');
        hundredths = -hundredths;
      }
      Serial.print(hundredths / 100);
      Serial.print('.');
      if (hundredths % 100 < 10) Serial.print('0');
      Serial.print(hundredths % 100);
      Serial.print(" C");
    }
    Serial.print(i + 1 < count ? "   " : "\n");
  }
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The sketch reports 0 sensors on the wire. This project
    Check the 4.7k resistor goes between DQ (pin 2) and 5V. Without a pull-up the bus cannot work.
  • Only one sensor is found. This project
    Both sensors need VDD to 5V, GND to ground and DQ joined to pin 2.
  • The sensor reads -127 C or no device is found. Temperature sensor
    Put a 4.7 k resistor between the data line and VCC. Check the pinout, since swapping VCC and GND can overheat the sensor. Then confirm the data pin matches your code.
  • It always shows 85 C. Temperature sensor
    85 C is the power on value, meaning the conversion has not finished or the supply is weak. Wait for the conversion (up to 750 ms at 12 bit) before reading.
  • The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
    Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure.
  • A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
    Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground.
  • Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
    Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected.
  • Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
    Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.