Sharjah, UAE GST
Home/ Projects/ Lesson 3: Parking Beeper
Lesson 3 of 9 Automation Beginner ◎ 30 min

Lesson 3: Parking Beeper

Add the buzzer to make a desk-size parking sensor: slow beeps when something is near, fast beeps when it is very close.

Arduino Ultrasonic Distance & Parking Learning Kit
One of 9 projects in the Arduino Ultrasonic Distance & Parking Learning Kit — the parts below are from that kit.

Before you start

Time
30 min
Do these first
On your own
You can do this lesson on your own.

Exact parts

PartHow many
Wiring from Lessons 1-2
Active buzzer module1
Male-to-female jumper wires3

Not in the box

Nothing extra - just the kit and your computer.

What you build and learn

  • Wiring a 3-pin module with male-to-female jumpers
  • Making a beep pattern without stopping the program (millis())
  • Adding a fourth zone (CRITICAL)
  • Why a learning model is not a safety device

The zones are the same as Lesson 2. Under 20 cm red now beeps slowly (120 ms on, 380 ms off); under 10 cm it beeps fast (100 ms on, 100 ms off).

beep() never waits. Each time round loop() it checks millis(): if it is time to switch, it flips the buzzer and works out when the next switch is due. So the sensor keeps measuring while it beeps.

With no echo, yellow blinks and the buzzer stays silent: a beep would claim the sketch knows the distance when it does not.

Real parking sensors are tested and certified. This one is a desk model for learning - never use it on a vehicle.

New words

Active buzzer
A buzzer with its own tone maker inside: switch its pin HIGH and it beeps.
Module
A small board with a part already fitted and its pins labelled.
millis()
The number of milliseconds since the UNO started. Comparing it with a saved time tells us when to switch the buzzer.

What this project uses

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

Build this with

Arduino Ultrasonic Distance & Parking Learning Kit

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

AED 85.00 See the kit
Need only these parts? Order them loose · from AED 87.65
Used in this project tick what you still need
Selected parts AED 87.65

One of each part. Packs (resistor assortment, jumper wires) already hold more than a single build needs. active-buzzer module, red/yellow/green LEDs: included in the kit, ask us for it loose. Delivery is AED 20, free on orders AED 100+.

Connections

Unplug USB before you change any wire. Pin names are the labels printed on the board.

Lesson 3: Parking Beeper - connection diagram with labelled pins
Connection diagram, drawn from this lesson's pin plan - not a photo of a tested build. Tap to open it full size. The table below says the same thing in words.
Part / pinConnect toNote
Buzzer I/O (signal)UNO pin 8May be labelled S or IN
Buzzer VCC (+)+ rail (5V)
Buzzer GND (−)− rail (GND)
LEDs and HC-SR04As in Lessons 1-2Pins 5, 6, 7, 9, 10

Step by step

One thing at a time. Tick nothing off until it matches the diagram.

  1. Unplug the USB cable.Watch out: Always unplug the USB cable before you add, move or remove a wire.
  2. Look at the buzzer module's 3 pins and read their labels: I/O (or S or IN), VCC and GND.Watch out: Go by the labels, not the position. Some modules put the pins in a different order.
  3. Push the female ends of 3 male-to-female jumper wires onto the buzzer pins.
  4. Plug the I/O wire into UNO pin 8, the VCC wire into the + rail and the GND wire into the − rail.
  5. Check against the diagram, plug in USB, then copy the Lesson 3 code into the IDE and upload it.Watch out: Keep the buzzer away from your ears.
  6. Move a book slowly towards the sensor from 60 cm. Listen as you pass 20 cm and 10 cm.Watch out: If the beeping is too much, unplug USB.

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.

lesson3_parking_beeper.ino arduino
/*
  DIY KIT - Arduino Ultrasonic Distance & Parking Learning Kit
  Lesson 3 of 9: Parking Beeper

  WIRING (unplug the USB cable before you wire)
    Keep the sensor and LEDs from Lessons 1-2.
    Active buzzer module, with 3 male-to-female jumper wires:
      I/O (signal, may be labelled S or IN) -> UNO pin 8
      VCC (+)                               -> 5V  (+ rail)
      GND (-)                               -> GND (- rail)

  WHAT SHOULD HAPPEN
    Green  50 cm or more   silent
    Yellow 20 to 49 cm     silent
    Red    10 to 19 cm     slow beeps
    Red    under 10 cm     fast beeps
    No echo: yellow blinks and the buzzer stays silent.
    A tabletop model of a parking sensor - never use it on a real vehicle.
*/

