All articles

    The DS18B20 Temperature Sensor: Good Accuracy and Several Sensors on One Wire

    The DS18B20 explained: the 1-Wire protocol, the essential 4.7k resistor, the waterproof version, parasite power mode, connecting several sensors to the same pin and reading each sensor's address, the OneWire and DallasTemperature libraries, conversion time and resolution and reading without waiting, wiring long cables, an aquarium heater control project, and a fault table.

    Updated 5 min read

    If the DHT11 isn't accurate enough, or you need to measure the temperature of water or something far away, or you want several sensors on one pin, the DS18B20 is your choice.

    All the sensors on one data wire, and each sensor has its own address
    All the sensors on one data wire, and each sensor has its own address

    Its advantages

    • A range from −55 up to 125 °C.
    • Accuracy of about ±0.5 °C in the range from −10 up to 85.
    • Digital: no ADC issues or noise on an analog signal.
    • Each sensor contains a different 64-bit address, so you can put several sensors on the same wire.
    • There's a waterproof version in a stainless steel sleeve with a cable, excellent for tanks, heaters and fridges.

    Wiring

    PinConnection
    GNDGround
    DQ (data)Any digital pin, + a 4.7k resistor to VCC
    VDD3.3 or 5 V

    The 4.7k resistor isn't optional. Without it the reading will come out as -127 or 85 every time. It's a pull-up for the 1-Wire protocol. Some ready-made modules have the resistor fitted.

    Cable colours on the waterproof version are usually: red VCC, black GND, and yellow (or white) data. Check with the seller.

    And the ordinary package (like a TO-92 transistor): facing you with the flat side towards you and the pins down, from the left: GND, DQ, VDD. If you reverse it, it gets very hot within seconds, so disconnect immediately.

    Parasite power (just two wires)

    The sensor can take its power from the data line itself if you connect VDD to ground, so you need only two wires. It saves a wire on long cables, but it's less stable, especially with several sensors and 12-bit resolution. If you can run 3 wires, do.

    The code

    Two libraries: OneWire and DallasTemperature.

    #include <OneWire.h>
    #include <DallasTemperature.h>
    
    OneWire bus(2);
    DallasTemperature sensors(&bus);
    
    void setup() {
      Serial.begin(9600);
      sensors.begin();
      Serial.print("Sensors: ");
      Serial.println(sensors.getDeviceCount());
    }
    
    void loop() {
      sensors.requestTemperatures();
      Serial.println(sensors.getTempCByIndex(0));
      delay(1000);
    }

    For several sensors, use getTempCByIndex(1) and (2)… or, better, read each sensor's address once and use it, so the order doesn't change.

    Several sensors: knowing which is which

    The order by index depends on the addresses, not on where the sensor is, and if you replace a sensor the order may get mixed up. The fix: print the addresses once:

    DeviceAddress a;
    for (int i = 0; i < sensors.getDeviceCount(); i++) {
      sensors.getAddress(a, i);
      for (int j = 0; j < 8; j++) { Serial.print(a[j], HEX); Serial.print(" "); }
      Serial.println();
    }

    Warm one sensor with your hand, see which address's reading rises, and write it into the code:

    DeviceAddress tank = {0x28, 0xFF, 0x64, 0x1E, 0x0F, 0x10, 0x02, 0x3A};
    float t = sensors.getTempC(tank);

    That way "the tank" is always the tank, however many sensors you add. Printing numbers in HEX is explained in the Serial Monitor article.

    Resolution and speed

    ResolutionTemperature stepApproximate conversion time
    9-bit0.5 °C94 ms
    12-bit0.0625 °C750 ms

    The default is 12-bit. That means a reading takes about three quarters of a second. You can change it with sensors.setResolution(10) if you don't need that resolution. And if you're in a hurry, use setWaitForConversion(false) and read later, as with the millis idea:

    sensors.setWaitForConversion(false);
    sensors.requestTemperatures();
    unsigned long tReq = millis();
    // ... do other things ...
    if (millis() - tReq >= 750) {
      float t = sensors.getTempCByIndex(0);
      sensors.requestTemperatures();      // start the next conversion
      tReq = millis();
    }

    And note: a step of 0.0625 doesn't mean the accuracy is 0.0625. The accuracy is still ±0.5, but small changes show up, which is useful for knowing whether the temperature is rising or falling.

    Long cables and several sensors

    • Connect the sensors one after another on a single line (a daisy chain), not as a star radiating from one point. Long branches cause reflections that spoil the signal.
    • Twisted cable (such as network cable): one pair for data and ground.
    • The longer the cable, the smaller the resistor (3.3k or 2.2k).
    • Tens of metres and tens of sensors are possible, but test before you install.

    The waterproof sleeve is slow

    The sensor sits inside stainless steel and resin, so when you move it from air to water it takes 10–30 seconds for the reading to settle. That isn't a fault. If you're measuring something that changes quickly, use the ordinary sensor stuck firmly to the surface.

    Project: an aquarium heater

    A waterproof sensor in the tank, and a relay switching the heater, without the relay clicking every second:

    float t = sensors.getTempC(tank);
    if (t < 25.5) digitalWrite(RELAY, HIGH);   // on below 25.5
    if (t > 26.5) digitalWrite(RELAY, LOW);    // off above 26.5

    The gap between the two thresholds is called hysteresis, the same idea as in the comparator. And since the heater runs on 220 V, read mains safety first. And if you want the threshold adjustable and saved, see saving settings in EEPROM.

    Common problems

    ReadingUsual cause
    -127The sensor isn't connected or the resistor is missing
    Exactly 85You read before the conversion finished, or a supply problem (85 is its power-on value)
    Sensor count is zeroWrong pin, or the sensor is reversed
    Readings jump occasionallyA long cable or star wiring
    The sensor is very hotThe pins are reversed
    • There are many counterfeit versions, some less accurate. If accuracy matters, buy from a trusted source and compare with a known thermometer.

    FAQ

    Does it work with an ESP32 at 3.3 V? Yes, power it from 3.3 V and keep the resistor at 3.3 V as well.

    DS18B20 or a thermistor? A thermistor is cheaper and withstands higher temperatures, but it needs calculations and calibration. Compare them in the NTC thermistor article.

    Circuits Nerd
    © 2026 Circuits Nerd. All rights reserved.