ESP32 MicroPython Ultrasonic Sensor
This tutorial instructs you how to measure the distance to an object using an ultrasonic sensor and an ESP32 with MicroPython. In detail, we will learn:
- How to connect an ultrasonic sensor with ESP32
- How to write MicroPython code for ESP32 to measure distance using an ultrasonic sensor
Hardware Preparation
1 | × | ESP-WROOM-32 Dev Module | |
1 | × | USB Cable Type-C | |
1 | × | Ultrasonic Sensor | |
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) |
Additionally, some of these links are for products from our own brand, DIYables.
Overview of Ultrasonic Sensor
The HC-SR04 ultrasonic sensor measures the distance to objects using ultrasonic waves. It sends out a ultrasonic wave that humans can't hear and listens for the echo that returns after hitting an object. By measuring the time it takes for the ultrasonic wave to come back, the sensor determines the distance from it to the object.
Pinout
The HC-SR04 ultrasonic sensor includes four pins.
- VCC pin: Attach this pin to VCC (5 volts).
- GND pin: Attach this pin to GND (0 volts).
- TRIG pin: Link this to the ESP32 to send control signals.
- ECHO pin: This sends signals back to the ESP32, which detects these signals to determine distance.
Wiring Diagram
- How to connect ESP32 and ultrasonic sensor using breadboard (powered via USB cable)
This image is created using Fritzing. Click to enlarge image
- How to connect ESP32 and ultrasonic sensor using breadboard (powered via Vin pin)
- How to connect ESP32 and ultrasonic sensor using screw terminal block breakout board (powered via USB cable)
- How to connect ESP32 and ultrasonic sensor using screw terminal block breakout board (powered via Vin pin)
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.
- Connect the ESP32 board to the ultrasonic sensor as shown in the above 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).
- In Thonny IDE, Navigate to the Tools Manage packages on the Thonny IDE.
- Search “DIYables-MicroPython-Ultrasonic-Sensor”, then find the Ultrasonic Sensor library created by DIYables.
- Click on DIYables-MicroPython-Ultrasonic-Sensor, then click Install button to install Ultrasonic Sensor 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.
- Wave your hand in front of the ultrasonic sensor.
- Check out the message in the Shell at the bottom of Thonny IDE to see the output.
Code Explanation
You can find the explanation in the comments of the ESP32 MicroPython code mentioned above.
Video Tutorial
Ultrasonic Sensor Applications
- Avoiding Accidents
- Ensuring Capacity
- Measuring Elevation
- Detecting Proximity