ESP32 MicroPython Temperature Sensor

This guide shows you how to use the DS18B20 1-wire temperature sensor with the ESP32 and MicroPython. We will cover:

ESP32 MicroPython temperature sensor

Hardware Preparation

1×ESP-WROOM-32 Dev Module
1×USB Cable Type-C
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 One Wire Temperature Sensor DS18B20

Pinout

The DS18B20 temperature sensor has three pins.

  • GND pin: Attach it to GND (0 volts).
  • VCC pin: Attach it to VCC (either 5 volts or 3.3 volts).
  • DATA pin: This is used for 1-Wire Data. Connect it to a digital pin on the ESP32.

There are two main types of DS18B20 temperature sensors: the TO-92 package, which looks like a transistor, and the waterproof probe. This guide will focus on using the waterproof probe.

DS18B20 temperature sensor Pinout

Connecting a DS18B20 temperature sensor to an ESP32 typically requires a pull-up resistor, which can be complex to implement. Fortunately, some manufacturers offer wiring adapters that include a pull-up resistor and a screw terminal block, simplifying the setup process.

Wiring Diagram

  • How to connect ESP32 and DS18B20 temperature sensor using breadboard.
The wiring diagram between ESP32 MicroPython Temperature Sensor

This image is created using Fritzing. Click to enlarge image

  • How to connect ESP32 and DS18B20 temperature sensor using breadboard and adapter.
The wiring diagram between ESP32 MicroPython DS18B20

This image is created using Fritzing. Click to enlarge image

How to connect ESP32 and temperature sensor

We suggest getting a DS18B20 sensor with a wiring adapter. This adapter helps you easily connect the sensor as it already has a resistor included, so you do not need another one.

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 """ import machine import onewire import ds18x20 import time ds_pin = machine.Pin(17) # The ESP32 pin GPIO17 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() print('Found DS18B20 devices: ', sensor_addresses) # Main loop to read and print the temperature every second while True: DS18B20.convert_temp() time.sleep_ms(750) for address in sensor_addresses: temperature = DS18B20.read_temp(address) print('Temperature: {:.2f} °C'.format(temperature)) 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.
  • Follow the given diagram to attach the DS18B20 1-wire temperature sensor to the ESP32.
  • 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).
  • 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.
  • Put the sensor in hot and cold water, or hold it in your hand.
  • Check out the message in the Shell at the bottom of Thonny.
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: 24.00 °C Temperature: 24.31 °C Temperature: 25.44 °C Temperature: 26.63 °C Temperature: 27.50 °C Temperature: 28.06 °C Temperature: 28.56 °C Temperature: 29.06 °C Temperature: 29.25 °C
MicroPython (ESP32) • CP2102 USB To UART Bridge Controller @ COM12 ≡

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!