Ultrasonic Distance Alarm System

IMG_3055.jpeg)
This project uses an Arduino Uno to create a smart sensing system that:
- Measures distance using an ultrasonic sensor
- Lights up 7 LEDs based on how close something is
- Activates a servo motor to rotate based on distance
- Beeps a buzzer and flashes LEDs when someone is too close
- Displays the current mode and distance on a 4-digit TM1637 display
- Changes mode using a push button, turns on/off with a slide switch
- Uses an RGB LED to show mode colour (green/red/blue)
Supplies
IMG_3040.jpeg)
IMG_3038.jpeg)
Materials
Arduino Uno (1)
(HC-SR04) Ultrasonic Sensor (1)
Slide Switch (1)
Push Button (1)
Buzzer (1)
LEDs (Random Colors) (x7)
220Ξ© or 330Ξ© Resistors (10)
If you are using Normal wires These are the tools you will need
Wire Cutter (1)
Wire Stripper (1)
Long Nose Plier (1)
Project Development Reference Videos
Stage 1: LEDs + Sensor
- Ultrasonic sensor controlled 7 LEDs (one LED turned on for each distance level)
- Purpose: Learn how to use pulseIn() and control multiple LEDs
Stage 2: Servo + Sensor
- Added servo rotation based on distance
- Learned map() and how to use servo.write()
I made stage one and 2 from this video
than I kept on adding components to the project to make it a level 4 project
How to Build the Circuit
IMG_3041.jpeg)
IMG_3042.jpeg)


IMG_3033.jpeg)
IMG_3036.jpeg)
IMG_3053.jpeg)
Place the Arduino and breadboard side by side.
Connections
πΉRGB LED (Common anode)
- R β Pin 13 (through 220Ξ©)
- G β A0
- B β A1
- Anode β 5v
πΉ Ultrasonic Sensor
- Trig β Pin 11
- Echo β Pin 12
- VCC β 5V
- GND β GND
πΉ Button
- One side β Pin 9
- Other side β GND
- Use INPUT_PULLUP or external pull-down resistor
πΉ Slide Switch
- Center pin β A4
- One side β 5V
- Other side β GND
πΉ Buzzer
- Positive β A2
- Negative β GND
πΉ LEDs
- Pins 2β8 each control one LED with 220Ξ© resistor
- Common GND
πΉ TM1637 4 Digit Display
- CLK β A5
- DIO β A3
- VCC β 5V
- GND β GND
πΉ Servo
- Signal β Pin 10
- VCC β 5V
- GND β GND
Connect the red rail with other side of bread board red rail with wire and connect black rail with the other side of bread board black rail with a black wire.
How the Circuit Works

Ultrasonic sensor: It sends out a sound pulse (ultrasonic) on βTrigβ and listens for the echo on βEcho.β The time between sending and hearing the pulse tells us the distance.
LEDs (7 total):
Each LED represents a distance threshold (e.g., 25cm, 20cm, 15cm, etc.). The closer the object, the more LEDs light up.
RGB LED:
Shows the current mode with color:
- Green = Mode 1 (LEDs + 4-Digit Display)
- Red = Mode 2 (Servo + 4-Digit Display)
- Blue = Mode 3 (LEDs + Servo + 4-Digit Display)
4-Digit Display (TM1637):
Displays distance and current mode:
- First digit = Mode (1, 2, or 3)
- Second digit = Static dash (-)
- Last two digits = Distance in cm
Servo motor : Rotates from 0Β° to 180Β° based on how close the object is (closer = larger angle).
Push button: Each press changes the mode (1 β 2 β 3 β back to 1). Single beep when changed to mode 1, double for mode 2, long beep for mode 3 confirms the change.
Slide Switch:
When OFF, the whole system shuts down: RGB turns off, LEDs off, servo is detached, display is cleared, and buzzer silences.
When ON, the system turns back on and resumes its last mode.
Buzzer :
Gives feedback:
- Beeps when changing mode
- Melody when switching system ON/OFF
- Alarm when object is very close (<5 cm)
Code
Copyβpaste this into your Arduino IDE and upload:
Testing & Tips
- Open Serial Monitor to see βMode:β and βDist:β outputs.
- Tweak the th[] array or map(β¦) values to change at which distances LEDs or servo react.