MoloWorks account

Welcome back.

Sign in to continue where you left off.

Forgot password?

New to MoloWorks?

EQ-14 · Ultrasonic sensor

Measure distance to a flat target

The four-pin 2020 revision measures distance from the time an ultrasonic pulse takes to return.

HC-SR04 ultrasonic distance sensor with four labeled pins

How to use it

You’ll need: ESP32-S3, the kit’s four-pin wide-voltage HC-SR04, jumpers, a ruler, a flat target, and a suitable logic buffer if the ECHO level is unknown.

Connections

This example uses one part at a time on the MoloWorks ESP32-S3. If a course uses different GPIO pins, follow that course’s wiring and code together. Disconnect USB and any external power before changing wires.

On the partConnect to
Module VCCESP32 3V3
Module GNDESP32 GND
Module TRIG (input)ESP32 GPIO5 (output)
Module ECHO (output)ESP32 GPIO4 (input) through a suitable level interface if needed
  1. Use the four-pin GPIO version supplied in the kit. The 3.3 V setup was tested; the 5 V setup was not. Confirm the ECHO pulse stays within 0–3.3 V with an oscilloscope. If you cannot verify it, use a 5 V-tolerant input buffer powered at 3.3 V, such as an SN74LVC1G17 breakout. Do not connect an unknown or 5 V echo level directly to the ESP32.
  2. Point the sensor squarely at a flat target about 20 cm away. Send a short trigger pulse and measure how long ECHO stays high. The sound travels there and back, so divide the travel distance by two.
  3. Move the target to about 40 cm and compare. The example times out rather than waiting for a missing echo indefinitely. A timeout means no valid measurement.

MicroPython example

Run this on the board with your MicroPython editor. Set up the ESP32-S3 first if you don’t have a working connection.

from machine import Pin, time_pulse_us
from time import sleep_ms, sleep_us

trig = Pin(5, Pin.OUT, value=0)
echo = Pin(4, Pin.IN)
for _ in range(20):
    trig.on()
    sleep_us(10)
    trig.off()
    duration = time_pulse_us(echo, 1, 30000)
    if duration > 0:
        print(round(duration * 0.0343 / 2, 1), "cm")
    else:
        print("no echo")
    sleep_ms(200)

What to look for: The reading increases as the target moves farther away. Compare with a ruler rather than expecting an exact match.

AI prompt

Copy the prompt and replace any brackets with details from your kit. Check the suggested wiring against the labels on your parts before applying power.

Measure distance to a flat target

I have the MoloWorks 2020 four-pin HC-SR04 in GPIO mode on an ESP32-S3 running MicroPython. Use 3.3 V supply, TRIG on GPIO5, and ECHO to GPIO4 only after the pulse level is verified or converted. Write a short trigger and timeout example, show why the travel distance is halved, and give me a 20 cm versus 40 cm bench test. Ask about the level interface if the echo voltage is unknown.

MicroPython pulse timing reference

Contact QR code

Open contact