ESP32 UNO - Temperature Sensor

How do you measure temperature with the ESP32 Uno? Connect a DS18B20 probe to the ESP32 Uno form board, and it sends exact readings over one data wire. This DS18B20 guide covers wiring, the DallasTemperature library and Arduino IDE code. The Serial Monitor shows Celsius and Fahrenheit.

By the end, you can:

  1. Wire a DS18B20 temperature sensor to the ESP32 Uno, with or without an adapter.
  2. Install the DallasTemperature and OneWire libraries.
  3. Read the 1-wire sensor value with requestTemperatures().
  4. Convert Celsius to Fahrenheit in code.
ESP32 Uno temperature sensor

Components You Need

1×ESP32 Uno-form Board
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×DS18B20 Temperature Sensor (WITH Adapter)
1×DS18B20 Temperature Sensor (WITHOUT Adapter)
1×Jumper Wires

Or you can buy the following kits:

1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Buy Note: Many DS18B20 sensors available in the market are unreliable. We strongly recommend buying the sensor from the DIYables brand using the link provided above. We tested it, and it worked reliably.

Overview of the DS18B20 One Wire Temperature Sensor

By the end of this part, you know what each sensor pin does.

The DS18B20 is a digital sensor. It sends the temperature as data, so you do not need an analog pin or any ADC math.

Pinout

Sensor Pin Role Connect To
GND Ground GND
VCC Power 3.3V (the sensor also accepts 5V, but use 3.3V here)
DATA 1-Wire data bus one digital pin of the ESP32 Uno

The sensor comes as a small TO-92 part that looks like a transistor, or as a waterproof probe on a cable. This page uses the waterproof probe.

DS18B20 temperature sensor Pinout

Good to know: the DATA line needs a pull-up resistor (about 4.7kΩ) to VCC. Some sellers add a small adapter board with the resistor and a screw terminal. It saves you one part and one mistake.

ESP32 Uno Pinout

Each Uno header label on the ESP32 Uno form board has a GPIO number behind it. Find D4 and the power pins in the picture before you place a wire.

ESP32 Uno pinout diagram

How to Connect

By the end of this part, the DATA wire is on D4 (GPIO17) and the sensor has power.

  • Breadboard wiring diagram
The wiring diagram between ESP32 Uno Temperature Sensor

This image is created using Fritzing. Click to enlarge image

  • Wiring diagram with the wiring adapter
The wiring diagram between ESP32 Uno DS18B20

This image is created using Fritzing. Click to enlarge image

Component Pin ESP32 Uno Pin
VCC 3.3V
GND GND
DATA (DQ) D4 (GPIO17)

Good to know: without an adapter, put a 4.7kΩ resistor between DATA and VCC. With the DS18B20 sensor and wiring adapter, the resistor is already on the board.

DS18B20 Code Basics

By the end of this part, you know the five calls that read the sensor.

Step 1 — Include the libraries

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

Step 2 — Create the bus and sensor objects

Pass the GPIO number of the DATA pin.

OneWire oneWire(SENSOR_PIN); // 1-Wire bus on the sensor pin DallasTemperature DS18B20(&oneWire); // the Dallas library uses that bus

Step 3 — Start the sensor

DS18B20.begin(); // wake up the sensor library

Step 4 — Request and read the temperature

DS18B20.requestTemperatures(); temperature_C = DS18B20.getTempCByIndex(0);

Step 5 — Convert to Fahrenheit (optional)

temperature_F = temperature_C * 9 / 5 + 32;

ESP32 Uno Code

The sketch reads the DS18B20 twice per second and prints both units.

