IoT/ESP32 B.Tech/Diploma Beginner

ESP32 DS18B20 Temperature Sensor with Arduino IDE (Single, Multiple, Web Server)

DS18B20 OneWire Temperature sensor with ESP32 using Arduino IDE. Learn how to read temperature from one or multiple sensors and build a web server to display sensor readings.

  • Suitable forB.Tech/Diploma IoT/ESP32 students
  • Build time1-2 weeks
  • LevelBeginner
  • Available atAjvika Labs, Kakinada

About this project

This is a in-depth guide for the DS18B20 temperature sensor with ESP32 using Arduino IDE. We’ll show you how to wire the sensor, install the required libraries, and write the code to get the sensor readings from one and multiple sensors. Finally, we’ll build a simple web server to display the sensor readings.
You might also like reading other DS18B20 guides:
ESP8266 DS18B20 Temperature Sensor with Arduino IDE
ESP32/ESP8266 DS18B20 Temperature Sensor with MicroPython
ESP32 with Multiple DS18B20 Temperature Sensors
DS18B20 Temperature Sensor with Arduino
Introducing DS18B20 Temperature Sensor
The
DS18B20 temperature sensor
is a one-wire digital temperature sensor. This means that it just requires one data line (and GND) to communicate with your ESP32.
It can be powered by an external power supply or it can derive power from the data line (called “parasite mode”), which eliminates the need for an external power supply.
Each DS18B20 temperature sensor has a unique 64-bit serial code. This allows you to wire multiple sensors to the same data wire. So, you can get temperature from multiple sensors using just one GPIO.
The DS18B20 temperature sensor is also available in
waterproof version
Here’s a summary of the most relevant specs of the DS18B20 temperature sensor:
Communicates over one-wire bus communication
Power supply range: 3.0V to 5.5V
Operating temperature range: -55ºC to +125ºC
Accuracy +/-0.5 ºC (between the range -10ºC to 85ºC)
For more information consult the
DS18B20 datasheet
Parts Required
To follow this tutorial you need the following parts:
ESP32
(read
Best ESP32 development boards
)
DS18B20 temperature sensor
(one or multiple sensors) –
waterproof version
4.7k Ohm resistor
Jumper wires
Breadboard
You can use the preceding links or go directly to
MakerAdvisor.com/tools
to find all the parts for your projects at the best price!
Schematic – ESP32
As mentioned previously, the DS18B20 temperature sensor can be powered through the VDD pin (
normal mode
), or it can derive its power from the data line (
parasite mode
). You can choose either mode.
If you’re using an ESP32 follow one of these two schematic diagrams.
Parasite Mode
Normal Mode
Preparing Your Arduino IDE
We’ll program the ESP32 using Arduino IDE, so make sure you have the ESP32 add-on installed before proceeding:
Install ESP32 Board in Arduino IDE (Windows, Mac OS X, and Linux instructions)
Installing Libraries
To interface with the DS18B20 temperature sensor, you need to install the
One Wire library by Paul Stoffregen
and the
Dallas Temperature library
. Follow the next steps to install those libraries.
1. Open your Arduino IDE and go to
Sketch
>
Include Library
>
Manage Libraries
. The Library Manager should open.
2. Type “
onewire
” in the search box and install OneWire library by Paul Stoffregen.
3. Then, search for “
Dallas
” and install DallasTemperature library by Miles Burton.
After installing the libraries, restart your Arduino IDE.
Code (Single DS18B20)
After installing the required libraries, you can upload the code to the ESP32. The following code reads temperature from the DS18B20 temperature sensor and displays the readings on the Arduino IDE Serial Monitor.

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

// GPIO where the DS18B20 is connected to
const int oneWireBus = 4;

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(oneWireBus);

// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);

void setup() {
// Start the Serial Monitor
Serial.begin(115200);
// Start the DS18B20 sensor
sensors.begin();
}

