ESP32 UNO - Temperature Sensor - LCD

Need a small thermometer that works without a computer? Connect a DS18B20 to the ESP32 Uno form board and print the temperature on a 16x2 LCD I2C screen. The top row shows degrees Celsius and the bottom row shows Fahrenheit, so one glance tells you how warm the water or the room is.

By the end, you can:

  1. Connect a DS18B20 probe and a 16x2 I2C screen to the ESP32 Uno
  2. Add three libraries from the Library Manager
  3. Read the sensor with getTempCByIndex() and convert °C to °F
  4. Show both values on the LCD screen
ESP32 Uno temperature sensor LCD

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×LCD I2C Display 3.3V
1×Alternatively, LCD I2C 20x4
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.

Buy Note: Most LCD I2C displays available on the market are 5V displays. They may work with this board, but it is unsafe and can damage the board. We strongly recommend using the 3.3V LCD I2C display recommended above.

The Two Parts in Short

After this part, you know what each part does in the circuit.

Part Job Connection Supply
DS18B20 Measures the temperature One data wire (one-wire bus) 3.0V to 5.5V
LCD I2C 16x2 Shows two lines of 16 characters I2C (SDA and SCL) 5V

The DS18B20 sends a digital number, so the ADC is not used here. The LCD needs only the two I2C lines.

Want the pinout and the basic sketch of each part? Read these guides first:

ESP32 Uno Pinout

Header labels such as D2, SDA and SCL are not the numbers you type in code. Look at the picture to match each label with its GPIO number before you place a wire.

ESP32 Uno pinout diagram

How to Connect

When this step is done, the sensor data wire is on D2 and the screen is on the SDA and SCL header pins.

The wiring diagram between ESP32 Uno Temperature Sensor LCD

This image is created using Fritzing. Click to enlarge image

※ NOTE THAT:

The wiring diagram may not show the correct wiring for the ESP32 Uno. The LCD backpack pulls SDA and SCL up to 5V, but ESP32 Uno pins are 3.3V only. The picture shows the LCD wired straight to SDA and SCL. We suggest a level converter between the LCD and the board, as the hardware list shows. We will update the diagram soon. Until then, follow the wiring table and the text on this page, not the picture.

Component Pin ESP32 Uno Pin
DS18B20 VCC 3.3V
DS18B20 GND GND
DS18B20 DQ (data) D2 (GPIO26)
LCD VCC 5V
LCD GND GND
LCD SDA SDA (GPIO21)
LCD SCL SCL (GPIO22)

Good to know: the adapter version of the DS18B20 has a pull-up resistor on board. A bare sensor needs a 4.7 kΩ resistor between DQ and 3.3V.

WARNING

The signal pins of the ESP32 Uno work at 3.3V and cannot take 5V. Some LCD backpacks tie SDA and SCL to 5V with pull-up resistors. If yours does, use a 5V to 3.3V level converter on SDA and SCL, or remove the pull-up resistors from the backpack.

※ NOTE THAT:

Do not wire the LCD to A4 and A5 here. The LCD belongs on the pins marked SDA and SCL.

Code

The sketch measures every 500 ms, turns the Celsius value into Fahrenheit, and writes both on the LCD.

/* * 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-lcd */ #include <OneWire.h> #include <DallasTemperature.h> #include <DIYables_LCD_I2C.h> #define SENSOR_PIN 26 // D2 (GPIO26) on the ESP32 Uno goes to the DQ pin of the DS18B20 OneWire oneWire(SENSOR_PIN); // create the one-wire bus on the sensor pin DallasTemperature DS18B20(&oneWire); // give the bus to the DallasTemperature library DIYables_LCD_I2C lcd(0x27, 16, 2); // LCD at I2C address 0x27 (DIYables LCD), 16 columns, 2 rows float temperature_C; // latest reading in degrees Celsius float temperature_F; // the same reading in degrees Fahrenheit void setup() { DS18B20.begin(); // start the temperature sensor lcd.init(); // start the LCD (uses SDA GPIO21 and SCL GPIO22) lcd.backlight(); // switch the backlight on } void loop() { DS18B20.requestTemperatures(); // ask the sensor to measure temperature_C = DS18B20.getTempCByIndex(0); // get the value of the first sensor in Celsius temperature_F = temperature_C * 9 / 5 + 32; // calculate Fahrenheit from Celsius lcd.clear(); lcd.setCursor(0, 0); // column 0 of the top row lcd.print(temperature_C); // show Celsius lcd.print((char)223); // draw the degree sign lcd.print("C"); lcd.setCursor(0, 1); // column 0 of the bottom row lcd.print(temperature_F); // show Fahrenheit lcd.print((char)223); // draw the degree sign lcd.print("F"); delay(500); }

※ NOTE THAT:

LCD makers use different I2C addresses. The sketch uses 0x27, the address of the DIYables LCD. If the screen stays blank, scan for the real address and change it in the code.

Upload and Test

Follow these steps in order:

  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 pressing the Libraries button in the side bar of Arduino IDE.
  6. Get DallasTemperature: type DallasTemperature in the search box, choose the one by Miles Burton, then press Install.
  • 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 when the IDE offers the OneWire library.
  2. Get the LCD library: type DIYables LCD I2C, choose DIYables_LCD_I2C from DIYables, then press Install.
  • Search for DIYables LCD I2C created by DIYables.io 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
