A parking sensor on Arduino Uno
Steps done: 0 of 5
Guess the answer and press Run
to the car, cm60time, s0.0
Lesson steps
What is lit at 25 cm?
The car is 25 cm from the sensor. Read the sketch and pick an answer.
Sketch
// Parking sensor: HC-SR04 (TRIG on A1, ECHO on A2), traffic light on D12, D11, D10, buzzer on D8
const int trig = A1;
const int echo = A2;
const int green = 12;
const int yellow = 11;
const int red = 10;
const int buzzer = 8;
int farCm = 40; // farther than this the green LED is on, cm
int nearCm = 15; // closer than this the red LED is on and the buzzer is continuous, cm
int msPerCm = 8; // pause between beeps per centimeter, ms
int beepHz = 2000; // beep frequency, Hz
void setup() {
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
pinMode(green, OUTPUT);
pinMode(yellow, OUTPUT);
pinMode(red, OUTPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
int cm = pulseIn(echo, HIGH, 30000) / 58;
Serial.println(cm);
digitalWrite(green, cm >= farCm);
digitalWrite(yellow, cm >= nearCm && cm < farCm);
digitalWrite(red, cm < nearCm);
if (cm < nearCm) {
tone(buzzer, beepHz);
delay(100);
} else if (cm < farCm) {
tone(buzzer, beepHz, 50);
delay(cm * msPerCm);
} else {
noTone(buzzer);
delay(100);
}
}
Tap a line or an orange number
Serial monitor
Serial, 9600 baudboard time 0.0 s
About the lesson
The final project of the series. An HC-SR04 range finder on A1 and A2, a traffic light of green, yellow and red LEDs on D12, D11, D10, a buzzer on D8. A slider or the back and forth button moves the toy car on the scene, an icon turns the sound on. About 15 minutes, no sign-up. Five steps: guess, run, read the code, change the numbers and do a task with a check.
Build it for real
Parts
- Arduino Uno R3
- Mini breadboard
- HC-SR04 range finder
- 5 mm LEDs: green, yellow, red
- 4 resistors, 220 Ω
- 12 mm passive buzzer
- 6 male-to-male jumper wires
- 4 male-to-female jumper wires
Wires
| From | To |
|---|---|
| Rangefinder VCC | 5V |
| TRIG | A1 |
| ECHO | A2 |
| Rangefinder GND | GND |
| D12, D11, D10 | anodes of green, yellow, red |
| LED cathodes | 220 Ω resistors, then GND |
| D8 | 220 Ω resistor, then the buzzer "+" |
| buzzer "−" | GND |
The LED resistor can go on the anode or the cathode side: the current in the circuit is the same. Here it is on the cathode side, and all three resistors meet in one breadboard column to GND.
Open the schematic in the builderPinout of the lesson boardReady-made schematic: How to connect an HC-SR04 to ESP32HC-SR04 pinoutBuzzer pinoutLED pinout
The sketch below goes to the board with the default numbers: farCm 40, nearCm 15, msPerCm 8, beepHz 2000. The numbers you changed in the lesson are passed to the flasher by the To the board link above the code.
The whole sketch
// Parking sensor: HC-SR04 (TRIG on A1, ECHO on A2), traffic light on D12, D11, D10, buzzer on D8
const int trig = A1;
const int echo = A2;
const int green = 12;
const int yellow = 11;
const int red = 10;
const int buzzer = 8;
int farCm = 40; // farther than this the green LED is on, cm
int nearCm = 15; // closer than this the red LED is on and the buzzer is continuous, cm
int msPerCm = 8; // pause between beeps per centimeter, ms
int beepHz = 2000; // beep frequency, Hz
void setup() {
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
pinMode(green, OUTPUT);
pinMode(yellow, OUTPUT);
pinMode(red, OUTPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
int cm = pulseIn(echo, HIGH, 30000) / 58;
Serial.println(cm);
digitalWrite(green, cm >= farCm);
digitalWrite(yellow, cm >= nearCm && cm < farCm);
digitalWrite(red, cm < nearCm);
if (cm < nearCm) {
tone(buzzer, beepHz);
delay(100);
} else if (cm < farCm) {
tone(buzzer, beepHz, 50);
delay(cm * msPerCm);
} else {
noTone(buzzer);
delay(100);
}
}
What to keep in mind on a real board
A0-A5 on the Uno also work as digital pins 14-19: pinMode(), digitalWrite() and pulseIn() work the same with them.
If there is no echo, pulseIn() returns 0 after 30000 µs, and the sketch decides the car is right against the sensor: red and a solid beep. A car parking sensor repeats or skips such readings.
tone() takes Timer2, so the traffic light LEDs are switched with digitalWrite(), with no PWM on pins 3 and 11.
The sketch takes 4442 bytes of flash and 213 bytes of RAM.
Licenses of the lesson code
The avr8js 0.21.1 emulator, © 2019-2025 Uri Shaked, MIT license. The CodeMirror 6 editor, © Marijn Haverbeke and contributors, MIT license. The three.js scene, © three.js authors, MIT license.
The lesson firmware is built with the Arduino AVR core (LGPL 2.1), sources: ArduinoCore-avr. The lesson sketch and scene are made for this site.
MIT license text: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.