EngineeringMaker Lab

Build guide

Featured project

Ultrasonic Distance Sensor

This project measures the travel time of an ultrasonic pulse and converts it to distance. It introduces digital timing, sensor protocols, 5 V-to-3.3 V level protection, and filtering unstable readings.

Last updated

Build Time
50-75 minutes
Estimated Cost
$12-$20
Skill Level
Intermediate
Raspberry Pi PicoRaspberry Pi PicoUltrasonicSensorsTiming

Project overview

What You Will Build

1. Power the sensor

Connect HC-SR04 VCC to 5 V, GND to Pico GND, and TRIG to GP3. Keep the system unpowered while building the echo divider.

2. Protect the echo input

Connect ECHO through 1 kohm to a junction connected to GP2. Connect 2 kohm from that junction to ground. Verify the divider before powering the circuit.

3. Run the measurement code

Point the sensor toward a flat object 10 to 50 cm away and run the program. Stable centimeter readings should appear four times per second.

4. Test the useful range

Move the target closer and farther away. Note where readings become unstable so your future project can reject unreliable values.

Skills

What You Will Learn

  • Trigger and time a digital sensor pulse
  • Convert pulse duration into distance
  • Protect a 3.3 V input with a voltage divider
  • Diagnose noisy or out-of-range measurements

Preparation

Parts Used

Gather these components before starting the build.

  • Qty: 1Raspberry Pi PicoMicroPython installed
  • Qty: 1HC-SR04 ultrasonic module5 V module
  • Qty: 11 kohm resistorEcho voltage divider
  • Qty: 12 kohm resistorEcho voltage divider
  • Qty: 1BreadboardHalf-size or larger
  • Qty: 6Jumper wiresMale-to-male

Verified resource

Complete Official Build Guide

Ultrasonic Sensor Module with Raspberry Pi Pico

Already have the recommended equipment? The verified SunFounder lesson provides the exact wiring, schematic, code, and step-by-step build instructions.

This verified Universal Maker Sensor Kit lesson provides the exact ultrasonic sensor wiring, schematic, code, and step-by-step build instructions.

Original preparation

Original Code and Preparation Notes

This independently written example summarizes the programming concept used by the project. Compare wiring and pin choices with the verified official guide, when available, before running distance.py.

distance.pypython
from machine import Pin, time_pulse_us
from time import sleep_us, sleep

trigger = Pin(3, Pin.OUT)
echo = Pin(2, Pin.IN)

def distance_cm():
    trigger.low()
    sleep_us(2)
    trigger.high()
    sleep_us(10)
    trigger.low()
    duration = time_pulse_us(echo, 1, 30000)
    return duration / 58.0

while True:
    try:
        print("{:.1f} cm".format(distance_cm()))
    except OSError:
        print("Out of range")
    sleep(0.25)

Problem solving

Common Beginner Mistakes

Every reading times out

Confirm TRIG and ECHO are not swapped

Readings jump randomly

Aim at a broad flat target

The Pico input may have received 5 V

Disconnect power and verify the echo voltage divider before continuing; never connect HC-SR04 ECHO directly to a Pico GPIO.

Common questions

Frequently Asked Questions

Why divide the echo voltage?

The HC-SR04 echo can be 5 V while Raspberry Pi Pico GPIO inputs are designed for 3.3 V logic.

What objects are difficult to detect?

Soft

Can I show the result on a display?

Yes. An I2C OLED or LCD is a useful next step after serial readings are reliable.

Project complete

Ready for Password Lock?

Build a keypad-controlled lock that moves a servo when the correct code is entered.