/* * This ESP32 Uno code was developed by newbiely.com * * This ESP32 Uno code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-uno/esp32-uno-temperature-sensor */ #include <OneWire.h> #include <DallasTemperature.h> #define SENSOR_PIN 17 // D4 (GPIO17) connects to the DQ (data) wire of the DS18B20 OneWire oneWire(SENSOR_PIN); // 1-Wire bus on the sensor pin DallasTemperature DS18B20(&oneWire); // the Dallas library talks through that bus void setup() { Serial.begin(9600); // open the Serial Monitor link DS18B20.begin(); // wake up the sensor library } void loop() { DS18B20.requestTemperatures(); // ask the sensor to measure now float temperature_C = DS18B20.getTempCByIndex(0); // get the result in Celsius float temperature_F = temperature_C * 9 / 5 + 32; // Celsius to Fahrenheit Serial.print("Temperature: "); Serial.print(temperature_C); // Celsius value Serial.print("°C"); Serial.print(" ~ "); // divider between the two units Serial.print(temperature_F); // Fahrenheit value Serial.println("°F"); delay(500); }

Upload and Test

  1. First time with this board? Set it up with ESP32 Uno - Getting Started before you continue.
  2. Build the circuit by following the wiring table above.
  3. Plug in the USB Type-C cable between the ESP32 Uno and your computer.
  4. Select the board in Arduino IDE: pick ESP32 Dev Module and the COM port of the board (CH340).
  5. Open the Library Manager with the Libraries icon on the left side of Arduino IDE.
  6. Install DallasTemperature: search for DallasTemperature and install the library by Miles Burton.
  • Search for DallasTemperature created by Miles Burton , Tim Newsome , Guil Barros , Rob Tillaart and click the Install button.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Library Manager
