ESP32 - Temperature Sensor

There are many type of temperature sensors can works with ESP32 such as LM35, TH02, HDC1000 or HTS221... In this tutorial, we are going to learn how to use waterproof DS18B20 temperature sensor. This sensor is inexpensive, easy to use and look neat.

Hardware Required

1×ESP-WROOM-32 Dev Module
1×Micro USB Cable
1×Temperature Sensor DS18B20
1×4.7 kΩ resistor
n×Jumper Wires
Please note: These are affiliate links. If you buy the components through these links, We may get a commission at no extra cost to you. We appreciate it.

About One Wire Temperature Sensor - DS18B20


DS18B20 temperature sensor has three pins:

  • GND pin needs to be connected to GND (0V)
  • VCC pin needs to be connected to VCC (5V or 3.3V)
  • DQ pin is 1-Wire Data Bus should be connected to a digital pin on ESP32.

The sensor usually has two forms: TO-92 package (looks like a transistor) and waterproof probe. We use the waterproof probe form in this tutorial.

DS18B20 temperature sensor Pinout

Wiring Diagram

  • With breadboard
  • ESP32 Temperature Sensor Wiring Diagram

    Image is developed using Fritzing. Click to enlarge image

  • Without breadboard
  • ESP32 DS18B20 Wiring Diagram

    Image is developed using Fritzing. Click to enlarge image

How To Program For DS18B20 Temperature Sensor

  • Include the library:
#include <OneWire.h> #include <DallasTemperature.h>
  • Declare OneWire and DallasTemperature object corresponding to the pin connected to sensor's DQ pin
OneWire oneWire(SENSOR_PIN); // setup a oneWire instance DallasTemperature sensors(&oneWire); // pass oneWire to DallasTemperature library
  • Initialize the sensor:
sensors.begin(); // initialize the sensor
  • Send the command to get temperatures:
  • Read temperature in Celsius:
tempCelsius = sensors.getTempCByIndex(0);
  • (Optional) Convert Celsius to Fahrenheit:
tempFahrenheit = tempCelsius * 9 / 5 + 32;

ESP32 Code

/* * Created by * * This example code is in the public domain * * Tutorial page: */ #include <OneWire.h> #include <DallasTemperature.h> const int SENSOR_PIN = 13; // ESP32 pin connected to DS18B20 sensor's DQ pin OneWire oneWire(SENSOR_PIN); // setup a oneWire instance DallasTemperature sensors(&oneWire); // pass oneWire to DallasTemperature library float tempCelsius; // temperature in Celsius float tempFahrenheit; // temperature in Fahrenheit void setup() { Serial.begin(9600); // initialize serial sensors.begin(); // initialize the sensor } void loop() { sensors.requestTemperatures(); // send the command to get temperatures tempCelsius = sensors.getTempCByIndex(0); // read temperature in Celsius tempFahrenheit = tempCelsius * 9 / 5 + 32; // convert Celsius to Fahrenheit Serial.print("Temperature: "); Serial.print(tempCelsius); // print the temperature in Celsius Serial.print("°C"); Serial.print(" ~ "); // separator between Celsius and Fahrenheit Serial.print(tempFahrenheit); // print the temperature in Fahrenheit Serial.println("°F"); delay(500); }

Quick Steps

  • If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE
  • Connect the ESP32 board to your PC via a micro USB cable
  • Open Arduino IDE, select the right ESP32 board and COM port
  • On Arduino IDE, Go to Tools Manage Libraries
  • ESP32 add library
  • Search “OneWire”, then find the OneWire library by Paul Stoffregen
  • Click Install button to install OneWire library.
  • ESP32 onewire library
  • Search “Dallas”, then find the DallasTemperature library by Miles Burton.
  • Click Install button to install DallasTemperature library.
  • ESP32 Dallas Temperature library
  • Copy the above code and paste it to Arduino IDE
  • Click Upload button on Arduino IDE to compile and upload code to ESP32 board
  • Put the sensor on hot and cold water, or grasp the sensor by your hand
  • See the result on Serial Monitor. It looks like the below:.
  • COM6
    Temperature: 26.31°C ~ 79.36°F Temperature: 26.44°C ~ 79.59°F Temperature: 26.50°C ~ 79.70°F Temperature: 26.56°C ~ 79.81°F Temperature: 27.06°C ~ 80.71°F Temperature: 27.75°C ~ 81.95°F Temperature: 28.37°C ~ 83.07°F Temperature: 29.00°C ~ 84.20°F Temperature: 29.56°C ~ 85.21°F Temperature: 30.00°C ~ 86.00°F Temperature: 30.31°C ~ 86.56°F Temperature: 30.62°C ~ 87.12°F Temperature: 30.87°C ~ 87.57°F
    Autoscroll Show timestamp
    Clear output
    9600 baud  

Video Tutorial

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.

Temperature Sensor on Commercial Products


Note that this tutorial is incomplete. We will post on our Facebook Page when the tutorial is complete. Like it to get updated.


  • We are AVAILABLE for HIRE. See how to hire us to do your project
  • If this tutorial is useful for you, please support us to make more tutorials.
  • We spent a lot of time and effort to create the content for this tutorial, please respect our work! Please do not copy the content to share on other websites. Howerver, please feel free to share the link of this tutorial anywhere