ESP32 UNO - Temperature Sensor - OLED

Want a pocket thermometer built with the ESP32 Uno? Connect a DS18B20 sensor and an SSD1306 OLED to the ESP32 Uno form board, and the temperature appears on the screen in big text. No computer is needed once the code is uploaded. You can also read the same value in the Serial Monitor.

By the end, you can:

  1. Wire a DS18B20 temperature sensor and an I2C OLED to the board
  2. Install the SSD1306, DallasTemperature and OneWire libraries
  3. Read degrees Celsius from the sensor with Arduino code
  4. Draw the value in the center of the OLED screen
ESP32 Uno temperature sensor OLED

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×SSD1306 I2C OLED Display 128x64
1×SSD1306 I2C OLED Display 128x32
1×DS18B20 Temperature Sensor (WITH Adapter)
1×DS18B20 Temperature Sensor (WITHOUT Adapter)
1×Breadboard
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.

Buy Note: If you want a bigger OLED display, use the 2.42 inch OLED Display 128x64 .

How the Sensor and the OLED Work Together

By the end of this part, you know what each module does. The DS18B20 measures the temperature. The OLED shows the number.

Part Job Connection
DS18B20 Measures temperature and sends it as digital data One data wire (1-Wire)
SSD1306 OLED Draws text and shapes on a 128x64 screen Two wires (I2C SDA and SCL)

Each part has its own tutorial with the pinout and more code examples:

Good to know: the ESP32 Uno runs at 3.3V logic. Both the DS18B20 and the SSD1306 work fine at 3.3V, so power them from the 3V3 pin.

ESP32 Uno Pinout

Every Uno header pin on the ESP32 Uno form board has a GPIO number, and the code uses that number. Look at the diagram to find D4, SDA and SCL before you place any wire.

ESP32 Uno pinout diagram

How to Connect

By the end of this part, both modules are wired. The sensor data wire goes to D4 (GPIO17). The OLED uses the SDA and SCL header pins.

The wiring diagram between ESP32 Uno DS18B20 Temperature Sensor OLED

This image is created using Fritzing. Click to enlarge image

※ NOTE THAT:

The wiring diagram may not show the correct pin. On the ESP32 Uno, the DS18B20 DATA wire goes to D4 (GPIO17), not D8 as the picture may show. We will update the diagram soon. Until then, follow the wiring table and the code on this page, not the picture.

Component Pin ESP32 Uno Pin
DS18B20 VCC 3.3V
DS18B20 GND GND
DS18B20 DATA D4 (GPIO17)
DS18B20 4.7kΩ pull-up Between DATA and 3.3V
OLED VCC 3.3V
OLED GND GND
OLED SDA SDA (GPIO21)
OLED SCL SCL (GPIO22)

Good to know: pick a DS18B20 sensor with a wiring adapter. The adapter has the pull-up resistor built in. With a bare sensor, add a 4.7kΩ resistor between DATA and 3.3V.

※ NOTE THAT:

Do not wire the OLED to A4 and A5 on this board, they are not I2C. Use the SDA (GPIO21) and SCL (GPIO22) header pins for the OLED.

ESP32 Uno Code - Show DS18B20 Temperature on the OLED

The sketch reads the sensor in a loop, builds a text like 24.37°C, prints it and draws it in the center of the screen.