DIYables LCD I2C by DIYables.io
This library is designed for HD44780-based I2C LCD displays. It provides LiquidCrystal-compatible API plus helper functions (text alignment, progress bars, predefined custom characters). Supports multiple I2C buses (Wire, Wire1, Wire2) for advanced boards like Arduino Giga, Due, and ESP32. Compatible with all Arduino-based platforms including Arduino Uno, Mega, Nano, ESP32, ESP8266, STM32, and Raspberry Pi Pico. More info
1.0.0
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. Send the sketch to the board with the Upload arrow.
Arduino IDE Upload Code
  1. Test it: hold the sensor between your fingers, or dip it in cold or warm water.
  2. Watch the LCD: the numbers rise or fall within a few seconds.
  3. Pro tip: put the sensor in a glass of ice water. A good sensor shows a value close to 0°C.

If the LCD shows nothing, read Troubleshooting for LCD I2C.

On the LCD you will see something like this:

LCD Row Example Text
Row 1 23.81°C
Row 2 74.86°F

Video Tutorial

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

Code Explanation

Each line has a comment, and this table gives the big picture.

Code Part What It Does
SENSOR_PIN 26 Sets D2 (GPIO26) as the one-wire data pin
DS18B20.begin() Starts the sensor on the bus
lcd.init() and lcd.backlight() Starts the screen and turns on the light
requestTemperatures() Tells the sensor to take a new measurement
getTempCByIndex(0) Gets the Celsius value of the first sensor on the bus
(char)223 Prints the degree sign on the LCD

The loop clears the screen, prints both rows, and waits 500 ms.

ESP32 Uno vs UNO R4 for This Project

Moving this thermometer from the UNO R4 to the ESP32 Uno needs only a new pin number in the code.

Item Arduino UNO R4 ESP32 Uno
Logic voltage 5V 3.3V
DS18B20 data pin D2 D2 (GPIO26), so SENSOR_PIN is 26
LCD I2C pins SDA and SCL SDA (GPIO21) and SCL (GPIO22)
Libraries DallasTemperature, OneWire, DIYables_LCD_I2C same libraries
Arduino IDE board Arduino UNO R4 WiFi ESP32 Dev Module

The wiring stays on the same header positions. Power the sensor from 3.3V and check the LCD I2C lines if the backpack pulls them up to 5V.

Go Wireless: Thermometer on Your Phone

Show the same temperature on a phone as well as on the LCD.

The ESP32 Uno has WiFi and Bluetooth. You can send the DS18B20 value to a web page or to a Bluetooth app, so you can read the room temperature from another room. Pick one of these guides to add it:

Troubleshooting

Problem Possible Cause Solution
No COM port in Arduino IDE CH340 driver missing or a charge-only USB cable Install the CH340 driver and use a data USB cable
Failed to connect or Waiting for download Board is not in upload mode Hold the BOOT button while the upload starts
LCD shows nothing No power, or wires on A4 and A5 Power the LCD from 5V and use the SDA (GPIO21) and SCL (GPIO22) header pins
Backlight on but no text Wrong I2C address or low contrast Scan for the address, fix it in the code, and turn the contrast screw
LCD shows -127.00 The sensor is not found Check DQ on D2 (GPIO26), GND, and the 4.7 kΩ pull-up
LCD shows 85.00 Sensor reports its power-on value Check the VCC and GND wires and power the sensor from 3.3V
Compile error for a missing header Library not installed Install DallasTemperature, OneWire and DIYables_LCD_I2C
Values jump around Long wires or a warm board close to the sensor Use shorter wires and keep the sensor away from the board

FAQ

What is the DS18B20 and how accurate is it?

It is a digital one-wire temperature sensor. Its range is -55°C to 125°C, and it is within ±0.5°C from -10°C to 85°C.

Can the DS18B20 run from 3.3V on the ESP32 Uno?

Yes. The supply range is 3.0V up to 5.5V. At 3.3V the data line stays at a level that the ESP32 Uno pins accept.

Why is the LCD on SDA and SCL and not on A4 and A5?

On this board A4 and A5 are only input pins and they are not I2C. The I2C bus sits on its own header pins, SDA (GPIO21) and SCL (GPIO22). Wire.begin() picks them automatically.

Do I need a pull-up resistor for the DS18B20?

The adapter version has one already. A bare sensor needs a 4.7 kΩ resistor between DQ and 3.3V.

Can I use a different pin for the data wire?

Yes. Most I/O pins work for a one-wire bus. Change SENSOR_PIN to a GPIO number, for example 14 for D7. Avoid D8 (GPIO12) and D10 (GPIO5), because they are boot strapping pins.

How do I connect two DS18B20 sensors?

Join all DQ wires on the same pin. Read the first sensor with getTempCByIndex(0) and the second with getTempCByIndex(1), then print each on its own LCD row.

Why does the LCD show 85.00 or -127.00?

85.00 is the power-on value and -127.00 means the library lost the sensor. Both point to a loose wire, weak power or a missing pull-up resistor.

Does this sketch need analogSetAttenuation on the ESP32 Uno?

No. The DS18B20 is digital, so the ADC is not used and no attenuation line is needed.

Can I use an LCD shield from the UNO on the ESP32 Uno?

Not this one. This project uses an I2C LCD with four wires, which fits the board. Shields that expect 5V logic or the R4 pin layout may not work, so check the pins first.

Can I read the temperature on my phone with the ESP32 Uno?

Yes. The ESP32 Uno has WiFi and Bluetooth, so you can send the same value to a web page or a Bluetooth app. See the Go Wireless section for links.

What if I run out of pins on the Uno headers?

The ESP32 Uno has a few extra holes near the power header. GPIO32 and GPIO33 are I/O, PWM and analog on ADC1, so they also work while WiFi is on. GPIO15 is I/O, PWM and analog on ADC2. Solder headers on the holes first. Then type the GPIO number in your sketch. For a second sensor 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, so a wrong connection can stop the board from booting or uploading code. Use GPIO32 and GPIO33 first. Second, never use the six holes under the digital header (SD0 to SD3, CMD and CLK, which are 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!