Build a Parking Sensor with an HC-SR04 and an LED
Build a parking sensor from an Arduino Uno, an HC-SR04 and an LED. As something comes closer, the LED blinks faster, and once it is closer than 10 cm the LED stays on.
This builds on measuring distance with an HC-SR04, the same sensor on the same pins. If you haven't wired an LED before, see the LED guide.
Try it first, no parts needed
This is the whole project, running in your browser. Press Run simulation, then move the target with the slider, or hold the button to bring your hand close, and watch the LED speed up.
What the HC-SR04 sees
The sketch takes a reading every 100 ms.
The warning LED
Its glow, enlarged.
Lights up when the simulation runs.
Arduino Uno serial monitor115200 baud
What the code does
The sketch measures the distance ten times a second and, in between, keeps the LED blinking at the speed that distance calls for. It never waits with delay(), so the blinking and the readings run together.
// Parking sensor: the LED blinks faster as something gets
// closer, stays on under 10 cm, and is off beyond 100 cm.
const int TRIG_PIN = 10;
const int ECHO_PIN = 9;
const int LED_PIN = 6;
const long STOP_CM = 10; // closer than this: LED on
const long WARN_CM = 100; // farther than this: LED off
long distanceCm = -2; // -1: no echo; -2: not read yet
unsigned long nextReading = 0;
unsigned long lastFlip = 0;
bool ledOn = false;
// One reading in centimetres, or -1 when no echo returns.
long readDistanceCm() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH); // a 10 us pulse starts it
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
unsigned long echoUs = pulseIn(ECHO_PIN, HIGH, 30000);
if (echoUs == 0) {
return -1;
}
return echoUs / 58;
}
void setup() {
Serial.begin(115200);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
}
void loop() {
unsigned long now = millis();
if (now >= nextReading) {
nextReading = now + 100; // a reading every 100 ms
long cm = readDistanceCm();
if (cm != distanceCm) {
distanceCm = cm;
if (cm < 0) {
Serial.println("No echo: LED off");
} else if (cm > WARN_CM) {
Serial.print(cm);
Serial.println(" cm: LED off");
} else if (cm < STOP_CM) {
Serial.print(cm);
Serial.println(" cm: LED on");
} else {
Serial.print(cm);
Serial.print(" cm: on/off every ");
Serial.print(cm * 5);
Serial.println(" ms");
}
}
}
if (distanceCm < 0 || distanceCm > WARN_CM) {
ledOn = false;
} else if (distanceCm < STOP_CM) {
ledOn = true;
} else if (now - lastFlip >= (unsigned long)(distanceCm * 5)) {
lastFlip = now; // closer: shorter blinks
ledOn = !ledOn;
}
digitalWrite(LED_PIN, ledOn);
}const long STOP_CM = 10; // closer than this: LED onCloser than
STOP_CMthe LED stays on; farther thanWARN_CMit stays off.return echoUs / 58;One reading, as on the HC-SR04 guide but in whole centimetres (
/ 58drops the fraction), or −1 when no echo comes back.nextReading = now + 100; // a reading every 100 msTakes a reading every 100 ms, checking the clock with
millis()instead of waiting.if (cm != distanceCm) {Prints only when the distance changes, so the Serial Monitor isn't flooded.
} else if (now - lastFlip >= (unsigned long)(distanceCm * 5)) { lastFlip = now; // closer: shorter blinks ledOn = !ledOn; }Flips the LED each time the distance × 5 ms has passed, so a nearer target blinks it faster. See how it works.
How it works
The LED follows the last distance read, in three zones. At 50 cm each on and each off lasts 250 ms; at 20 cm, 100 ms; under 10 cm the LED stays on, and beyond 100 cm it stays off.
So at 50 cm the LED blinks twice a second, and at 20 cm five times. Because the sketch checks millis() rather than waiting, a new reading every 100 ms changes the blink speed straight away.
Build it
Now with real parts. Wire it, get the code, then upload it and bring your hand in.
You'll need
- An Arduino Uno and its USB cable
- An HC-SR04
- A red LED, 5 mm or 3 mm, and a 220 Ω resistor (red, red, brown bands)
- A breadboard and jumper wires
- Software to upload the sketch to the Uno: either Cirkit Designer, free in your browser, or the Arduino IDE, a free download for Windows, macOS and Linux
Wire it
The HC-SR04 as on the HC-SR04 guide, then the LED from pin 6 to GND with its resistor.
Tap a connection to see it on the circuit.
Pin Connect to the Uno Show HC-SR04 VCC 5V Show HC-SR04 TRIG Pin 10 Show HC-SR04 ECHO Pin 9 Show HC-SR04 GND GND Show LED anode (long leg) Pin 6 Show LED cathode (short leg) 220 Ω resistor, then GND Show The resistor can go on either side of the LED, as long as the two are in series between pin 6 and GND.
Get the code
I'm using:Steps 2 and 3 follow the tool you pick.
The project opens with the sketch already in it, so there's nothing to install.
No Arduino IDE yet? Download it free for Windows, macOS or Linux.
The sketch needs no library. Copy it into a new sketch:
How it works line by line is in what the code does, above.
Upload and check it
Plug in your Uno. In Cirkit, open the Build tab, then Upload firmware: click Select Port and pick your Uno, then Upload to Device. The first time, Cirkit asks you to install Cirkit Create Agent, a small desktop helper that lets it reach the board over USB. Then open Upload output and serial monitor and click Start Serial Monitor.
Show me where to click in Cirkit
Select a device port
Select the device port for uploading code and serial communications
COM3 (Arduino Uno)- Open Build
- Choose Upload firmware
- Click Select Port
- Pick COM3 (Arduino Uno)
- Click Upload to Device
- Open Upload output and serial monitor
- Click Start Serial Monitor
In the Arduino IDE, choose Arduino Uno and its port from the board menu, press Upload, then open the Serial Monitor and set it to 115200 baud.
Show me where to click in the Arduino IDE
parking_sensor.ino// Parking sensor: the LED blinks faster as something gets // closer, stays on under 10 cm, and is off beyond 100 cm. const int TRIG_PIN = 10; const int ECHO_PIN = 9; const int LED_PIN = 6; const long STOP_CM = 10; // closer than this: LED on
OutputSerial Monitor115200 baudCompiling sketch...
Sketch uses 2940 bytes (9%) of program storage space. Maximum is 32256 bytes.
Global variables use 265 bytes (12%) of dynamic memory, leaving 1783 bytes for local variables. Maximum is 2048 bytes.
Uploading...
99 cm: on/off every 495 ms
49 cm: on/off every 245 ms
24 cm: on/off every 120 ms
5 cm: LED on
Done uploading.
- Click the board menu
- Pick Arduino Uno on COM3
- Click Upload
- Wait for Done uploading
- Click Serial Monitor
- Check it is set to 115200 baud
It works when:
- With nothing within a metre, the LED is off.
- As you bring your hand in, the LED starts blinking and blinks faster the closer you get.
- Closer than 10 cm, the LED stays on.
- The Serial Monitor prints a line each time the distance changes, such as
49 cm: on/off every 245 ms: the LED switches on or off every 245 ms.
Device Serial99 cm: on/off every 495 ms49 cm: on/off every 245 ms24 cm: on/off every 120 ms5 cm: LED onOutputSerial Monitor99 cm: on/off every 495 ms49 cm: on/off every 245 ms24 cm: on/off every 120 ms5 cm: LED onNot working? See troubleshooting.
Troubleshooting
Find your symptom; the checks under it run from the most common cause down.
The Serial Monitor shows distances, but the LED never lights
- The LED is the right way round. Its long leg goes to pin 6, its short leg to the resistor.
- The target is within a metre. Beyond 100 cm the LED is off by design.
The LED guide's troubleshooting has more checks.
It prints "No echo: LED off" and nothing after
- The HC-SR04 is wired as on the HC-SR04 guide. See its "Out of range" checks.
Nothing in the Serial Monitor
- The Serial Monitor is set to 115200 baud. It must match
Serial.begin(115200). - The sketch prints only when the distance changes. It prints once at the start; move something in front of the sensor to see more.
Make it yours
- Move the zones. Change
STOP_CMandWARN_CMto suit your garage or robot. - Blink faster or slower. Change the 5 in
distanceCm * 5: 3 blinks faster at every distance, 10 slower. - Show the distance too. Add a small OLED screen and draw the distance on it; the OLED guide shows how to wire and drive one.
FAQ
Why doesn't the sketch use delay() to blink?
A delay() in the blink would hold up the next reading until the blink finished. Checking millis() instead lets each pass through loop() update the LED and take a reading when one is due.
Can I use an ESP32 instead?
Yes, with ECHO through a voltage divider; the HC-SR04 ESP32 guide shows the wiring. Put the LED on any output pin and change LED_PIN, TRIG_PIN and ECHO_PIN to match.
Simulating the parking sensor in Cirkit Designer
Cirkit's HC-SR04 answers as in the HC-SR04 guide, with an exact echo, and its LED lights while pin 6 is HIGH. Readings come out a little short, so a target at 50 cm prints 49 cm; check your own board against a tape measure.