/* * 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-oled */ #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include <OneWire.h> #include <DallasTemperature.h> #define OLED_WIDTH 128 // screen width in pixels #define OLED_HEIGHT 64 // screen height in pixels #define SENSOR_PIN 17 // D4 (GPIO17) is the DATA pin of the DS18B20 sensor Adafruit_SSD1306 oled(OLED_WIDTH, OLED_HEIGHT, &Wire, -1); // OLED object on the I2C bus (SDA GPIO21, SCL GPIO22) OneWire oneWire(SENSOR_PIN); // 1-Wire bus on the sensor pin DallasTemperature DS18B20(&oneWire); // the Dallas library talks through that bus String temperature_str; void setup() { Serial.begin(9600); // start the OLED at I2C address 0x3C (128x64 screen) if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("SSD1306 allocation failed")); while (true); } delay(2000); // give the screen time to power up oled.clearDisplay(); // start with a blank screen oled.setTextSize(2); // big text oled.setTextColor(WHITE); // white pixels on black oled.setCursor(0, 10); // first cursor position DS18B20.begin(); // start the temperature sensor temperature_str.reserve(10); // reserve space so the String does not fragment memory } void loop() { DS18B20.requestTemperatures(); // ask the sensor for a new measurement float temperature_C = DS18B20.getTempCByIndex(0); // get the value in degrees Celsius temperature_str = String(temperature_C, 2); // keep two digits after the dot temperature_str += char(247) + String("C"); Serial.println(temperature_str); // show the text in the Serial Monitor oled_display_center(temperature_str); } void oled_display_center(String text) { int16_t x1; int16_t y1; uint16_t width; uint16_t height; oled.getTextBounds(text, 0, 0, &x1, &y1, &width, &height); // put the text in the middle, both across and down oled.clearDisplay(); // wipe the old text oled.setCursor((OLED_WIDTH - width) / 2, (OLED_HEIGHT - height) / 2); oled.println(text); // draw the new text oled.display(); }
Code Part What It Does
SENSOR_PIN 17 Sets the 1-Wire data pin to GPIO17, which is header D4
oled.begin(...0x3C) Starts the OLED at I2C address 0x3C
requestTemperatures() Tells the DS18B20 to measure
getTempCByIndex(0) Returns the first sensor value in Celsius
oled_display_center() Measures the text and puts it in the middle of the screen

The result is a reading that refreshes on the OLED every time the loop runs.

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 by clicking the Libraries icon in the left bar.
  6. Search for "SSD1306" and find the library by Adafruit.
  7. Click Install to add it.
  • Search for Adafruit SSD1306 created by Adafruit 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
