Introduction: Ultrasonic Distance Alarm System
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
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)
Step 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
Step 2: How to Build the Circuit
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.
Step 3: 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)
Step 4: 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.


