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:
- How to connect the DS18B20 temperature sensor to a ESP32.
- How to connect the DS18B20 temperature sensor to a ESP32 using an adapter.
- How to write a MicroPython code for the ESP32 to read the temperature from the DS18B20 sensor.
Hardware Preparation
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables.
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.
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.
This image is created using Fritzing. Click to enlarge image
- How to connect ESP32 and DS18B20 temperature sensor using breadboard and adapter.
This image is created using Fritzing. Click to enlarge image
- How to connect ESP32 and temperature sensor using screw terminal block breakout board and adapter.
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
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.