Home ▸ Arduino ▸ Sensors Interfacing ▸ Interface DS18B20 Sensor

DS18B20 Arduino Tutorial: Read Single and Multiple Temperature Sensors on One Pin with OLED Output

The DS18B20 is a digital temperature sensor that talks to the Arduino over a single data wire. Every sensor carries its own 64-bit address, so several sensors can share the same pin and be read one by one. This tutorial explains the pinout, the 4.7 kΩ pull-up resistor, the OneWire and DallasTemperature libraries, the resolution settings, and the code for one sensor, for several sensors and for an SSD1306 OLED display.

The display section builds on the SSD1306 OLED tutorial, and the Arduino UNO pinout guide lists the pins used in the wiring. Humidity can be added with the DHT11 and DHT22 tutorial, and the Arduino sensor interfacing tutorials collect the other sensors on the site.

How the DS18B20 Sensor Works

The DS18B20 measures temperature with an internal sensor and an ADC, and it sends the result as a digital value. The Arduino therefore needs no analog input and no conversion formula for the sensor itself.

DS18B20 Specifications

The values below come from the manufacturer datasheet.

FeatureDS18B20
Temperature range-55 to +125 °C
Accuracy±0.5 °C from -10 to +85 °C, and ±2 °C over the full range
Resolution9 to 12 bits, 12 bits by default
Supply voltage3.0 to 5.5 V
InterfaceOne-Wire, one data line plus ground
AddressUnique 64-bit code for every sensor
PackagesTO-92, and waterproof probes that use the same chip
Three versions of the DS18B20 temperature sensor: a bare TO-92 package, a breakout module and a waterproof stainless steel probe

One-Wire Protocol

The One-Wire protocol uses one data line, called DQ, for both directions. The Arduino starts every transfer, and the sensor answers on the same wire. Because each sensor has its own 64-bit address, the Arduino can first send the address of one sensor and then ask only that sensor for its temperature. The datasheet describes the number of devices on one bus as virtually unlimited, and the practical limit depends on the cable length and the wiring.

The protocol is not the same as the single-wire protocol of the DHT11 and DHT22, so a DHT library cannot read a DS18B20.


Standard Breakout Module vs Waterproof Probe

FeatureStandard TO-92 or breakout moduleWaterproof probe
EnclosureBare package or small PCBStainless steel tube
CableShort pins or headerFixed cable of about 1 m, depending on the seller
Pull-up resistorOften already on the breakout boardNot included, so it must be added
Typical usePrototypes and room temperatureLiquids, soil, outdoor and pipe measurements
Code differenceNoneNone

The pull-up and cable details depend on the seller, so the product page should be checked before the sensor is wired.


DS18B20 vs DHT22 and SHT21

The DS18B20 measures temperature only, while the DHT22 and the SHT21 also measure humidity. The DS18B20 is the better choice when the sensor must be waterproof, when several temperature points are needed on one pin, or when the temperature goes below -40 °C.

FeatureDS18B20DHT22
MeasuresTemperatureTemperature and humidity
Temperature range-55 to +125 °C-40 to +80 °C
Temperature accuracy±0.5 °C from -10 to +85 °CBetter than ±0.5 °C
InterfaceOne-WireProprietary single wire
Several sensors on one pinYes, each sensor has a unique addressNo, one sensor per pin
Waterproof versionYesNo
Conversion or sampling timeUp to 750 ms at 12 bitsAbout 2 s
Supply voltage3.0 to 5.5 V3.3 to 6 V

The SHT21 uses the I2C interface and is covered in the SHT21 tutorial. Other options on the site include the AHT20, the SHT30, SHT31 and SHT35, the BME280 and the BMP180.


DS18B20 Pinout

The TO-92 version of the DS18B20 has three pins.

PinNameFunction
1GNDGround
2DQOne-Wire data line, which needs a 4.7 kΩ pull-up to VDD
3VDDPower supply, 3.0 to 5.5 V

With the flat face of the package toward the reader and the pins pointing down, the pins run from left to right in the order GND, DQ and VDD. This orientation should be checked against the printed datasheet drawing before the first power-up, because a reversed supply makes the sensor hot and can destroy it.