const int RED_LED = 5;
const int YELLOW_LED = 6;
const int GREEN_LED = 7;
const int BUZZER_PIN = 8;
const int TRIG_PIN = 9;
const int ECHO_PIN = 10;

const int CLEAR_CM = 50;      // this far or more: green
const int NEAR_CM = 20;       // closer than this: red + slow beeps
const int CRITICAL_CM = 10;   // closer than this: red + fast beeps

const unsigned long SLOW_ON_MS = 120, SLOW_OFF_MS = 380;   // slow beep
const unsigned long FAST_ON_MS = 100, FAST_OFF_MS = 100;   // fast beep

// Most modules beep when the pin is HIGH. If yours beeps in the gaps or
// all the time, change false to true and upload again.
const bool BUZZER_ACTIVE_LOW = false;

const unsigned long ECHO_TIMEOUT_US = 30000;
const int MIN_CM = 2;
const int MAX_CM = 400;

bool buzzerOn = false;
unsigned long nextBeepChange = 0;

long readOnceCm() {
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);
  unsigned long us = pulseIn(ECHO_PIN, HIGH, ECHO_TIMEOUT_US);
  if (us == 0) return 0;
  long cm = us / 58;
  if (cm < MIN_CM || cm > MAX_CM) return 0;
  return cm;
}

void showLights(bool red, bool yellow, bool green) {
  digitalWrite(RED_LED, red ? HIGH : LOW);
  digitalWrite(YELLOW_LED, yellow ? HIGH : LOW);
  digitalWrite(GREEN_LED, green ? HIGH : LOW);
}

void buzzer(bool on) {
  buzzerOn = on;
  if (BUZZER_ACTIVE_LOW) digitalWrite(BUZZER_PIN, on ? LOW : HIGH);
  else                   digitalWrite(BUZZER_PIN, on ? HIGH : LOW);
}

// Beep with the given on and off times without stopping the program:
// millis() tells us when it is time to switch.
void beep(unsigned long onMs, unsigned long offMs) {
  unsigned long now = millis();
  if (now >= nextBeepChange) {
    buzzer(!buzzerOn);
    nextBeepChange = now + (buzzerOn ? onMs : offMs);
  }
}

void setup() {
  pinMode(RED_LED, OUTPUT);
  pinMode(YELLOW_LED, OUTPUT);
  pinMode(GREEN_LED, OUTPUT);
  pinMode(BUZZER_PIN, OUTPUT);
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  buzzer(false);
  Serial.begin(9600);
  Serial.println("Lesson 3: Parking Beeper - learning model only");
  Serial.println("cm, zone, buzzer");
}

void loop() {
  long cm = readOnceCm();
  const char *zone;

  if (cm == 0) {
    zone = "NO_ECHO";
    unsigned long halfSeconds = millis() / 500;   // blink yellow, as in Lesson 2
    showLights(false, halfSeconds % 2 == 0, false);
    buzzer(false);                       // unknown: never beep "all clear" or "danger"
  } else if (cm < CRITICAL_CM) {
    zone = "CRITICAL";
    showLights(true, false, false);
    beep(FAST_ON_MS, FAST_OFF_MS);
  } else if (cm < NEAR_CM) {
    zone = "NEAR";
    showLights(true, false, false);
    beep(SLOW_ON_MS, SLOW_OFF_MS);
  } else if (cm < CLEAR_CM) {
    zone = "CAUTION";
    showLights(false, true, false);
    buzzer(false);
  } else {
    zone = "CLEAR";
    showLights(false, false, true);
    buzzer(false);
  }

  Serial.print(cm);
  Serial.print(", ");
  Serial.print(zone);
  Serial.print(", ");
  Serial.println(buzzerOn ? 1 : 0);
  delay(40);
}
ℹCompiler-checked for Arduino UNO (0 warnings) and run in the kit simulator. Pins match the lesson diagram. Not yet bench-tested on the shipped parts.