void loop() {
sensors.requestTemperatures();
float temperatureC = sensors.getTempCByIndex(0);
float temperatureF = sensors.getTempFByIndex(0);
Serial.print(temperatureC);
Serial.println("ºC");
Serial.print(temperatureF);
Serial.println("ºF");
delay(5000);
}
There are many different ways to get the temperature from DS18B20 temperature sensors. However, if you’re using just one single sensor, this is one of the easiest and simplest ways.
How the Code Works
Start by including the
OneWire
and the
DallasTemperature
libraries.
#include <OneWire.h>
#include <DallasTemperature.h>
Create the instances needed for the temperature sensor. The temperature sensor is connected to
GPIO 4
// GPIO where the DS18B20 is connected to
const int oneWireBus = 4;

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(oneWireBus);

// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);
In the
setup()
, initialize the Serial Monitor at a baud rate of 115200.
Serial.begin(115200);
Initialize the DS18B20 temperature sensor:
sensors.begin();
Before actually getting the temperature, you need to call the
requestTemperatures()
method.
sensors.requestTemperatures();
Then, get the temperature in Celsius by using the
getTempCByIndex()
method as shown below:
float temperatureC = sensors.getTempCByIndex(0);
Or use the
getTempFByIndex()
to get the temperature in Fahrenheit.
float temperatureF = sensors.getTempFByIndex(0);
The
getTempCByIndex()
and the
getTempFByIndex()
methods accept the index of the temperature sensor. Because we’re using just one sensor its index is 0. If you want to read more than one sensor, you use index 0 for one sensor, index 1 for other sensor and so on.
Finally, print the results in the Serial Monitor.
Serial.print(temperatureC);
Serial.println("ºC");
Serial.print(temperatureF);
Serial.println("ºF");
New temperature readings are requested every 5 seconds.
delay(5000);
Demonstration
After uploading the code, you should get your sensor readings displayed in the Serial Monitor:
Getting Temperature from Multiple DS18B20 Temperature Sensors
The DS18B20 temperature sensor communicates using one-wire protocol and each sensor has a unique 64-bit serial code, so you can read the temperature from multiple sensors using just one single GPIO. You just need to wire all data lines together as shown in the following schematic diagram:
Code (Multiple DS18B20s)
Then, upload the following code. It scans for all devices on
GPIO 4
and prints the temperature for each one. (This sketch is based on an example provided by the DallasTemperature library).

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

// Data wire is plugged TO GPIO 4
#define ONE_WIRE_BUS 4

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

// Number of temperature devices found
int numberOfDevices;

// We'll use this variable to store a found device address
DeviceAddress tempDeviceAddress;

void setup(){
// start serial port
Serial.begin(115200);

// Start up the library
sensors.begin();

// Grab a count of devices on the wire
numberOfDevices = sensors.getDeviceCount();

// locate devices on the bus
Serial.print("Locating devices...");
Serial.print("Found ");
Serial.print(numberOfDevices, DEC);
Serial.println(" devices.");

// Loop through each device, print out address
for(int i=0;i<numberOfDevices; i++){
// Search the wire for address
if(sensors.getAddress(tempDeviceAddress, i)){
Serial.print("Found device ");
Serial.print(i, DEC);
Serial.print(" with address: ");
printAddress(tempDeviceAddress);
Serial.println();
} else {
Serial.print("Found ghost device at ");
Serial.print(i, DEC);
Serial.print(" but could not detect address. Check power and cabling");
}
}
}

void loop(){
sensors

Technologies used

ESP32ESP8266ArduinoBLEDS18B20PythonMicroPythonArduino IDE

Hardware components

  • ESP32
  • ESP8266
  • Arduino
  • DS18B20

Software & tools

  • Arduino IDE
  • MicroPython

What you get

  • Working project / hardware kit, tested before delivery
  • Complete source code with explanation
  • Project demo and guidance for your viva
  • Project report, PPT and IEEE paper support (on request)
Delivery & Payment
Delivery takes a minimum of two weeks from the date your inquiry is confirmed. Payment is in two parts: • 50% advance to start the project • Remaining 50% before delivery Note: • Documentation and PPT are not included by default • If you need documentation/PPT, it can be arranged for extra time and cost We'll confirm the exact timeline and cost with you after reviewing your requirements.