DS18B20 TO-92 pinout with the flat face toward the viewer showing GND, DQ and VDD from left to right

DS18B20 Arduino Wiring

A single sensor needs three connections and one resistor. The resistor holds the data line high between transfers and is required for the sensor to respond.

DS18B20 to Arduino Connections

Sensor pinArduino pin
VDD5V or 3.3V
DQD2, with a 4.7 kΩ resistor from DQ to VDD
GNDGND

Any digital pin can be used for DQ, and this tutorial uses D2.

DS18B20 to Arduino Uno wiring diagram with DQ on pin 2, VDD on 5V, GND on GND and a 4.7 kilohm resistor between DQ and 5V

DS18B20 Pull-Up Resistor

A 4.7 kΩ resistor between DQ and VDD is the standard value, and the datasheet recommends a pull-up of about 5 kΩ. Without it, the sensor may not answer, and the library then returns -127 °C. Many breakout boards already carry the resistor, but waterproof probes usually do not. The same single resistor serves every sensor on the line, so a second resistor is not added for each sensor. On long cables, a lower value can improve the signal, and the value should then be tested with the actual cable.


DS18B20 Arduino Library Setup

Two libraries handle the One-Wire timing and the sensor commands, so the sketches need no low-level code.

Install the OneWire and DallasTemperature Libraries

  1. Open the Arduino IDE and go to Sketch, then Include Library, then Manage Libraries.
  2. Search for OneWire and install the library by Paul Stoffregen.
  3. Search for DallasTemperature and install the library by Miles Burton.
  4. Accept the offer to install any missing dependencies.
Arduino IDE Library Manager showing the OneWire library by Paul Stoffregen ready to install
Arduino IDE Library Manager showing the DallasTemperature library by Miles Burton ready to install

DS18B20 Arduino Serial Monitor Code

The first sketch reads one sensor every second and prints the temperature on the Serial Monitor at 9600 baud. The Arduino UART tutorial explains the Serial Monitor settings.

Single DS18B20 Sketch

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

const uint8_t ONE_WIRE_BUS = 2;   // DQ pin of the sensor

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

void setup() {
  Serial.begin(9600);
  sensors.begin();

  // Report a missing sensor once at startup
  if (sensors.getDeviceCount() == 0) {
    Serial.println("No DS18B20 found. Check the wiring and the 4.7k pull-up resistor.");
  }
}

void loop() {
  sensors.requestTemperatures();               // Starts a conversion and waits for it to finish
  float tempC = sensors.getTempCByIndex(0);    // Sensor with index 0

  // DEVICE_DISCONNECTED_C (-127) means the sensor did not answer
  if (tempC == DEVICE_DISCONNECTED_C) {
    Serial.println("Sensor not found. Check the wiring and the pull-up resistor.");
  } else {
    Serial.print("Temperature: ");
    Serial.print(tempC);
    Serial.println(" °C");
  }

  delay(1000);
}

How the Serial Monitor Code Works

  • OneWire handles the data line on pin 2, and DallasTemperature uses it to talk to the sensors.
  • sensors.begin() finds the sensors on the bus, and getDeviceCount() returns how many were found.
  • requestTemperatures() starts a conversion on every sensor. With the default settings the call waits until the conversion is finished, which takes up to 750 ms at 12 bits.
  • getTempCByIndex(0) returns the temperature of the first sensor in degrees Celsius, or -127 when the sensor does not answer.
  • The one-second delay sets the update rate, and it is not a requirement of the sensor.

Serial Monitor Output

The Serial Monitor shows a new value every second, with two decimals at the default 12-bit resolution.

Arduino Serial Monitor at 9600 baud showing a DS18B20 temperature reading of 23.44 degrees Celsius every second

DS18B20 Resolution and Conversion Time

The resolution decides how small a step the sensor can report, and it also decides how long a conversion takes. A lower resolution gives a faster reading, and a higher resolution gives finer steps.

ResolutionStep sizeMaximum conversion time
9 bits0.5 °C93.75 ms
10 bits0.25 °C187.5 ms
11 bits0.125 °C375 ms
12 bits (default)0.0625 °C750 ms

