In this user guide, we will learn how to use a DS18B20 temperature sensor with an ESP32 board using Arduino IDE. This is a comprehensive article in which we will discuss the sensor and how to access sensor readings through both single and multiple sensors. Additionally, we will also display the temperature readings on an SSD1306 OLED display.
This article is divided into these sub-topics:
- Introduction of DS18B20 sensor
- Setting up Arduino IDE
- Displaying temperature reading from single DS18B20 sensor
- Displaying temperature readings from multiple DS18B20 sensors
- Integrating OLED display, ESP32 and DS18B20 sensor
We have a similar guide with Arduino Uno:
DS18B20 Introduction
DS18B20 is a temperature sensor that is single wire programmable in nature. It is widely used to measure the temperature of chemical solutions and substances which are present in a hard environment. One of the advantages of using this sensor is that we only require a single pin of our ESP boards to transfer data. Thus, it is extremely convenient to use with the micro-controller as we can measure multiple temperatures by using the least number of pins on our development board.
The table below shows some key characteristics of the ds18b120 sensor.
Feature | Value |
---|---|
Operating Voltage | 3-5V |
Temperature Range | -55°C to +125°C |
Accuracy | ±0.5°C |
Output Resolution | 9-12 bit |
Pinout Diagram
A waterproof version of this sensor is also available in the market. The following figures show the pinout of the DS18B20 sensors.
The following diagram shows the pinout of the normal DS18B20 temperature sensor.
The table below lists the pin configurations:
Pin | Description |
---|---|
VCC | This is the pin that powers up the sensor. It is 3.3V for ESP boards. |
Data | This pin gives the temperature reading. |
Ground | This pin is connected with the ground |
This temperature sensor also comes in a single package module which contains a sensor and a pull-up resistor. If you are using a module, you do not need to connect an external 4.7K ohm resistor. This is because the module already has an onboard pull-up resistor.
You can have a look at articles related to DS18B20 by accessing the links below:
- MicroPython: DS18B20 Web Server with ESP32/ESP8266(Weather Station)
- DS18B20 Temperature Module interfacing with arduino
Setting up Arduino IDE
We will use Arduino IDE to program our ESP32 development board. Thus, you should have the latest version of Arduino IDE. Additionally, you also need to install the ESP32 plugin.
If your IDE does not have the plugin installed you can visit the link below: Installing ESP32 library in Arduino IDE and upload code.
Installing DS18B20 Libraries
To use the Dallas DS18B20 sensor we will have to install two libraries.
- OneWire library
- DallasTemperature library
Follow the steps below to successfully install them. We will use the Library Manager in our Arduino IDE to install the latest versions of the libraries. Open your Arduino IDE and go to Sketch > Include Libraries > Manage Libraries. Type each library name in the search bar and install them both.
After installation of the libraries, restart your IDE.
Connecting ESP32 board with a single DS18B20 sensor
The connection of DS18B20 with the ESP32 boards is very simple. The DS18B20 sensor can be powered in two different modes.
Normal Mode: The sensor is powered through an external source through the VDD pin and 4.7K ohm pull-up resistor.
Parasite Mode: The sensor obtains power from its own data line. Hence, no external power supply is required.
For this article, we will power the sensor in the normal mode. Thus, we have to connect the VCC terminal with 3.3V, ground with the ground (common ground), and the data pin of the sensor with an appropriate GPIO pin of ESP32 via a pull-up resistor.
Required Components
- ESP32
- DS18B20 sensor
- 4.7k ohm resistor
- Breadboard
- Connecting Wires
ESP32 with DS18B20 Schematic Diagram
As you can see in the schematic diagram below, we have used DS10B20 in normal mode and powered the sensor with its VCC pin from the 3.3V pin of ESP32 board. Connect ESP32 device with DS18B20 as shown in the schematic diagram below:
As you can see above, we have powered the sensor using the normal mode. The DS18B20 sensor has three terminals which we saw above in the pinout. The first terminal is grounded with the ESP32 board. The data line of the sensor, which is the middle terminal, is connected through GPIO14 through a pull-up resistor of 4.7k-ohm. You can choose any other GPIO pin as well. The third terminal is powered by 3.3V from the ESP board.
Arduino Sketch (single DS18B20 sensor)
Open your Arduino IDE and go to File > New. A new file will open. Copy the code given below in that file and save it. This sketch will read the sensor data from the DS18B20 sensor and also print it on the Serial Monitor.
#include <OneWire.h>
#include <DallasTemperature.h>
const int SensorDataPin = 14;
OneWire oneWire(SensorDataPin);
DallasTemperature sensors(&oneWire);
void setup() {
Serial.begin(115200);
sensors.begin();
}
void loop() {
sensors.requestTemperatures();
float temperature_Celsius = sensors.getTempCByIndex(0);
float temperature_Fahrenheit = sensors.getTempFByIndex(0);
Serial.print("Temperature: ");
Serial.print(temperature_Celsius);
Serial.println(" ºC");
Serial.print("Temperature: ");
Serial.print(temperature_Fahrenheit);
Serial.println(" ºF");
Serial.println("");
delay(10000);
}
How the Code Works?
Including Libraries
Firstly, we will include the necessary libraries. For this project, we are using two of them. As we have to connect our ESP32 to the DS18B20 sensor thus we will require the following libraries: OneWire.h and DallasTemperature.h. These were the ones which we previously installed.
#include <OneWire.h>
#include <DallasTemperature.h>
Defining GPIO PIN
Secondly, we will create a variable to store the GPIO pin through which the sensor’s data pin is connected. We have used GPIO14 in this example.
const int SensorDataPin = 14;
Creating Instances
We will require the following instances as well to access the temperature readings. First, we will create a oneWire instance and use the SensorDataPin as an argument inside it. Then we will call the DallasTemperature sensor and pass the oneWire reference which we created above as an argument inside it.
OneWire oneWire(SensorDataPin);
DallasTemperature sensors(&oneWire);
setup() function
Inside the setup() function, we will open a serial connection at a baud rate of 115200. Moreover, we will call sensors.begin() to initialize the DS18B20 sensor as well.
void setup() {
Serial.begin(115200);
sensors.begin();
}
loop() function
In the infinite loop() function, we will obtain the temperature readings from the sensor and display them on OLED as well. Firstly, we will call the requestTemperatures() method. Then we will use the getTempCByIndex() function and getTempFByIndex() function to retrieve the temperature in degree Celsius and Fahrenheit respectively. Notice that we are passing 0 as a parameter inside the functions. This is because we are using a single DS18B20 sensor. When using multiple sensors this value will increment for each additional sensor.
sensors.requestTemperatures();
float temperature_Celsius = sensors.getTempCByIndex(0);
float temperature_Fahrenheit = sensors.getTempFByIndex(0);
Lastly, we will display the temperature readings with appropriate units on the serial monitor after every 10 seconds.
Serial.print(temperature_Celsius);
Serial.println("ºC");
Serial.print(temperature_Fahrenheit);
Serial.println("ºF");
delay(10000);
Demonstration
Choose the correct board and COM port before uploading your code to the board.
Go to Tools > Board and select ESP32 Dev Module.
Next, go to Tools > Port and select the appropriate port through which your board is connected.
Click on the upload button to upload the code into the ESP32 development board. After you have uploaded your code to the ESP32 development board press its ENABLE button.
In your Arduino IDE, open up the serial monitor and you will be able to see the temperature readings. After every 10 seconds, newer readings get displayed.
Obtaining Temperature readings from multiple DS18B20 sensors
Now, we will learn how to get temperature readings from multiple DS18B20 sensors connected with our ESP32 board. As you know this temperature sensor uses one wire protocol for communication purposes. Each sensor comes with a unique 64-bit serial code which makes it easy to connect several DS18B20 sensors on the same one-wire bus. Thus, we will read the temperature readings from multiple DS18B20 sensors by connecting them through a single GPIO.
Required Components
We will need the following components to connect our ESP32 board with the DS18B20 sensors.
- ESP32 development board
- Three DS18B20 sensors
- 4.7k ohm resistor
- Breadboard
- Connecting Wires
ESP32 with multiple DS18B20 sensors Schematic Diagram
As you can see in the schematic diagram below, we have used three DS10B20 sensors in normal mode and powered the sensor with its VCC pin from 3.3V pin of ESP32 board. Connect ESP32 device with DS18B20 as shown in the schematic diagram below:
We will connect all the sensors in parallel. The VCC, ground and data pins will be common. The GND terminal is grounded with the ESP32 board. The data line of the sensors is connected through GPIO14 through a pull-up resistor of 4.7k-ohm. You can choose any other GPIO pin as well. Also, we will require a single resistor for the whole bus. The VCC terminal is powered by 3.3V from the ESP board.
Arduino Sketch (Multiple DS18B20 sensors)
Open your Arduino IDE and go to File > New. A new file will open. Copy the code given below in that file and save it. This sketch will identify the DS18B20 sensors and reads the sensor data from all three and print it on the Serial Monitor.
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 14
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
int total_devices;
DeviceAddress sensor_address;
void setup(){
Serial.begin(115200);
sensors.begin();
total_devices = sensors.getDeviceCount();
Serial.print("Locating devices...");
Serial.print("Found ");
Serial.print(total_devices, DEC);
Serial.println(" devices.");
for(int i=0;i<total_devices; i++){
if(sensors.getAddress(sensor_address, i)){
Serial.print("Found device ");
Serial.print(i, DEC);
Serial.print(" with address: ");
printAddress(sensor_address);
Serial.println();
} else {
Serial.print("Found device at ");
Serial.print(i, DEC);
Serial.print(" but could not detect address. Check circuit connection!");
}
}
}
void loop(){
sensors.requestTemperatures();
for(int i=0;i<total_devices; i++){
if(sensors.getAddress(sensor_address, i)){
Serial.print("Temperature for device: ");
Serial.println(i,DEC);
float temperature_degreeCelsius = sensors.getTempC(sensor_address);
Serial.print("Temp (degree celsius): ");
Serial.println(temperature_degreeCelsius);
}
}
delay(10000);
}
void printAddress(DeviceAddress deviceAddress) {
for (uint8_t i = 0; i < 8; i++){
if (deviceAddress[i] < 16) Serial.print("0");
Serial.print(deviceAddress[i], HEX);
}
}
How the Code Works?
Most of the parts in the code are similar to the ones which we discussed above. I will discuss the code where we are incorporating multiple sensors.
At the start of the sketch, we defined a variable of type int and named it ‘total_devices.’ Inside the setup() function we used sensors.getDeviceCount() to obtain the number of sensors that were successfully initialized. This number gets saved in the variable total_devices.
total_devices = sensors.getDeviceCount();
We will print the number of devices on the Serial Monitor.
Serial.print("Locating devices...");
Serial.print("Found ");
Serial.print(total_devices, DEC);
Serial.println(" devices.");
This section of code uses a for loop to print the addresses for all the sensors attached. The unique address will be accessed through sensors.getAdress() with the sensor_address as the parameter. We will also call the printAddress() function with ‘sensor_address’ as the argument which will print the address of the sensors on the serial monitor.
for(int i=0;i<total_devices; i++){
if(sensors.getAddress(sensor_address, i)){
Serial.print("Found device ");
Serial.print(i, DEC);
Serial.print(" with address: ");
printAddress(sensor_address);
Serial.println();
} else {
Serial.print("Found device at ");
Serial.print(i, DEC);
Serial.print(" but could not detect address. Check circuit connection!");
}
}
loop() function
Inside the loop() function we will obtain the temperature readings from the sensor and display them on the serial monitor. We will use sensors.requestTemperatures() to request the temperature readings.
sensors.requestTemperatures();
Then we will use a ‘for loop’ to display the temperature for each sensor individually.
for(int i=0;i<total_devices; i++){
if(sensors.getAddress(sensor_address, i)){
Serial.print("Temperature for device: ");
Serial.println(i,DEC);
float temperature_degreeCelsius = sensors.getTempC(sensor_address);
Serial.print("Temp (degree celsius): ");
Serial.print(temperature_degreeCelsius);
}
}
printAddress() function
The following function will print the address for each sensor.
void printAddress(DeviceAddress deviceAddress) {
for (uint8_t i = 0; i < 8; i++){
if (deviceAddress[i] < 16) Serial.print("0");
Serial.print(deviceAddress[i], HEX);
}
Demonstration
Choose the correct board and COM port before uploading your code to the board.
Go to Tools > Board and select ESP32 Dev Module.
Next, go to Tools > Port and select the appropriate port through which your board is connected.
Click on the upload button to upload the code into the ESP32 development board. After you have uploaded your code to the ESP32 development board press its ENABLE button.
In your Arduino IDE, open up the serial monitor and you will be able to see the temperature readings for all three sensors. After every 10 seconds, newer readings get displayed.
Displaying DS18B20 Sensor values on OLED Display with Arduino IDE
In this section, we will see how to display Temperature values on a 0.96 SSD1306 OLED display using ESP32 and Arduino IDE.
Installing OLED Libraries in Arduino IDE
To use the OLED display in our project, we have to install the Adafruit SSD 1306 library and Adafruit GFX library in Arduino IDE. Follow the steps below to successfully install them.
Open Arduino IDE and click on Sketch > Library > Manage Libraries. Type ‘SSD1306’ in the search tab and install the Adafruit SSD1306 OLED library.
We will also require the Adafruit GFX library which is a dependency for SSD1306. Type ‘Adafruit GFX’ in the search tab and install it as well.
After installing the libraries, restart your IDE.
You can read this in-depth guide on OLED interfacing with ESP32:
Connection Diagram– OLED with ESP32 and DS18B20
Recommended Reading: OLED Display with ESP32 and ESP8266 in MicroPython
Required Components:
- ESP32 board
- DS18B20 sensor
- 0.96-inch SSD 1306 OLED Display
- 4.7k ohm resistor
- Breadboard
- Connecting Wires
Assemble the circuit as shown in the schematic diagram below:
As you can see above, we have connected all the VCC terminals with a 3.3V power supply. The data pin of the sensor is connected with GPIO14 of the ESP32 board. A 4.7K Resistor is used as a pull-up resistor & is connected between the data pin and VCC. The SCL terminal of the OLED is connected with GPIO22 and the SDA terminal of the OLED is connected with GPIO21. The grounds of all three devices are common.
Arduino Sketch (Displaying Temperature Readings on OLED Display)
Open your Arduino IDE and go to File > New. A new file will open. Copy the code given below in that file and save it. This sketch will read the sensor data from the DS18B20 sensor and display it on the Serial Monitor as well as on the OLED.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <OneWire.h>
#include <DallasTemperature.h>
const int SensorDataPin = 4;
OneWire oneWire(SensorDataPin);
DallasTemperature sensors(&oneWire);
Adafruit_SSD1306 display = Adafruit_SSD1306(128, 64, &Wire, -1);
void setup() {
Serial.begin(115200);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3D)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
delay(2000);
display.clearDisplay();
display.setTextColor(WHITE);
}
void loop() {
delay(5000);
display.setCursor(0,0);
display.setTextSize(1);
display.clearDisplay();
sensors.requestTemperatures();
float temperature_Celsius = sensors.getTempCByIndex(0);
float temperature_F = sensors.getTempFByIndex(0);
Serial.print("Temperature = "); Serial.print(temperature_Celsius); Serial.println(" *C");
//display.print("Temperature: "); display.print(temperature_Celsius); display.println(" *C");
display.setTextSize(1);
display.setCursor(0,0);
display.print("Temperature: ");
display.setTextSize(2);
display.setCursor(0,10);
display.print(temperature_Celsius);
display.print(" ");
display.setTextSize(1);
display.cp437(true);
display.write(167);
display.setTextSize(2);
display.print("C");
Serial.println();
display.display();
delay(2000);
}
How the Code Works?
Including Libraries
Firstly, we will include all the following libraries which are required for this project. Wire.h will allow us to communicate through the I2C protocol. Whereas the other libraries are the ones which we previously installed and are required for the proper functionality of the OLED display and the DS18B20 sensor.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <OneWire.h>
#include <DallasTemperature.h>
Defining DS18B20 sensor parameters
Secondly, we will create a variable to store the GPIO pin through which the sensor’s data pin is connected. We have used GPIO4 in this example.
const int SensorDataPin = 4;
We will require the following instances as well to access the temperature readings. First, we will create a oneWire instance and use the SensorDataPin as an argument inside it. Then we will call the DallasTemperature sensor and pass the oneWire reference which we created above as an argument inside it.
OneWire oneWire(SensorDataPin);
DallasTemperature sensors(&oneWire);
Defining OLED Display
We will initialize the display by creating an object and specifying the width, height, I2C instance (&Wire), and -1 as parameters inside Adafruit_SSD1306. -1 specifies that the OLED display which we are using does not have a RESET pin. If you are using the RESET pin then specify the GPIO through which you are connecting it with your development board.
Adafruit_SSD1306 display = Adafruit_SSD1306(128, 64, &Wire, -1);
setup() function
Inside the setup() function, we will open a serial connection at a baud rate of 115200. Moreover, we will also initialize the OLED display. Make sure you specify the correct address of your display. In our case, it is 0X3D.
Serial.begin(115200);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3D)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
The following lines of code will clear the display and set the text color to white.
display.clearDisplay();
display.setTextColor(WHITE);
loop() function
Inside the loop() function, we will display the temperature readings on the OLED display and the Serial Monitor.
We will use the setCursor() function to denote the x and the y axis position from where the text should start. We have passed (0,0) as the parameter hence the text starts from the upper left corner. THe clearDisplay() function will wipe off the previous readings from the display after each loop() so that new ones can be printed.
display.setCursor(0,0);
display.setTextSize(1);
display.clearDisplay();
sensors.requestTemperatures();
float temperature_Celsius = sensors.getTempCByIndex(0);
Serial.print("Temperature = ");
Serial.print(temperature_Celsius);
Serial.println(" *C");
display.print("Temperature: ");
display.print(temperature_Celsius);
display.println(" *C");
Serial.println();
display.display();
Demonstration
To see the demonstration of the above code, upload the code to Arduino. Before uploading the code, make sure to select the ESP32 board from Tools > Board and also select the correct COM port to which the ESP32 board is connected from Tools > Port.
Once the code is uploaded to ESP32, open the serial monitor of Arduino IDE and set the baud rate to 115200. Finally, we can see the BME680 readings on the Arduino serial monitor and on the OLED display as shown below:
Conclusion
In conclusion, we learned how to interface single and multiple DS18B20 sensors with the ESP32 development board. We used it to display temperature readings on an OLED display as well as on the Serial Monitor of Arduino IDE. Moreover, obtaining temperature readings from a single sensor as well as multiple sensors was also included.
You may also like to read other sensors guides with ESP32:
Parabéns excelente tutorial.
Thanks
Hello, what should the sketch look like if you have 3 x DS18B20 with a small OLED 0.69 (loop info one after the other) … regards Rafael
Thank you so much for this information.
Hello, I want to connect 12 ds18b20 sensors, following your tutorial it works ok, but it is very slow, can I config two GPIO from ESP32 to work with two different sets of sensors?
DS18B20センサー、OELD SSD1306、ESP32を使用してWiFiにつないで、測定したデータをスマホに送るプログラムを作成出来ないでしょうか。