Skip to main content

7.3 Parking Sensor

placeholder
Parking Sensor project demonstration

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
placeholder
All components needed for the Parking Sensor project

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

ComponentNULA MINI PinDescription
HC-SR04 VCC3.3 VPower supply
HC-SR04 GNDGNDGround
HC-SR04 TRIGIO4Trigger pin to send sound pulse
HC-SR04 ECHOIO3Echo pin to receive reflection
Passive BuzzerIO2Emits warning tone
LED (with 330 Ω resistor)IO5Turns ON when object is closer than 10 cm
placeholder
Wiring diagram for Parking Sensor project

Distance behavior logic

Distance range (cm)Buzzer behaviorLED behavior
>100 cmSilentOFF
60–100 cmSlow beepsOFF
30–60 cmMedium beepsOFF
10–30 cmFast beepsOFF
< 10 cmContinuous toneON

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

placeholder
Full project demonstration video

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.