At 12 bits, the raw 16-bit value is the temperature multiplied by 16, so the library divides it by 16 to get degrees Celsius. The step size is not the same as the accuracy, because the accuracy is ±0.5 °C from -10 to +85 °C.

The resolution can be changed in setup() with one call. The call sets the resolution for every sensor on the bus, and the sketch should repeat it in setup() after each power-up.

sensors.begin();
sensors.setResolution(10);   // 10 bits: 0.25 °C steps, conversion in up to 187.5 ms

The datasheet lists +85 °C as the power-on reset value of the temperature register. A reading of exactly 85.00 °C right after startup therefore usually means that the sensor returned its reset value instead of a finished conversion, and a new reading gives the real temperature.


DS18B20 Arduino SSD1306 OLED Display

A 0.96 inch SSD1306 OLED shows the temperature without a computer. The display uses the I2C interface on pins A4 and A5, and the DS18B20 uses pin D2, so the two do not share a bus. The basics of the display are covered in the Arduino SSD1306 OLED tutorial.

DS18B20 and SSD1306 Wiring

PartPinArduino pin
DS18B20VDD5V or 3.3V
DS18B20DQD2, with a 4.7 kΩ resistor from DQ to VDD
DS18B20GNDGND
SSD1306 OLEDVCC3.3V
SSD1306 OLEDGNDGND
SSD1306 OLEDSDAA4 (UNO and Nano)
SSD1306 OLEDSCLA5 (UNO and Nano)

The supply voltage of an OLED module depends on the board, so the markings on the module should be checked before it is connected to 5 V. The I2C pins are different on other boards, and the Arduino I2C tutorial lists them together with an address scanner.

DS18B20 and SSD1306 OLED connected to an Arduino Uno with DQ on pin 2, SDA on A4 and SCL on A5

Install the Adafruit SSD1306 and GFX Libraries

  1. Open Sketch, then Include Library, then Manage Libraries.
  2. Search for Adafruit SSD1306 and install it.
  3. Accept the offer to install Adafruit GFX Library, which the SSD1306 library needs.
Arduino IDE Library Manager showing the Adafruit SSD1306 library ready to install
Arduino IDE Library Manager showing the Adafruit GFX Library ready to install

SSD1306 I2C Address and Memory

Most 128×64 modules answer at the address 0x3C, and some use 0x3D. The sketch stops with a message on the Serial Monitor when the display does not answer. The library keeps a copy of the screen in RAM, and for 128×64 pixels this buffer needs 1024 bytes, which is half of the 2048 bytes of RAM on an Arduino UNO. The sketch therefore leaves less room for other data, and begin() fails when the memory is not enough.


DS18B20 OLED Sketch

At text size 2 each character is 12 pixels wide, so one line of the 128-pixel screen holds 10 characters. The sketch therefore prints a small label on the first line and the temperature in large text below it.

#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

const uint8_t ONE_WIRE_BUS = 2;       // DQ pin of the sensor
const uint8_t SCREEN_WIDTH = 128;
const uint8_t SCREEN_HEIGHT = 64;
const uint8_t OLED_ADDRESS = 0x3C;    // Some modules use 0x3D

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);   // -1: no reset pin
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

void setup() {
  Serial.begin(9600);
  sensors.begin();

  // Stop here if the display does not answer or the RAM is not enough
  if (!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDRESS)) {
    Serial.println("SSD1306 not found. Check the wiring and the I2C address.");
    while (true) { }
  }
  display.setTextColor(SSD1306_WHITE);
}

void loop() {
  sensors.requestTemperatures();
  float tempC = sensors.getTempCByIndex(0);

  display.clearDisplay();

  display.setTextSize(1);
  display.setCursor(0, 0);
  display.print("DS18B20 temperature");

  display.setTextSize(2);
  display.setCursor(0, 24);
  if (tempC == DEVICE_DISCONNECTED_C) {   // -127 means the sensor did not answer
    display.print("No sensor");
  } else {
    display.print(tempC, 2);              // Two decimals, for example 23.44
    display.print(" C");
  }

  display.display();                      // Sends the buffer to the screen
  delay(1000);
}

DS18B20 OLED Output

The OLED shows the label in small text and the temperature in large text, and the value updates every second.

