7.3 Parking Sensor

The goal of this project is to build a reverse parking sensor using the Soldered NULA MINI, an HC-SR04 ultrasonic distance sensor, a passive buzzer, and an LED.
This project teaches you how to measure distances, use conditional logic to control outputs, and simulate how real car parking sensors work.
In this documentation you will learn:
- How to use the HC-SR04 ultrasonic sensor to measure distance.
- How to generate sound using a passive buzzer with
tone(). - How to use an LED as a warning indicator.
- How to implement time-based beeping logic that changes with distance.
Hardware required
- 1× Soldered NULA MINI board
- 1× HC-SR04 ultrasonic distance sensor
- 1× Passive buzzer
- 1× LED (any color)
- 1× 330 Ω resistor
- Breadboard
- Jumper wires
- USB-C cable

How it works
The HC-SR04 sensor measures the distance to an object by sending an ultrasonic pulse and timing how long it takes for the echo to return.
The NULA MINI then uses this distance to control how fast the buzzer beeps and whether the LED turns on.
As the object gets closer:
- The buzzer beeps faster,
- The LED lights up when the distance is less than 10 cm.
This simulates how parking sensors in real cars alert drivers when they’re too close to an obstacle.
Wiring and connections
| Component | NULA MINI Pin | Description |
|---|---|---|
| HC-SR04 VCC | 3.3 V | Power supply |
| HC-SR04 GND | GND | Ground |
| HC-SR04 TRIG | IO4 | Trigger pin to send sound pulse |
| HC-SR04 ECHO | IO3 | Echo pin to receive reflection |
| Passive Buzzer | IO2 | Emits warning tone |
| LED (with 330 Ω resistor) | IO5 | Turns ON when object is closer than 10 cm |

Distance behavior logic
| Distance range (cm) | Buzzer behavior | LED behavior |
|---|---|---|
| >100 cm | Silent | OFF |
| 60–100 cm | Slow beeps | OFF |
| 30–60 cm | Medium beeps | OFF |
| 10–30 cm | Fast beeps | OFF |
| < 10 cm | Continuous tone | ON |
Full code
Below is the complete example code for this project.
#include "Ultrasonic-distance-sensor-easyC-SOLDERED.h" // Include Soldered ultrasonic sensor library
// --- Pin definitions ---
#define TRIGPIN 4 // Trigger pin for the HC-SR04
#define ECHOPIN 3 // Echo pin for the HC-SR04
#define BUZZER_PIN 2 // Passive buzzer pin
#define LED_PIN 5 // LED warning indicator pin
// --- Create ultrasonic sensor object ---
Ultrasonic_Sensor hc(TRIGPIN, ECHOPIN);
// --- Buzzer parameters ---
const int TONE_FREQ = 2700; // Frequency in Hz (try 2–4 kHz for loudest result)
// --- Timing variables ---
unsigned long lastBeep = 0; // Time of the last beep toggle
unsigned long beepInterval = 0; // How long between beeps
bool buzzerOn = false; // Is the buzzer currently on?
void setup()
{
Serial.begin(115200); // Start serial communication
hc.begin(); // Initialize ultrasonic sensor
pinMode(BUZZER_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW); // Ensure LED is off initially
Serial.println("Ultrasonic buzzer + LED reverse sensor started");
}
void loop()
{
// --- Measure distance ---
float distance = hc.getDistance(); // Distance in centimeters
long duration = hc.getDuration(); // Echo round-trip time in microseconds
// --- Print readings to Serial Monitor ---
Serial.print("Distance from obstacle: ");
Serial.print(distance);
Serial.println(" cm");
// --- Default LED state ---
digitalWrite(LED_PIN, LOW);
// --- Decide buzzer and LED behavior based on distance ---
if (distance > 100) {
beepInterval = 0;
noTone(BUZZER_PIN);
}
else if (distance > 60) {
beepInterval = 800;
}
else if (distance > 30) {
beepInterval = 400;
}
else if (distance > 10) {
beepInterval = 150;
}
else {
// Closer than 10 cm → continuous tone + LED ON
tone(BUZZER_PIN, TONE_FREQ);
beepInterval = 0;
digitalWrite(LED_PIN, HIGH);
}
// --- Handle timed beeping ---
if (beepInterval > 0) {
unsigned long now = millis();
if (now - lastBeep >= beepInterval) {
lastBeep = now;
buzzerOn = !buzzerOn; // Toggle buzzer
if (buzzerOn) tone(BUZZER_PIN, TONE_FREQ);
else noTone(BUZZER_PIN);
}
}
delay(50); // Small delay for stability
}
Full example

Check out the full example code on the link below:
7.3_Parking_Sensor.ino
Project that uses an ultrasonic sensor, buzzer, and LED to simulate a reverse-parking warning system.