Introduction: Ultrasonic Distance Alarm System

This project uses an Arduino Uno to create a smart sensing system that:

  1. Measures distance using an ultrasonic sensor
  2. Lights up 7 LEDs based on how close something is
  3. Activates a servo motor to rotate based on distance
  4. Beeps a buzzer and flashes LEDs when someone is too close
  5. Displays the current mode and distance on a 4-digit TM1637 display
  6. Changes mode using a push button, turns on/off with a slide switch
  7. Uses an RGB LED to show mode colour (green/red/blue)

Supplies

Step 1: Project Development Reference Videos

Stage 1: LEDs + Sensor

  1. Ultrasonic sensor controlled 7 LEDs (one LED turned on for each distance level)
  2. Purpose: Learn how to use pulseIn() and control multiple LEDs

Stage 2: Servo + Sensor

  1. Added servo rotation based on distance
  2. 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)

  1. R → Pin 13 (through 220Ω)
  2. G → A0
  3. B → A1
  4. Anode → 5v

🔹 Ultrasonic Sensor

  1. Trig → Pin 11
  2. Echo → Pin 12
  3. VCC → 5V
  4. GND → GND

🔹 Button

  1. One side → Pin 9
  2. Other side → GND
  3. Use INPUT_PULLUP or external pull-down resistor

🔹 Slide Switch

  1. Center pin → A4
  2. One side → 5V
  3. Other side → GND


🔹 Buzzer

  1. Positive → A2
  2. Negative → GND

🔹 LEDs

  1. Pins 2–8 each control one LED with 220Ω resistor
  2. Common GND

🔹 TM1637 4 Digit Display

  1. CLK → A5
  2. DIO → A3
  3. VCC → 5V
  4. GND → GND

🔹 Servo

  1. Signal → Pin 10
  2. VCC → 5V
  3. 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:

  1. Green = Mode 1 (LEDs + 4-Digit Display)
  2. Red = Mode 2 (Servo + 4-Digit Display)
  3. Blue = Mode 3 (LEDs + Servo + 4-Digit Display)

4-Digit Display (TM1637):

Displays distance and current mode:

  1. First digit = Mode (1, 2, or 3)
  2. Second digit = Static dash (-)
  3. 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:

  1. Beeps when changing mode
  2. Melody when switching system ON/OFF
  3. Alarm when object is very close (<5 cm)


Step 4: Code

Copy–paste this into your Arduino IDE and upload:

Testing & Tips

  1. Open Serial Monitor to see “Mode:” and “Dist:” outputs.
  2. Tweak the th[] array or map(…) values to change at which distances LEDs or servo react.