SSD1306 OLED showing a DS18B20 reading of 23.44 C in large text below a small DS18B20 temperature label

DS18B20 Multiple Sensors on One Pin

Several DS18B20 sensors can share one data pin, because the Arduino selects each sensor by its 64-bit address. All sensors use the same VDD, DQ and GND lines, and one 4.7 kΩ pull-up serves the whole bus.

Multiple DS18B20 Wiring

Sensor pinArduino pin
VDD of every sensor5V or 3.3V
DQ of every sensorD2, with one 4.7 kΩ resistor from DQ to VDD
GND of every sensorGND
Three DS18B20 sensors connected in parallel to one Arduino pin with shared VDD, DQ and GND lines and a single 4.7 kilohm pull-up resistor
Image showing multiple DS18B20 sensors are connected using same pin to the Arduino.

Find the DS18B20 Addresses

The sketch below lists every sensor on the bus with its 8-byte address. The address is useful because it stays with the sensor, while the index of a sensor can change when sensors are added or removed.

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

const uint8_t ONE_WIRE_BUS = 2;   // Shared DQ pin

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

// Prints the 8 bytes of an address in hexadecimal
void printAddress(const uint8_t *address) {
  for (uint8_t i = 0; i < 8; i++) {
    if (address[i] < 16) Serial.print("0");   // Keeps two digits per byte
    Serial.print(address[i], HEX);
    if (i < 7) Serial.print(" ");
  }
}

void setup() {
  Serial.begin(9600);
  sensors.begin();

  uint8_t count = sensors.getDeviceCount();
  Serial.print("Sensors found: ");
  Serial.println(count);

  for (uint8_t i = 0; i < count; i++) {
    DeviceAddress address;
    Serial.print("Sensor ");
    Serial.print(i);
    Serial.print(": ");
    if (sensors.getAddress(address, i)) {
      printAddress(address);
    } else {
      Serial.print("address not found");
    }
    Serial.println();
  }
}

void loop() { }

Read Multiple DS18B20 Sensors on the OLED

The sketch reads every sensor by its index and prints the values on the Serial Monitor and on the OLED. The 128×64 screen has room for eight lines of small text, so the sketch shows at most eight sensors on the display. The wiring of the OLED is the same as in the previous section.

#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

const uint8_t ONE_WIRE_BUS = 2;       // Shared DQ pin
const uint8_t SCREEN_WIDTH = 128;
const uint8_t SCREEN_HEIGHT = 64;
const uint8_t OLED_ADDRESS = 0x3C;    // Some modules use 0x3D
const uint8_t MAX_LINES = 8;          // 64 pixels / 8 pixels per text line

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

uint8_t deviceCount = 0;

void setup() {
  Serial.begin(9600);
  sensors.begin();
  deviceCount = sensors.getDeviceCount();   // Counted once at startup

  if (!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDRESS)) {
    Serial.println("SSD1306 not found. Check the wiring and the I2C address.");
    while (true) { }
  }
  display.setTextColor(SSD1306_WHITE);
  display.setTextSize(1);

  Serial.print("Sensors found: ");
  Serial.println(deviceCount);
}

void loop() {
  sensors.requestTemperatures();   // One request converts all sensors on the bus

  display.clearDisplay();
  display.setCursor(0, 0);

  for (uint8_t i = 0; i < deviceCount; i++) {
    float tempC = sensors.getTempCByIndex(i);
    bool failed = (tempC == DEVICE_DISCONNECTED_C);   // -127 means no answer

    // Serial Monitor output
    Serial.print("Sensor ");
    Serial.print(i);
    Serial.print(": ");
    if (failed) {
      Serial.println("no answer");
    } else {
      Serial.print(tempC);
      Serial.println(" °C");
    }

    // OLED output, limited to the lines that fit on the screen
    if (i < MAX_LINES) {
      display.print("S");
      display.print(i);
      display.print(": ");
      if (failed) {
        display.println("ERR");
      } else {
        display.print(tempC, 2);
        display.println(" C");
      }
    }
  }

  display.display();
  Serial.println("------------------");
  delay(1000);
}

The conversion time does not grow with the number of sensors, because requestTemperatures() starts all conversions together and then waits once.

