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.

IntermediateRuns in your browser. Free, and no account needed.

Thermometers on one wire · Arduino Uno R3live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

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");
  }
}

Parts list

5 parts, plus the jumper wires. Every one is in the editor's parts bin.

How it is wired

3 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.

  • Arduino Uno R3 pin 2; Temperature sensor, DS18B20 (1) pin DQ; Temperature sensor, DS18B20 (2) pin DQ; Resistor, 4.7k Ω pin 1
  • Arduino Uno R3 pin 5V; Temperature sensor, DS18B20 (1) pin VDD; Temperature sensor, DS18B20 (2) pin VDD; Resistor, 4.7k Ω pin 2
  • Ground: Arduino Uno R3 pin GND; Temperature sensor, DS18B20 (1) pin GND; Temperature sensor, DS18B20 (2) pin 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.