Projects with an ultrasonic sensor
29 Mokxi projects use an HC-SR04 ultrasonic sensor (6 beginner and 23 intermediate). They run on Arduino Uno, Raspberry Pi Pico and ESP32 boards. Here are 24, from “Grabbing candy bowl” to “Reject station”. Each card says what the project does, the board and parts it needs and its level, and Open in the simulator runs it in your browser in one click, with no account.
How to pick your first one
- Start with “Ultrasonic distance on pins 12 and 11”, a beginner project that needs an ultrasonic sensor.
- Arduino Uno runs 20 of them, Raspberry Pi Pico 5 and ESP32 2. Start on the board you have, or on an Arduino Uno if you are buying one.
- The parts used most here are the servo motor (7 projects), the piezo speaker (6) and the buzzer (5). With those on hand, most of this list is open to you.
- Before you build one on a real bench, read the HC-SR04 ultrasonic sensor wiring guide for your board below. It gives every pin and the voltage notes.
24 projects of 29

Grabbing candy bowl
An HC-SR04 watches across the top of a candy bowl: when a hand reaches in, a piezo cackles and a servo swings a rubber hand down to grab it.
- Board
- Arduino Uno R3
- Parts
- HC-SR04 ultrasonic sensor, servo motor, piezo speaker
- Level
- Beginner

Ultrasonic theremin
Ultrasonic theremin on an Arduino Nano: wave a hand over an HC-SR04 to play notes of a pentatonic scale, or glide the pitch like a real theremin.
- Board
- Arduino Nano
- Parts
- HC-SR04 ultrasonic sensor, arcade button, piezo speaker
- Level
- Beginner

HC-SR04 on a Pico with MicroPython
MicroPython on a Raspberry Pi Pico: machine.time_pulse_us() times an HC-SR04’s echo, divided down to 3.3 V, and prints the distance twice a second.
- Board
- Raspberry Pi Pico
- Parts
- HC-SR04 ultrasonic sensor, resistor
- Level
- Beginner

Ultrasonic distance on pins 12 and 11
An HC-SR04 with TRIG on pin 12 and ECHO on pin 11: the SR04 library times the echo and prints the distance in centimeters once a second.
- Board
- Arduino Uno R3
- Parts
- HC-SR04 ultrasonic sensor
- Level
- Beginner

Smart dustbin
Smart dustbin on an Arduino: an ultrasonic sensor sees a hand and a servo opens the lid.
- Board
- Arduino Uno R3
- Parts
- HC-SR04 ultrasonic sensor, piezo speaker, traffic light module, servo motor
- Level
- Beginner

Ultrasonic sensor on 8 and 7, the LED on 13 when out of range
An HC-SR04 with TRIG on pin 8 and ECHO on 7 prints the distance in centimeters; when nothing is between 0 and 200 cm the LED on pin 13 lights.
- Board
- Arduino Uno R3
- Parts
- HC-SR04 ultrasonic sensor
- Level
- Beginner

MicroPython HC-SR04 distance, timed by hand
MicroPython on an ESP32: a pulse on TRIG (GPIO 13) and the ECHO on GPIO 14 timed with ticks_us() give the distance in centimeters.
- Board
- ESP32-DevKitC V4
- Parts
- HC-SR04 ultrasonic sensor, resistor
- Level
- Intermediate

HC-SR04 distance sensor with gpiozero
gpiozero DistanceSensor on a Raspberry Pi 5: an HC-SR04 with its echo divided down to 3.3 V.
- Board
- Raspberry Pi 5
- Parts
- HC-SR04 ultrasonic sensor, resistor, LED
- Level
- Intermediate

Ultrasonic ranging on an LCD
Hold the button on A0 and an HC-SR04 (TRIG on pin 9, ECHO on 3) measures the distance in front of it, shown on a 16x2 LCD wired to pins 2, A3, 8, 7.
- Board
- Arduino Uno R3
- Parts
- 16x2 character LCD, HC-SR04 ultrasonic sensor, push button
- Level
- Intermediate

Pico W reversing aid: HC-SR04 and a beeper
MicroPython on a Pico W: an HC-SR04 on GP17 and GP16 measures the gap, and an active buzzer and an LED beep faster as an obstacle gets closer.
- Board
- Raspberry Pi Pico W
- Parts
- HC-SR04 ultrasonic sensor, resistor, buzzer, transistor, LED
- Level
- Intermediate

MicroPython HC-SR04 on a Pico, timed by hand
MicroPython on a Raspberry Pi Pico: an HC-SR04 with TRIG on GP19 and ECHO on GP18, its echo timed with ticks_us, prints centimeters twice a second.
- Board
- Raspberry Pi Pico
- Parts
- HC-SR04 ultrasonic sensor, resistor
- Level
- Intermediate