SSD1306 OLED listing the temperatures of several DS18B20 sensors on separate lines, labeled S0, S1 and S2

Read a DS18B20 by Address

Reading by address keeps each value tied to the same physical sensor, for example the sensor in a tank and the sensor in the room. The address array is filled with the bytes that the scanner sketch printed.

// Replace the bytes with the address printed by the scanner sketch
DeviceAddress tankSensor = {0x28, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

float tankTemp = sensors.getTempC(tankSensor);   // Returns -127 if this sensor does not answer

DS18B20 Parasite Power Mode

In parasite power mode the sensor has no separate supply wire. The VDD pin is connected to GND, and the sensor charges an internal capacitor from the DQ line while the line is high. The cable then needs only two wires, which helps with long runs and tight spaces.

The mode has a limit that matters for the Arduino. A conversion can draw up to 1.5 mA, which the 4.7 kΩ pull-up cannot supply, so the datasheet requires a strong pull-up, such as a MOSFET, within 10 µs of the convert command and for the whole conversion time. The datasheet also advises against parasite power above 100 °C. For these reasons, the three-wire connection used in this tutorial is the more reliable choice, and parasite power should be tested with the real cable before it is relied on.


DS18B20 Arduino Troubleshooting

The table lists the most common problems with the DS18B20 and the usual fix for each one.

ProblemLikely causeFix
The reading is -127 °CThe sensor does not answer because of a missing pull-up resistor, a loose DQ wire, or a wrong pinConnect the 4.7 kΩ resistor between DQ and VDD, check that DQ goes to D2, and confirm the connections on the breadboard
The reading is exactly 85 °CThe sensor returned its power-on value instead of a finished conversionRead again, and check the power supply if the value stays at 85 °C
The sensor gets hot or the reading is wrong from the startVDD and GND are reversedDisconnect the power at once and check the pinout, because a reversed supply can destroy the sensor
The sketch finds 0 sensorsWrong pin, missing pull-up, or a broken cableCheck the pin number in the sketch and test the cable with a single sensor
One sensor is missing in a multi-sensor setupA loose wire, a long cable, or a damaged sensorTest each sensor alone and check the shared VDD, DQ and GND connections
The order of the sensors changesThe index depends on the order in which the library finds the sensorsRead each sensor by its address with getTempC()
The readings are noisy or jumpElectrical noise, a long cable, or poor groundingUse a twisted pair or a shielded cable, keep the DQ wire away from motor wires, and test a lower pull-up value
The OLED stays blankWrong I2C address, SDA and SCL swapped, or not enough RAMCheck the address (0x3C or 0x3D), confirm A4 and A5, and read the message on the Serial Monitor
The temperature is a few degrees offHeat from the board, the OLED or the hand holding the sensorKeep the sensor away from heat sources and compare it with a reference thermometer

Conclusion

The DS18B20 gives an accurate digital temperature reading with one data pin, one pull-up resistor and the DallasTemperature library. This tutorial covered the pinout, the wiring, the resolution settings, a sketch with error checks for one sensor, an SSD1306 display, several sensors on one pin and an address scanner for reading each sensor by its address.

A reading of -127 °C points to a wiring problem, and a reading of exactly 85 °C right after power-up is the power-on value of the sensor. The readings can be stored with the SD card logging tutorial or time-stamped with the DS3231 RTC tutorial. When humidity is needed as well, the AHT20 tutorial and the SHT30, SHT31 and SHT35 tutorial cover I2C sensors for both values.


Download Project Here

Download the CubeMX project files and HAL source code — all tested on real hardware.

If you find this useful, you can support us with a Donation as well.

Browse More Arduino Sensor Tutorials

About the Author
Arun Rawat
Arun Rawat
Embedded Systems Engineer · Founder, ControllersTech

Arun is an embedded systems engineer with 10+ years of experience in STM32, ESP32, and AVR microcontrollers. He created ControllersTech to share practical tutorials on embedded software, HAL drivers, RTOS, and hardware design — grounded in real industrial automation experience.

Subscribe
Notify of

0 Comments
Newest
Oldest Most Voted
×

Don’t Miss Future STM32 Tutorials

Join thousands of developers getting free guides, code examples, and updates.