Skip to content
FlintMindsHigh School
High School

Middle School

Distance Alarm

Use an ultrasonic sensor to measure distance and trigger an alarm when something gets too close.

Level
Builder
Time
45 min
Parts
5
Hardware-checked
Not yet

What this build assumes

  • Controlling a motor with code
  • Commanding precise angles
  • Using a code library

0 of 5 build milestones complete

What this build is

Ultrasonic sensors measure distance by sending out a sound pulse and timing how long it takes to bounce back, the same idea behind a car's parking sensors.

In this project you'll read live distance values and use them to decide when to sound an alarm.

How it works

System architecture

  1. Input

    The ultrasonic sensor measures distance to the nearest object.

  2. Process

    Your code compares the reading to a threshold.

  3. Output

    The buzzer sounds when something is too close.

Parts

Microcontroller board × 1

Reads the sensor and decides when to trigger the alarm.

Ultrasonic distance sensor (e.g. HC-SR04) × 1

Sends out a sound pulse and measures how long it takes to bounce back, giving a distance reading.

Buzzer or piezo speaker × 1

Sounds an alert when something gets closer than your chosen threshold.

Breadboard × 1

Holds the sensor and buzzer connections in place.

Jumper wires × a handful

Wire the sensor and buzzer to the board.

Build

  1. 1

    Mount the sensor

    Place the ultrasonic sensor facing the direction you want to monitor.

    Sensor mounting diagram

    Not drawn yet. We only publish a diagram once someone has built this with real parts and checked it — so rather than guess at the connections, we are telling you they are missing. The written wiring notes are accurate.

  2. 2

    Wire power, ground, and signal pins

    Connect the sensor's power and ground to your board, and its trigger and echo pins to two digital pins.

    Sensor wiring diagram

    Not drawn yet. We only publish a diagram once someone has built this with real parts and checked it — so rather than guess at the connections, we are telling you they are missing. The written wiring notes are accurate.

  3. 3

    Add the buzzer

    Connect the buzzer's positive leg to a digital pin and its negative leg to GND.

    Buzzer wiring diagram

    Not drawn yet. We only publish a diagram once someone has built this with real parts and checked it — so rather than guess at the connections, we are telling you they are missing. The written wiring notes are accurate.

  4. 4

    Upload the alarm code

    Connect your board to your computer and upload the code below.

  5. 5

    Calibrate your threshold

    Test at different distances and adjust the threshold variable until the alarm feels right.

Wiring

Distance Alarm wiring diagram

Not drawn yet. We only publish a diagram once someone has built this with real parts and checked it — so rather than guess at the connections, we are telling you they are missing. The written wiring notes are accurate.

The ultrasonic sensor has four pins: power, ground, trigger, and echo. Power and ground connect to 5V/GND; trigger and echo connect to two digital pins — match the pins in your code below. The buzzer's positive leg connects to a separate digital pin, with its negative leg to GND.

Code

arduino
1const int trigPin = 9;  // update to match your wiring2const int echoPin = 10; // update to match your wiring3const int buzzerPin = 8;4const int thresholdCm = 15; // distance that triggers the alarm5 6void setup() {7  pinMode(trigPin, OUTPUT);8  pinMode(echoPin, INPUT);9  pinMode(buzzerPin, OUTPUT);10}11 12void loop() {13  digitalWrite(trigPin, LOW);14  delayMicroseconds(2);15  digitalWrite(trigPin, HIGH);16  delayMicroseconds(10);17  digitalWrite(trigPin, LOW);18 19  long duration = pulseIn(echoPin, HIGH);20  long distanceCm = duration * 0.034 / 2;21 22  if (distanceCm < thresholdCm) {23    digitalWrite(buzzerPin, HIGH);24  } else {25    digitalWrite(buzzerPin, LOW);26  }27}28 
  • Line 4: thresholdCm is the distance, in centimeters, that counts as "too close" — change this number to tune sensitivity.
  • Line 22: This conditional is what decides whether to trigger the buzzer.

Expected behaviour

  1. 1Upload the code and wave your hand in front of the sensor.
  2. 2The buzzer should stay silent until your hand gets closer than the threshold distance, then sound.
  3. 3Move your hand away and confirm the buzzer stops.

Diagnosis

Symptoms and their usual causes. Work down the list — each check rules out a subsystem.

The buzzer never sounds.
Double-check trigPin and echoPin match your wiring, and that the sensor is facing the object you're testing with.
The buzzer sounds constantly.
Try increasing thresholdCm, or check that nothing is blocking the sensor at close range.
Readings seem random or inconsistent.
Ultrasonic sensors can misread soft or angled surfaces — test against a flat, hard object first.

Open problems

Extensions

Small change

Make the threshold adjustable

Store the trigger distance in a variable you can easily change and retest.

Add something

Add a graduated warning

Add an LED that blinks faster the closer an object gets.

Design it yourself

Track the closest distance

Keep track of and print the closest distance seen since the program started.

You decide how it works. No steps for this one.

Check your understanding

1. How does an ultrasonic sensor measure distance?

2. What does the threshold value in your code represent?

3. Why is a conditional statement (if/else) needed in this project?

Where this leads

You now know:

  • Reading a sensor's input
  • Measuring distance
  • Using conditional (if/else) logic
  • Calibrating a threshold

The engineering brief for this project has not been written yet.

The problem statement, the constraints, the design decisions and the validation procedure are the parts of a project that make it worth an engineering student’s time, and this one does not have them yet. Writing them means building it with real components and measuring what it does — so rather than approximate, we are telling you they are missing. Why we do this.