Upload it

  1. Click Copy (or Download .ino and open the file).
  2. In the Arduino IDE choose File → New Sketch, select everything in it and paste.
  3. Check the bottom of the window says Arduino Uno and your port (Start here, step 5).
  4. Click Upload (the → arrow) and wait for Done uploading.

How the code works

In the codeWhat it does
const int CRITICAL_CM = 10;The new, closest zone: fast beeps.
const bool BUZZER_ACTIVE_LOW = false;Most modules beep when the pin is HIGH. If yours beeps in the gaps or all the time, change it to true.
beep(onMs, offMs)Our own function. It uses millis() to flip the buzzer at the right moments without stopping the program.
buzzer(false) in the NO_ECHO branchSilence when the distance is unknown.

Change it and see

Change: Change SLOW_OFF_MS from 380 to 900.What happens: The slow beeps are further apart.
Change: Change CRITICAL_CM from 10 to 15.What happens: Fast beeping starts further away.

What you should see or hear

  • Green and silent at 50 cm and more; yellow and silent at 20-49 cm.
  • Red with slow beeps at 10-19 cm; red with fast beeps under 10 cm.
  • No echo: yellow blinks and the buzzer is silent. The Serial Monitor shows cm, zone and 1 or 0 for the buzzer.

If it doesn't work

ProblemWhat to check
No sound at allUnplug USB. Check I/O to pin 8, VCC to the + rail, GND to the − rail, by the labels on the module.
It beeps in the gaps, or all the timeYour module beeps when the pin is LOW. Set BUZZER_ACTIVE_LOW to true and upload again.
Only a click, no beepIt may be a passive buzzer, which needs tone(). Check the label says active, or ask us.
Beeps but the LEDs are wrongCheck the LEDs against Lesson 2: red 5, yellow 6, green 7.
It always says no echoUnplug USB. Check VCC to the + rail, GND to the − rail, TRIG to pin 9 and ECHO to pin 10 - by the labels on the sensor, not their position. Point it at a big flat object 20-50 cm away.
The upload failsSee Start here, step 6: "If the upload fails".

Small experiment

Hold the book at exactly 10 cm. Does the beep keep switching between slow and fast? Why might that happen at a zone edge?

Check what you learned

What is the difference between an active and a passive buzzer?

An active buzzer has its own tone maker and beeps when switched on; a passive one needs the UNO to make the tone.

Why does beep() use millis() instead of delay()?

delay() would stop the program, so the sensor could not measure while the buzzer beeps.

Why must you never use this on a real car?

It is an untested learning model: the sensor can miss objects, and real parking aids are tested and certified.

▶In the simulator: The simulator shows the buzzer turning on and off with the same timing. It cannot tell whether your module beeps on HIGH or LOW - only the real buzzer can.

Next

  • Lesson 4 explores what happens when the sensor gets it wrong: too close, too far and no echo. Keep all the wiring.

Stuck?

Most problems are one wire in the wrong hole. Check these first:

  1. Unplug, then compare every wire with the lesson's diagram and table. One pin off is the most common problem.
  2. Sensor: are VCC, TRIG, ECHO and GND wired by their labels, with TRIG on 9 and ECHO on 10?
  3. LEDs: is the long leg on the resistor side?
  4. Is the − rail connected to a UNO GND pin, and the + rail to 5V?
  5. Serial Monitor: is it set to 9600 baud (and "New Line" when you type commands)?
  6. Upload problems: is Tools → Board set to Arduino Uno and Tools → Port to your UNO? (Start here, step 5.)
Still stuck? Ask us on WhatsApp. Send this message with a photo of your wiring - it tells us exactly where you are.
Hi DIY KIT, I'm stuck on the Arduino Ultrasonic Distance & Parking Learning Kit.
Lesson: __
Step number: __
What should happen: __
What happens instead: __
Error message (copied exactly): __
I'll attach a photo of my wiring.
Message us on WhatsApp

More from this kit

All projects →

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