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
Project illustration
A visual reference for this build. Use the parts, wiring, and instruction sections below for exact assembly details.
Recommended for
- Middle School
- High School
- Adult Beginners
- Homeschool
Skills
What You'll 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
Required Parts
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
Wiring
Circuit Diagram
TRIG connects to GP3. ECHO reaches GP2 through a 1 kohm and 2 kohm divider so the 5 V echo signal is reduced for the Pico input.
Circuit overview
Pico ultrasonic sensor circuit. Follow the written connection notes and numbered build steps for exact wiring.
Build
Step-by-Step Instructions
Work through each stage in order and disconnect power before changing the wiring.
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.
Programming
Project Code
Upload distance.py after completing the circuit.
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
Troubleshooting
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
FAQ
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.
Go deeper
Related Tutorials and Resources
Project complete
Ready for Password Lock?
Build a keypad-controlled lock that moves a servo when the correct code is entered.
Reuse your kit
More projects you can build with this kit
Keep using the SunFounder Raspberry Pi Pico Ultimate Starter Kit instead of starting with a new parts list.
Blink an LED
Wire and program an LED to blink at a steady interval with a Raspberry Pi Pico.
View projectLED Night Light
Build an automatic LED night light that turns on when the room gets dark.
View projectPassword Lock
Build a keypad-controlled lock that moves a servo when the correct code is entered.
View projectServo Motor Control
Sweep a small hobby servo through precise angles using PWM from a Raspberry Pi Pico.
View projectTraffic Light
Build a timed red, yellow, and green traffic light sequence with three GPIO outputs.
View project