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:

ESP32 MicroPython ultrasonic sensor

Hardware Preparation

1×ESP-WROOM-32 Dev Module
1×USB Cable Type-C
1×Ultrasonic Sensor
1×Jumper Wires
1×(Recommended) Screw Terminal Expansion Board 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.

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.
Ultrasonic Sensor Pinout
image source: diyables.io

Wiring Diagram

  • How to connect ESP32 and ultrasonic sensor using breadboard (powered via USB cable)
The wiring diagram between ESP32 MicroPython Ultrasonic Sensor

This image is created using Fritzing. Click to enlarge image

  • How to connect ESP32 and ultrasonic sensor using breadboard (powered via Vin pin)
The wiring diagram between ESP32 MicroPython Ultrasonic Sensor
How to connect ESP32 and ultrasonic sensor
How to wire ESP32 and ultrasonic sensor

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-ultrasonic-sensor """ # example_usage.py from DIYables_MicroPython_Ultrasonic_Sensor import UltrasonicSensor import time TRIG_PIN = 23 # The ESP32 pin GPIO23 connected to TRIG pin of the ultrasonic sensor ECHO_PIN = 22 # The ESP32 pin GPIO22 connected to ECHO pin of the ultrasonic sensor sensor = UltrasonicSensor(trig_pin=TRIG_PIN, echo_pin=ECHO_PIN) sensor.set_detection_threshold(140) # Set detection threshold to 140 cm sensor.enable_filter(num_samples=20) # Enable filtering and set number of samples to 20 while True: sensor.loop() # Perform measurement cycle distance = sensor.get_distance() if not distance: print('No object detected within the set distance') else: # Print the distance formatted to two decimal places using format() print('Distance: {:.2f} cm'.format(distance)) time.sleep(0.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 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.
ESP32 MicroPython 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.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot No object detected within the set distance No object detected within the set distance No object detected within the set distance Distance: 8.90 cm Distance: 6.34 cm Distance: 5.70 cm Distance: 5.83 cm Distance: 4.66 cm Distance: 6.18 cm Distance: 8.00 cm Distance: 13.64 cm Distance: 15.64 cm Distance: 17.21 cm Distance: 18.41 cm No object detected within the set distance No object detected within the set distance
MicroPython (ESP32) • CP2102 USB To UART Bridge Controller @ COM12 ≡

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

Learn More

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