Parking Sensor
Light a warning LED when an ultrasonic target is closer than 30 cm.

Wire it up
Disconnect power before changing connections. Follow terminal names rather than wire colors.
| From | To | Why it matters |
|---|---|---|
| HC-SR04 (sensor1) · VCC | Uno · 5V | Power connection |
| HC-SR04 (sensor1) · GND | Uno · GND.2 | Ground reference |
| HC-SR04 (sensor1) · TRIG | Uno · 7 | Match this terminal to the sketch |
| HC-SR04 (sensor1) · ECHO | Uno · 8 | Match this terminal to the sketch |
| Uno · 9 | Resistor (r-led1) · 1 | Match this terminal to the sketch |
| Resistor (r-led1) · 2 | LED (led1) · A | Match this terminal to the sketch |
| LED (led1) · C | Uno · GND.1 | Ground reference |
The complete sketch
float readDistance() {
digitalWrite(7, LOW); delayMicroseconds(2);
digitalWrite(7, HIGH); delayMicroseconds(10); digitalWrite(7, LOW);
unsigned long us = pulseIn(8, HIGH, 30000);
return us ? us * 0.0343 / 2.0 : -1;
}
void setup() {
Serial.begin(9600); pinMode(7, OUTPUT); pinMode(8, INPUT); pinMode(9, OUTPUT);
}
void loop() {
float cm = readDistance();
digitalWrite(9, cm > 0 && cm < 30);
if (cm < 0) Serial.println("No echo");
else { Serial.print("distance:"); Serial.println(cm); }
delay(100);
}
The launch buttons use this same code and a matching saved circuit.
Build it step by step
Open a complete project
Launch in Simulator creates a separate Uno sketch and its matching wired circuit. Existing sketches are preserved. Press Run to compile and execute.
Explore the live circuit
Set the sensor to 20 cm and then 100 cm. The orange LED should switch on and off while serial output reports distance.
Try it on hardware
Disconnect power while wiring. Match terminal names in the table, select the exact board, Verify, then connect and upload. Check component ratings and power requirements first.
Understand the sketch
A threshold with an invalid state
Only a valid positive reading below 30 cm activates the LED. Missing echoes are reported separately. This is an educational indicator, not a vehicle safety system.
If something is different
Warning is reversed
The comparison uses distance below 30, not above. Check LED polarity and its series resistor.
Expected output and tested scope
The warning LED lights when the modeled target is closer than the sketch threshold (30 cm).
Version 1 · Last source- and build-matched test: 2026-10-25. Actual shipped AVR-GCC WASM compilation and Uno AVR engine execution; no physical hardware test.
Supported tutorial target: Arduino Uno R3 / ATmega328P. Compile: PASS · Modeled execution: PASS. Other boards require a separate review of pins, voltage, libraries and the exact build profile.