Arduino Ultrasonic Sensor HC-SR04 and PIR Motion Sensor
Level 6 · Level 6 — Distance and Motion · 35 minutes
A reverse parking alarm: beeps for distance, but a person nearby always wins.

Preview the project free; a course seat is required to run and submit. No hardware kit needed.
Your project challenge
Build a parking alarm. Closer wall, faster beeps. Wire it up: - HC-SR04: VCC to 5V, GND to GND, TRIG to D6, ECHO to D7 - PIR: VCC to 5V, GND to GND, signal to D4 - Buzzer: signal to D8, other to GND - Heartbeat LED: anode to D13, cathode to GND A person always wins: PIR HIGH, buzzer solid. Then compile.
Components you’ll use
- Arduino Uno
- HC-SR04 Ultrasonic Distance Sensor
- PIR Motion Sensor
- Piezo Buzzer
- LED (Light-Emitting Diode)
What you'll build
A reverse parking alarm. It beeps faster as a wall nears. A person behind the car always wins.
How it works
- The HC-SR04 fires ultrasonic sound from
D6and times the echo onD7. Same trick bats use to fly in total darkness. - Nearer wall, shorter gap between beeps on the buzzer at
D8. - The PIR on
D4watches the infrared heat a moving body gives off. HIGH means someone is there, so the buzzer goes solid instead. - The heartbeat LED on
D13flips every 100 ms, proof the loop keeps running. Nothing stalls it longer thanpulseIn, which times an 8.7 ms echo off the 150 cm wall.
The key idea
The echo is a round trip, so halve it. Sound covers 0.0343 cm per microsecond. A 20 cm wall echoes for about 1166 of them.
distanceCm = duration * 0.0343 / 2
⚠ Watch out
At 3 seconds a person steps behind the car. The buzzer must stay solid. Leave the PIR's VCC off 5V and it drives nothing. The buzzer keeps beeping. The 3.5 second check finds it off. Red X.
The interactive editor includes the wiring guide, code editor and simulation controls.