Smart trash can: ultrasonic on 8 and 7, servo lid on 9
An HC-SR04 with TRIG on pin 8 and ECHO on 7 watches for a hand; closer than 20 cm and the servo on pin 9 swings the lid open.
- Board
- Arduino Uno R3
- Parts
- HC-SR04 ultrasonic sensor, servo motor
- Level
- Intermediate

Radar sweep: ultrasonic on 10 and 11, servo on 12
An HC-SR04 (TRIG 10, ECHO 11) rides a servo on pin 12 that sweeps from 15 to 165 degrees and back.
- Board
- Arduino Uno R3
- Parts
- HC-SR04 ultrasonic sensor, servo motor
- Level
- Intermediate

Obstacle-avoiding car
A 2WD robot car on an L298N with an HC-SR04 on the front. It drives ahead, and when something comes within 25 cm it backs up, turns and carries on.
- Board
- Arduino Uno R3
- Parts
- L298N motor driver, 2WD robot car chassis, HC-SR04 ultrasonic sensor
- Level
- Intermediate

Pico W HC-SR04 distance on GP17 and GP16
MicroPython on a Pico W: an HC-SR04 with TRIG on GP17 and ECHO on GP16 times its echo with ticks_us() and prints the distance in centimeters.
- Board
- Raspberry Pi Pico W
- Parts
- HC-SR04 ultrasonic sensor, resistor
- Level
- Intermediate

Reversing radar: ultrasonic, buzzer and I2C LCD
An HC-SR04 (TRIG 8, ECHO 7) measures the gap behind the car; an active buzzer on pin 9 beeps faster as it closes.
- Board
- Arduino Uno R3
- Parts
- HC-SR04 ultrasonic sensor, buzzer, I2C LCD
- Level
- Intermediate

MicroPython HC-SR04 distance with a driver file
MicroPython on an ESP32: a small sr04.py driver times the HC-SR04 echo with time_pulse_us() and prints the distance twice a second.
- Board
- ESP32-DevKitC V4
- Parts
- HC-SR04 ultrasonic sensor, resistor
- Level
- Intermediate

Automatic soap dispenser: ultrasonic on 8 and 7, pump on 2
An HC-SR04 (TRIG 8, ECHO 7) sees a hand under the spout and runs a mini water pump for a moment through an L293D whose 1A input is on pin 2.
- Board
- Arduino Uno R3
- Parts
- L293D motor driver, mini water pump, HC-SR04 ultrasonic sensor
- Level
- Intermediate

MicroPython HC-SR04 on a Pico with a driver module
MicroPython on a Raspberry Pi Pico: an HC-SR04 on GP19 and GP18, read through a small hcsr04.py driver, prints the distance twice a second.
- Board
- Raspberry Pi Pico
- Parts
- HC-SR04 ultrasonic sensor, resistor
- Level
- Intermediate

Sonar depth and fish finder
An HC-SR04 as sonar: the echo time becomes depth at the speed of sound in water.
- Board
- Arduino Uno R3
- Parts
- HC-SR04 ultrasonic sensor, SSD1306 OLED display, piezo speaker
- Level
- Intermediate

Parking sensor
An ultrasonic ranger and a buzzer that beeps faster as the wall gets closer. Drag the slider to move the wall.
- Board
- Arduino Uno R3
- Parts
- HC-SR04 ultrasonic sensor, buzzer, resistor, LED
- Level
- Intermediate

Scanning obstacle rover
A robot with an ultrasonic sensor on a servo neck. Blocked, it stops, looks right, ahead and left, picks the clearest way and turns that way.
- Board
- Arduino Uno R3
- Parts
- TB6612FNG motor driver, 2WD robot car chassis, servo motor, HC-SR04 ultrasonic sensor
- Level
- Intermediate

Sumo robot
A mini-sumo robot with an ultrasonic eye and two edge sensors.
- Board
- Arduino Uno R3
- Parts
- DRV8833 motor driver, 2WD robot car chassis, HC-SR04 ultrasonic sensor, TCRT5000 line sensor
- Level
- Intermediate

Reject station
A quality check that tracks each part: an HC-SR04 measures the part's height at the gate.
- Board
- Arduino Uno R3
- Parts
- Servo motor, traffic light module, two-channel relay board, DC motor, alarm siren, +3 more
- Level
- Intermediate
Wiring, libraries and more ideas
Wiring guides
Library pages
More project ideas
- Arduino Uno project ideas
- Raspberry Pi Pico project ideas
- ESP32 project ideas
- Arduino Nano project ideas
- Servo motor projects
- Projects with a piezo speaker
- Projects with a buzzer
- Projects with a robot car chassis
- Projects with an I2C LCD
- LED projects for Arduino, ESP32 and Pico
- Projects with a traffic light module