ESP32 MicroPython Temperature Sensor OLED

This tutorial instructs you how to use a ESP32 and MicroPython code to read the temperature from a DS18B20 temperature sensor and show it on an OLED I2C display.

ESP32 MicroPython Temperature Sensor OLED I2C

Hardware Preparation

1×ESP-WROOM-32 Dev Module
1×USB Cable Type-C
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×Jumper Wires
1×Breadboard
1×(Recommended) Screw Terminal Expansion Board for ESP32
1×(Recommended) Power Splitter For ESP32

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
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 Temperature Sensor and OLED

If you're new to DS18B20 temperature sensor, OLED I2C, or MicroPython programming for the ESP32, I recommend checking out these tutorials:

These tutorials will provide you with a comprehensive understanding of DS18B20 temperature sensor and OLED, how to connect these components to the ESP32, and how to effectively control their behavior using MicroPython code.

Wiring Diagram

  • How to connect ESP32 with temperature sensor and oled using breadboard
The wiring diagram between ESP32 MicroPython Temperature Sensor OLED

This image is created using Fritzing. Click to enlarge image

How to connect ESP32 with temperature sensor and oled

This image is created using Fritzing. Click to enlarge image

ESP32 MicroPython Code

""" This ESP32 MicroPython code was developed by newbiely.com This ESP32 MicroPython code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/esp32-micropython/esp32-micropython-temperature-sensor-oled """ import machine import onewire import ds18x20 import time from machine import I2C, Pin from DIYables_MicroPython_OLED import OLED_SSD1306_I2C ds_pin = machine.Pin(23) # The ESP32 pin GPIO23 connected to the DS18B20 sensor # Create the onewire object OneWire = onewire.OneWire(ds_pin) # Create the DS18X20 object DS18B20 = ds18x20.DS18X20(OneWire) # Scan for devices on the bus sensor_addresses = OneWire.scan() if not sensor_addresses: raise Exception("No DS18B20 sensor found!") sensor_address = sensor_addresses[0] print('Found DS18B20 sensor with address: ', sensor_address) # Initialize I2C i2c = I2C(0, scl=Pin(22), sda=Pin(21), freq=400000) # Adjust ESP32 pins according to your setup # Initialize the OLED display oled = OLED_SSD1306_I2C(128, 64, i2c) # Clear the display oled.clear_display() oled.set_text_size(3) oled.display() def oled_display_center(oled, text): # Get the text bounds (width and height) of the string oled.clear_display() x1, y1, width, height = oled.get_text_bounds(text, 0, 0) # Set cursor to the calculated centered position cursor_x = (oled.WIDTH - width) // 2 cursor_y = (oled.HEIGHT - height) // 2 oled.set_cursor(cursor_x, cursor_y) # Print the text on the display oled.println(text) # Refresh the display to show the text oled.display() # Main loop to read and print the temperature every second while True: DS18B20.convert_temp() time.sleep_ms(750) temperature = DS18B20.read_temp(sensor_address) print('Temperature: {:.2f} °C'.format(temperature)) oled_display_center(oled, "{:.2f}".format(temperature) + OLED_SSD1306_I2C.DEGREE_SYMBOL + "C") time.sleep(1)

Detailed Instructions

Here’s instructions on how to set up and run your MicroPython code on the ESP32 using Thonny IDE:

  • Make sure Thonny IDE is installed on your computer.
  • Confirm that MicroPython firmware is loaded on your ESP32 board.
  • If this is your first time using an ESP32 with MicroPython, check out the ESP32 MicroPython Getting Started guide for step-by-step instructions.
  • Connect the ESP32 board to the DS18B20 temperature sensor and OLED I2C according to the provided diagram.
  • Connect the ESP32 board to your computer with a USB cable.
  • Open Thonny IDE on your computer.
  • In Thonny IDE, go to Tools Options.
  • Under the Interpreter tab, choose MicroPython (ESP32) from the dropdown menu.
  • Make sure the correct port is selected. Thonny IDE usually detects it automatically, but you might need to select it manually (like COM12 on Windows or /dev/ttyACM0 on Linux).
  • Navigate to the Tools Manage packages on the Thonny IDE.
  • Search “DIYables-MicroPython-OLED”, then find the OLED library created by DIYables.
  • Click on DIYables-MicroPython-OLED, then click Install button to install OLED library.
ESP32 MicroPython OLED library
  • Copy the provided MicroPython code and paste it into Thonny's editor.
  • Save the code to your ESP32 by:
    • Clicking the Save button or pressing Ctrl+S.
    • In the save dialog, choose MicroPython device.
    • Name the file main.py.
  • Click the green Run button (or press F5) to execute the script.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot MPY: soft reboot Found DS18B20 sensor with address: bytearray(b'(\xffP\x05.\x04\x00\x07') Temperature: 26.07 °C Temperature: 26.11 °C Temperature: 26.11 °C Temperature: 26.24 °C Temperature: 27.44 °C Temperature: 29.10 °C Temperature: 29.86 °C Temperature: 30.24 °C Temperature: 30.39 °C
MicroPython (ESP32) • CP2102 USB To UART Bridge Controller @ COM12 ≡
  • Put the sensor on hot or cold water, or hold it in your hand.
  • Look at the display on the OLED screen. It looks like the below image:
ESP32 displays temperature on OLED

Code Explanation

Look at the comments in the source code for explanations of each line!

Video Tutorial

※ 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!