Adafruit SSD1306 by Adafruit
SSD1306 oled driver library for monochrome 128x64 and 128x32 displays More info
2.5.9
INSTALL
Newbiely.ino
···
1 void setup() {
Output
Serial Monitor
Ln 1, Col 1
ESP32 Dev Module on COM15
1
  1. Accept the dependencies. Click Install All to add Adafruit GFX and the other required libraries.
  • Search for Adafruit GFX Library created by Adafruit 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
Adafruit GFX Library by Adafruit
Install this library in addition to the display library for your hardware. More info
1.12.6
INSTALL
Newbiely.ino
···
1 void setup() {
Output
Serial Monitor
Ln 1, Col 1
ESP32 Dev Module on COM15
1
  1. Search for "DallasTemperature" and find the library by Miles Burton.
  2. Click Install to add it.
  • 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. Accept the dependency. Click Install All to add the OneWire library.
  • 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 sketch from the code box and paste it in a new Arduino IDE window.
  2. Click Upload. If the upload fails, hold the BOOT button while the upload starts.
  3. Change the temperature. Hold the sensor in your hand or dip it in warm or cold water.
  4. Look at the OLED and at the Serial Monitor (9600 baud).
  5. Pro tip: seal the sensor tip in a thin plastic bag before you dip it in water, unless you have the waterproof version.
∞
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
23.87°C 23.94°C 24.50°C 26.12°C 28.69°C
Ln 11, Col 1
ESP32 Dev Module on COM15
2

※ NOTE THAT:

The code finds the middle of the screen on its own. No manual cursor values needed.

Video Tutorial

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

ESP32 Uno vs UNO R4 for This Project

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

Item Arduino UNO R4 ESP32 Uno
Logic voltage 5V 3.3V
Sensor pin in code 8 (D8) 17 (D4 is GPIO17)
OLED I2C pins SDA and SCL header SDA (GPIO21) and SCL (GPIO22) header
Libraries Adafruit SSD1306, DallasTemperature, OneWire same libraries, no change
Wireless WiFi only on the WiFi model WiFi and Bluetooth built in
Arduino IDE board Arduino UNO R4 WiFi ESP32 Dev Module

The wiring is almost the same on both boards. Power the modules from 3.3V on the ESP32 Uno, but move the DATA wire to D4 (GPIO17). D8 (GPIO12) is a boot pin on the ESP32 Uno, so the picture pin does not work.

Go Wireless: Temperature on Your Phone

By the end of this part, you know how to read this sensor without looking at the OLED.

The ESP32 Uno has WiFi and Bluetooth. You can send the DS18B20 value to a web page or to a phone app, then watch it live or draw a graph. The OLED stays as a local display.

Try These Next

  • Show the temperature in Fahrenheit with getTempFByIndex(0)
  • Use the 128x32 OLED and change OLED_HEIGHT to 32
  • Print the highest and lowest temperature on a second line
  • Turn on a fan or a buzzer when the value goes above a limit

Troubleshooting

Problem Possible Cause Solution
No COM port in Arduino IDE CH340 driver missing or charge-only cable Install the CH340 driver and use a USB cable that carries data
Failed to connect or Waiting for download Board not in download mode Hold the BOOT button while the upload starts
OLED stays blank Wrong wires or wrong I2C address Use SDA (GPIO21) and SCL (GPIO22), not A4 and A5, and try address 0x3D
Serial Monitor shows SSD1306 allocation failed OLED not found on the I2C bus Check the power and the two I2C wires, then press RESET
Display shows -127.00°C The sensor is not found on the 1-Wire bus Check DATA on D4, GND, and the 4.7kΩ pull-up to 3.3V
Display shows 85.00°C The sensor did not finish the measurement or has weak power Check the VCC wire and the 3.3V supply
Text is off-center on a 128x32 screen OLED_HEIGHT is still 64 Set OLED_HEIGHT to 32
Compile error about a missing header A library is not installed Install SSD1306, GFX, DallasTemperature and OneWire

FAQ

How does a DS18B20 send the temperature to the ESP32 Uno?

It uses one data wire and the 1-Wire protocol. The sensor sends a digital number, so you do not need an ADC pin. The DallasTemperature library turns the number into degrees.

How accurate is the DS18B20?

It is about plus or minus 0.5°C between -10°C and +85°C. It reads from -55°C to +125°C.

Can I power the DS18B20 from 5V on the ESP32 Uno?

Avoid it. The ESP32 Uno pins are not 5V tolerant, and a 5V pull-up would push 5V into the DATA pin. The sensor works from 3.0V to 5.5V, so use 3.3V.

Do I need a pull-up resistor?

Yes, a 4.7kΩ resistor from DATA to 3.3V. The sensor adapter board already has one. A bare sensor needs it added.

Why is SENSOR_PIN 17 in the code and not 4?

The Uno header label is D4, but the code uses the GPIO number. D4 is GPIO17 on the ESP32 Uno.

Which I2C pins does the OLED use on the ESP32 Uno?

Pins SDA (GPIO21) and SCL (GPIO22) on the header. The default Wire.begin() call picks them. A4 and A5 are analog input pins only.

Do I need an ADC setting for this sensor?

No. The DS18B20 is a digital sensor, so the code never calls analogRead(). The ADC range does not matter here.

Can I use more than one DS18B20?

Yes. All sensors share the same data wire. Use getTempCByIndex(1) for the second one, and so on.

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

Yes. The board has WiFi and Bluetooth. See the Go Wireless section for ready tutorials.

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 are I/O, PWM and analog pins on ADC1, so they also work while WiFi is on. GPIO15 is I/O, PWM and 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. Two warnings. First, GPIO15 and GPIO0 (the IO0 pin at the unused Uno NC position) are boot strapping pins, and a wrong connection can stop the board from booting or uploading code, so use GPIO32 and GPIO33 first. Second, never use the six holes under the digital header (SD0 to SD3, CMD, CLK = GPIO6 to 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!