Type:
All
Topic:
All
DallasTemperature by Miles Burton , Tim Newsome , Guil Barros , Rob Tillaart
Supports DS18B20, DS18S20, DS1822, DS1820 More info
3.9.0
INSTALL
Newbiely.ino
···
1 void setup() {
Output
Serial Monitor
Ln 1, Col 1
ESP32 Dev Module on COM15
1
  1. Install the dependency: click Install All so the OneWire library is added too.
  • Search for OneWire created by Jim Studt, Tom Pollard, Robin James, Glenn Trewitt, Jason Dangel, Guillermo Lovato, Paul Stoffregen, Scott Roberts, Bertrik Sikken, Mark Tillotson, Ken Butcher, Roger Clark, Love Nystrom and click the Install button.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Library Manager
Type:
All
Topic:
All
OneWire by Jim Studt, Tom Pollard, Robin James, Glenn Trewitt, Jason Dangel, Guillermo Lovato, Paul Stoffregen, Scott Roberts, Bertrik Sikken, Mark Tillotson, Ken Butcher, Roger Clark, Love Nystrom
More info
2.3.8
INSTALL
Newbiely.ino
···
1 void setup() {
Output
Serial Monitor
Ln 1, Col 1
ESP32 Dev Module on COM15
1
  1. Copy the code into Arduino IDE.
  2. Upload the sketch with the Upload button.
  3. Test it: hold the probe in your hand, or put it into warm and cold water.
  4. Open the Serial Monitor and check the readings.
  5. Pro tip: wait about one second after power-up. The first reading can show -127°C while the sensor wakes up.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32 Dev Module' on 'COM15')
New Line
9600 baud
Temperature: 22.12°C ~ 71.82°F Temperature: 22.19°C ~ 71.94°F Temperature: 23.44°C ~ 74.19°F Temperature: 25.81°C ~ 78.46°F Temperature: 28.69°C ~ 83.64°F Temperature: 31.25°C ~ 88.25°F Temperature: 33.00°C ~ 91.40°F Temperature: 33.56°C ~ 92.41°F
Ln 11, Col 1
ESP32 Dev Module on COM15
2

Video Tutorial

See the ESP32 Uno build in action in the video below.

Code Explanation

Code Part What It Does
SENSOR_PIN 17 Sets D4 (GPIO17) as the 1-Wire data pin
OneWire / DallasTemperature Create the bus and the sensor helper
DS18B20.begin() Starts the sensor library once in setup()
requestTemperatures() Tells the sensor to measure
getTempCByIndex(0) Returns the Celsius value of the first sensor
temperature_C * 9 / 5 + 32 Converts Celsius to Fahrenheit

The loop prints both values and waits 500 ms.

ESP32 Uno vs UNO R4 for This Project

By the end of this part, you know what changes when you move this sensor from the UNO R4 to the ESP32 Uno.

Item Arduino UNO R4 ESP32 Uno
Logic voltage 5V 3.3V
Data pin D4 D4 (GPIO17)
Sensor power 5V or 3.3V 3.3V
Libraries OneWire, DallasTemperature OneWire, DallasTemperature (same)
Arduino IDE board Arduino UNO R4 WiFi ESP32 Dev Module
USB driver none needed CH340 driver

The wiring stays the same. Only the pin number in code changes: the sketch uses 17 instead of 4. Power the sensor from 3.3V so the data line stays at a safe level.

Go Wireless: Temperature on Your Phone

By the end of this part, you have an idea to show the temperature without a cable.

The ESP32 Uno has WiFi and Bluetooth. You can send the DS18B20 value to a web page or to a phone app, so you can watch the room temperature from the couch. Start with one of these pages:

Try These Next

  • Print a warning when the temperature goes above a limit.
  • Turn on a fan or relay when it is too hot.
  • Show the value on an LCD or OLED screen.
  • Connect two DS18B20 sensors on the same pin and read them with getTempCByIndex(1).

Troubleshooting

Problem Possible Cause Solution
No COM port appears CH340 driver missing or charge-only cable Install the CH340 driver and use a USB cable that carries data
Upload fails with "Failed to connect" or "Waiting for download" Board is not in download mode Hold the BOOT button while the upload starts
Reading is -127°C DATA wire loose or no pull-up resistor Check D4 (GPIO17) and add a 4.7kΩ resistor between DATA and 3.3V
Reading is 85°C Sensor power-on default value Check the 3.3V wire and read again after one second
Nothing prints Wrong baud rate in Serial Monitor Set the Serial Monitor to 9600
Compile error OneWire.h not found Library missing Install DallasTemperature and click Install All for OneWire
Value jumps around Long or loose wires Shorten the wires and press the connectors in firmly
Temperature is too high Sensor sits near the board or the hand Move the probe away from warm parts
Wrong pin used Code pin and wire do not match Use GPIO17 in code for the D4 header pin

FAQ

What is the DS18B20 sensor?

It is a digital 1-wire temperature sensor. It sends the temperature as data, so you do not need an analog pin.

What temperature range can the DS18B20 measure?

It measures from -55°C to +125°C. It is accurate to about ±0.5°C between -10°C and +85°C.

Do I need a pull-up resistor?

Yes, a 4.7kΩ resistor between DATA and VCC. A wiring adapter board already has one.

Should I power the DS18B20 from 3.3V or 5V on the ESP32 Uno?

Use 3.3V. The ESP32 Uno pins are not 5V tolerant, and the sensor works fine at 3.3V.

Which library reads the DS18B20?

Use DallasTemperature together with OneWire. Install DallasTemperature and click Install All.

Can I connect more than one DS18B20?

Yes. Put all DATA wires on the same pin and read each sensor with getTempCByIndex(0), getTempCByIndex(1) and so on.

Which pin does the code use on the ESP32 Uno?

The code uses GPIO17, which is the D4 header pin. In code you always write the GPIO number.

Do I need analogSetAttenuation for this sensor?

No. The DS18B20 is digital, so the ADC is not used.

Why does the code differ from the UNO R4 code?

Only the pin number changes, from 4 to 17. The libraries and calls are the same.

Can I show the temperature on a web page with the ESP32 Uno?

Yes. The ESP32 Uno has WiFi and Bluetooth. See the Go Wireless section for links.

What if my project needs more pins than the Uno headers give?

The ESP32 Uno has a few extra holes near the power header: GPIO32 and GPIO33 (I/O, PWM, analog on ADC1, so they also work while WiFi is on) and GPIO15 (I/O, PWM, analog on ADC2). Solder pin headers to use them and write the GPIO number in code. For a second 1-wire bus, GPIO32 or GPIO33 is a good choice. Warning 1: GPIO15 and GPIO0 (the IO0 pin at the unused Uno NC position) are boot strapping pins. A wrong connection can stop the board from booting or uploading code, so use GPIO32/33 first. Warning 2: never use the six holes under the digital header (SD0–SD3, CMD, CLK = GPIO6–GPIO11). They are wired to the flash chip, and using them crashes the board.

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!