🌡 Connecting multiple DS18B20 sensors to ESP32

🌡 Connecting multiple DS18B20 sensors to ESP32

Daniil Zhuk

This guide describes how to read data from multiple DS18B20 temperature sensors using an ESP32 chip using the Arduino IDE.

We will show how to connect several sensors to one ESP32 data bus, install the necessary libraries and give a simple example that you can use in your projects. This instruction is also suitable for boards of the ESP8266 family and various Arduino platforms.

DS18B20 - digital temperature sensor with the ability to connect one wire. Each sensor is assigned a unique 64-bit serial number, which allows the use of multiple sensors on one data bus (that is, multiple sensors connected to one general-purpose I / O line).

This is especially useful for projects with data logging and temperature control. The DS18B20 is an excellent sensor because it is inexpensive, accurate and very easy to use.

Readings from multiple temperature sensors DS18B20 via ESP32 chip
The pinout is from left to right: the first pin is GND grounding, the second is data, the third rightmost is VCC power.

Note: There is also a waterproof version of the DS18B20 temperature sensor.

The main characteristics of the temperature sensor DS18B20:

Communication is supported by a single wire bus.
Operating temperature range: -55 ... + 125 ° C
Accuracy: ± 0.5 ° C (at temperatures from -10 to +85 ° C)
To connect the DS18B20 temperature sensor, you need to connect a 4.7 kΩ resistor between the VCC power supply and the data line. The following diagram shows an example of three sensors (if necessary, you can add more of them).

In the diagram above, the sensor is turned with the rounded side towards us. The flat side looks in the opposite direction.

For the Arduino IDE, there is an add-on that allows you to program the ESP32 chip through this medium and its programming language.

Soon there will be a separate material on how to prepare the Arduino IDE to work with ESP32.

Before downloading the code, you need to install two libraries on the Arduino IDE. The Paul Schtoffregen OneWire Library and Dallas Temperature Library. Follow these steps to install these libraries.

OneWire Library

  1. OneWire library downloads here. After downloading, an archive file with the .zip extension should appear in your downloads folder (for example, in the Downloads folder).
  2. Unzip the file with the .zip extension — the OneWire-master folder should appear.
  3. Change the name of the OneWire-master folder to OneWire.
  4. Move the OneWire folder to the folder with the Arduino IDE libraries installed.
  5. Restart the Arduino IDE to load the library.

Dallas Temperature Library

  1. The DallasTemperature library is downloaded here. An archive file with the .zip extension should appear in your download folder (for example, in the Downloads folder).
  2. Unzip the file with the .zip extension - the Arduino-Temperature-Control-Library-master folder should appear.
  3. Change the folder name Arduino-Temperature-Control-Library-master to DallasTemperature
  4. Move the DallasTemperature folder to the folder with the installed libraries of the Arduino IDE
  5. Restart the Arduino IDE.

Getting the DS18B20 Sensor Address

Each DS18B20 temperature sensor is still assigned a serial number at the factory. First of all, you need to know this number in order to mark the corresponding sensor with it. This is necessary so that later you can understand from which sensor the temperature readings were obtained.

Download the following code to the ESP32 chip. Be sure to choose the right board and serial COM port.

#include "OneWire.h"

// Based on the OneWire library example

OneWire ds(15); //data wire connected to GPIO15

void setup(void) {

void loop(void) {
byte i;
byte addr[8];

if (!ds.search(addr)) {
Serial.println(" No more addresses.");
Serial.print(" ROM =");
for (i = 0; i < 8; i++) {
Serial.write(' ');
Serial.print(addr[i], HEX);

Connect only one sensor at a time to find out the address of each (or add a new sensor in sequence) so that you can relate the sensor and its address to each other. Now you can label directly on each sensor.

Open the window of the serial COM port (Serial Monitor) and set the baud rate to 115200 baud. You should get something like this (only with different addresses):

Uncheck the Autoscroll option so that you can copy the addresses. In our case, we received the following addresses:

  • Sensor 1: 28 FF 77 62 40 17 4 31
  • Sensor 2: 28 FF B4 6 33 17 3 4B
  • Sensor 3: 28 FF A0 11 33 17 3 96

It is very easy to get temperature readings from several sensors using one data bus.

The following program code example reads the temperature readings in degrees Celsius and Fahrenheit from each sensor and displays the result in the window of the serial COM port (Serial Monitor).

// Include the libraries we need
#include "OneWire.h"
#include "DallasTemperature.h"

// Data wire is connected to GPIO15
#define ONE_WIRE_BUS 15
// Setup a oneWire instance to communicate with a OneWire device
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);

DeviceAddress sensor1 = { 0x28, 0xFF, 0x77, 0x62, 0x40, 0x17, 0x4, 0x31 };
DeviceAddress sensor2 = { 0x28, 0xFF, 0xB4, 0x6, 0x33, 0x17, 0x3, 0x4B };
DeviceAddress sensor3= { 0x28, 0xFF, 0xA0, 0x11, 0x33, 0x17, 0x3, 0x96 };

void setup(void){

void loop(void){
Serial.print("Requesting temperatures...");
sensors.requestTemperatures(); // Send the command to get temperatures

Serial.print("Sensor 1(*C): ");
Serial.print(" Sensor 1(*F): ");

Serial.print("Sensor 2(*C): ");
Serial.print(" Sensor 2(*F): ");

Serial.print("Sensor 3(*C): ");
Serial.print(" Sensor 3(*F): ");


Open the window of the serial COM port (Serial Monitor) and set the baud rate to 115,200 baud. You should see something similar.

First of all, the necessary libraries are included:

#include "OneWire.h"
#include "DallasTemperature.h"

The necessary library copies for the temperature sensor are created. The temperature sensor is connected to the GPIO 15 general-purpose I / O line.

#define ONE_WIRE_BUS 15
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

Run the DallasTemperature library for the DS18B20 sensor.


Next, enter the addresses we learned earlier for each temperature sensor. In our case, the following happened:

DeviceAddress sensor1 = { 0×28, 0xFF, 0×77, 0×62, 0×40, 0×17, 0×4, 0×31 };
DeviceAddress sensor2 = { 0×28, 0xFF, 0xB4, 0×6, 0×33, 0×17, 0×3, 0×4B };
DeviceAddress sensor3 = { 0×28, 0xFF, 0xA0, 0×11, 0×33, 0×17, 0×3, 0×96 };

The setup () function initializes the serial link and runs the DallasTemperature library for the DS18B20 sensor.

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

The loop () function requests temperature readings in both Celsius and Fahrenheit degrees and displays the readings in the serial COM port window (Serial Monitor).
First of all, you need to request temperature readings using the following line of code:


Further, you can request a temperature reading using the sensor addresses:

  • sensors.getTempC (SENSOR_ADDRESS) - a request to read the temperature in degrees Celsius;
  • sensors.getTempF (SENSOR_ADDRESS) - request for the temperature reading in Fahrenheit.

For example, to request a temperature reading in degrees Celsius from sensor 1, use the following:



Where sensor1 is a variable containing the address of the first sensor.

This code is just a simple sketch, recognized to show how to get temperature readings from several DS18B20 sensors using the ESP32 debug board. This software code is also suitable for ESP8266 and various Arduino platforms.

You can not connect something or get errors? Write in the comments, we will try to help you! Successful compilations and interesting projects!

Add a comment

* Comments must be approved before being displayed.