Home/ Projects/ Parking assistant
Project 7 of 16 Sensing Intermediate

Parking assistant

Take the distance reading and turn it into beep rate: slow far away, faster as you close in, solid at the stop point. Short to build, and it teaches you to turn a number into something a person can act on without looking at a screen. Averaging a few readings stops a single bad echo from making it stutter.

ESP32 & ESP8266 IoT Automation Kit
One of 16 projects in the ESP32 & ESP8266 IoT Automation Kit — the parts below are from that kit.

What this project uses

Every item is in the kit — nothing extra to buy.

Everything for this project

ESP32 & ESP8266 IoT Automation Kit

All 24 parts in one box, with this project and 15 more to build from them.

AED 180.00 See the kit
Need only these parts? Order them loose · from AED 104.05
Used in this project tick what you still need
Also needed untick if you already have them
Selected parts AED 104.05

One of each part. Packs (resistor assortment, jumper wires) already hold more than a single build needs. Backorder parts ship once they arrive — the kit above has every part boxed today. Delivery is free on orders AED 150.00+.

Sketch for this project

Written for the board in the kit. Copy it into the Arduino IDE; change the pin numbers if you wire it differently.

07 Parking assistant Arduino C++
/*
  07 — Parking assistant
  Board: ESP32.  Parts: HC-SR04, buzzer module, 3 x 1 kΩ from the resistor pack.

  Wiring:
    HC-SR04: VCC -> VIN (5 V), GND -> GND, TRIG -> GPIO 5
             ECHO -> 1 kΩ -> GPIO 18, then two 1 kΩ in series from GPIO 18 -> GND
    Buzzer:  VCC -> 3V3, GND -> GND, IO -> GPIO 23   (active buzzer: HIGH = sound)

  Distance becomes beep rate: slow far away, faster closing in, solid at the
  stop line. Turning a number into something a person reacts to without a screen.
*/

const int TRIG_PIN   = 5;
const int ECHO_PIN   = 18;
const int BUZZER_PIN = 23;

const float STOP_CM  = 15;    // solid tone at or below this
const float FAR_CM   = 120;   // silent beyond this

float readCm() {
  digitalWrite(TRIG_PIN, LOW);  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH); delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);
  unsigned long us = pulseIn(ECHO_PIN, HIGH, 30000UL);
  return us == 0 ? -1 : us / 58.0;
}

void setup() {
  Serial.begin(115200);
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  pinMode(BUZZER_PIN, OUTPUT);
}

void loop() {
  // Average 3 readings — a single bad echo would make the beeps stutter.
  float sum = 0; int n = 0;
  for (int i = 0; i < 3; i++) { float d = readCm(); if (d > 0) { sum += d; n++; } delay(20); }
  float cm = n ? sum / n : -1;
  Serial.println(cm);

  if (cm < 0 || cm > FAR_CM) {            // nothing close: quiet
    digitalWrite(BUZZER_PIN, LOW);
    delay(100);
  } else if (cm <= STOP_CM) {             // stop line: solid
    digitalWrite(BUZZER_PIN, HIGH);
    delay(100);
  } else {                                // beep, faster as it gets closer
    int gap = map((int) cm, (int) STOP_CM, (int) FAR_CM, 60, 600);   // ms between beeps
    digitalWrite(BUZZER_PIN, HIGH); delay(40);
    digitalWrite(BUZZER_PIN, LOW);  delay(gap);
  }
}
Same HC-SR04 divider as project 06.

More from this kit

All projects →

Root
Online — usually replies instantly
Catalog assistant: verify final stock/price on the product page before checkout.