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.
Table of contents
- DS18B20 Arduino Tutorial: Read Single and Multiple Temperature Sensors on One Pin with OLED Output
- How the DS18B20 Sensor Works
- DS18B20 vs DHT22 and SHT21
- DS18B20 Pinout
- DS18B20 Arduino Wiring
- DS18B20 Arduino Library Setup
- DS18B20 Arduino Serial Monitor Code
- DS18B20 Resolution and Conversion Time
- DS18B20 Arduino SSD1306 OLED Display
- DS18B20 Multiple Sensors on One Pin
- DS18B20 Parasite Power Mode
- DS18B20 Arduino Troubleshooting
- Conclusion
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.
| Feature | DS18B20 |
|---|---|
| Temperature range | -55 to +125 °C |
| Accuracy | ±0.5 °C from -10 to +85 °C, and ±2 °C over the full range |
| Resolution | 9 to 12 bits, 12 bits by default |
| Supply voltage | 3.0 to 5.5 V |
| Interface | One-Wire, one data line plus ground |
| Address | Unique 64-bit code for every sensor |
| Packages | TO-92, and waterproof probes that use the same chip |
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
| Feature | Standard TO-92 or breakout module | Waterproof probe |
|---|---|---|
| Enclosure | Bare package or small PCB | Stainless steel tube |
| Cable | Short pins or header | Fixed cable of about 1 m, depending on the seller |
| Pull-up resistor | Often already on the breakout board | Not included, so it must be added |
| Typical use | Prototypes and room temperature | Liquids, soil, outdoor and pipe measurements |
| Code difference | None | None |
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.
| Feature | DS18B20 | DHT22 |
|---|---|---|
| Measures | Temperature | Temperature and humidity |
| Temperature range | -55 to +125 °C | -40 to +80 °C |
| Temperature accuracy | ±0.5 °C from -10 to +85 °C | Better than ±0.5 °C |
| Interface | One-Wire | Proprietary single wire |
| Several sensors on one pin | Yes, each sensor has a unique address | No, one sensor per pin |
| Waterproof version | Yes | No |
| Conversion or sampling time | Up to 750 ms at 12 bits | About 2 s |
| Supply voltage | 3.0 to 5.5 V | 3.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.
| Pin | Name | Function |
|---|---|---|
| 1 | GND | Ground |
| 2 | DQ | One-Wire data line, which needs a 4.7 kΩ pull-up to VDD |
| 3 | VDD | Power 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 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 pin | Arduino pin |
|---|---|
| VDD | 5V or 3.3V |
| DQ | D2, with a 4.7 kΩ resistor from DQ to VDD |
| GND | GND |
Any digital pin can be used for DQ, and this tutorial uses D2.
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
- Open the Arduino IDE and go to Sketch, then Include Library, then Manage Libraries.
- Search for OneWire and install the library by Paul Stoffregen.
- Search for DallasTemperature and install the library by Miles Burton.
- Accept the offer to install any missing dependencies.
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.
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.
| Resolution | Step size | Maximum conversion time |
|---|---|---|
| 9 bits | 0.5 °C | 93.75 ms |
| 10 bits | 0.25 °C | 187.5 ms |
| 11 bits | 0.125 °C | 375 ms |
| 12 bits (default) | 0.0625 °C | 750 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 msThe 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
| Part | Pin | Arduino pin |
|---|---|---|
| DS18B20 | VDD | 5V or 3.3V |
| DS18B20 | DQ | D2, with a 4.7 kΩ resistor from DQ to VDD |
| DS18B20 | GND | GND |
| SSD1306 OLED | VCC | 3.3V |
| SSD1306 OLED | GND | GND |
| SSD1306 OLED | SDA | A4 (UNO and Nano) |
| SSD1306 OLED | SCL | A5 (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.
Install the Adafruit SSD1306 and GFX Libraries
- Open Sketch, then Include Library, then Manage Libraries.
- Search for Adafruit SSD1306 and install it.
- Accept the offer to install Adafruit GFX Library, which the SSD1306 library needs.
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.
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 pin | Arduino pin |
|---|---|
| VDD of every sensor | 5V or 3.3V |
| DQ of every sensor | D2, with one 4.7 kΩ resistor from DQ to VDD |
| GND of every sensor | GND |
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.
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 answerDS18B20 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.
| Problem | Likely cause | Fix |
|---|---|---|
| The reading is -127 °C | The sensor does not answer because of a missing pull-up resistor, a loose DQ wire, or a wrong pin | Connect 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 °C | The sensor returned its power-on value instead of a finished conversion | Read again, and check the power supply if the value stays at 85 °C |
| The sensor gets hot or the reading is wrong from the start | VDD and GND are reversed | Disconnect the power at once and check the pinout, because a reversed supply can destroy the sensor |
| The sketch finds 0 sensors | Wrong pin, missing pull-up, or a broken cable | Check the pin number in the sketch and test the cable with a single sensor |
| One sensor is missing in a multi-sensor setup | A loose wire, a long cable, or a damaged sensor | Test each sensor alone and check the shared VDD, DQ and GND connections |
| The order of the sensors changes | The index depends on the order in which the library finds the sensors | Read each sensor by its address with getTempC() |
| The readings are noisy or jump | Electrical noise, a long cable, or poor grounding | Use 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 blank | Wrong I2C address, SDA and SCL swapped, or not enough RAM | Check the address (0x3C or 0x3D), confirm A4 and A5, and read the message on the Serial Monitor |
| The temperature is a few degrees off | Heat from the board, the OLED or the hand holding the sensor | Keep 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

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.
Recommended Tools
Essential dev tools
Categories
Browse by platform













