Distance-Triggered Security + Smart Lighting System

by pareesaochani in Circuits > Arduino

15 Views, 1 Favorites, 0 Comments

Distance-Triggered Security + Smart Lighting System

final circuit.jpg

This project is an Arduino-based Distance-Triggered Security + Smart Lighting System. The system constantly calculates the distance of a person or an object from the sensor and reacts immediately with an RGB LED, a buzzer alarm, and an OLED screen.


The purpose is to demonstrate a real-world integration and application of:

  1. Sensors (HC-SR04 ultrasonic sensor)
  2. Digital outputs (RGB LEDs, buzzer)
  3. Visual feedback (RGB LED + OLED)
  4. Decision-making using conditional logic


This system may be employed for home security, hallway monitoring, or smart room lighting, thus being both feasible and technically sophisticated.


What the System Does (Logic)

The HC-SR04 ultrasonic sensor constantly measures distance.

SAFE MODE

  1. Distance > 100 cm
  2. RGB LED = Green
  3. OLED displays: Clear -- 128 cm

WARNING MODE

  1. Distance between 40–100 cm
  2. RGB LED = Yellow
  3. OLED displays: Approaching -- 73 cm

INTRUDER MODE

  1. Distance < 40 cm
  2. RGB LED = Red
  3. Buzzer alarm turns ON
  4. OLED displays: INTRUDER -- 22 cm

Supplies

  1. Arduino Uno (ATmega328P): performed the role of the principal microcontroller for processing sensor signals and controlling the outputs of the system.


  1. HC-SR04 ultrasonic distance sensor: applied for measuring the distance to the nearest object by utilizing the principles of ultrasonic time-of-flight.


  1. Common-anode RGB LED: employed for the purpose of signaling the system's state clearly (safe, warning, intruder).


  1. One (1) 330 Ω resistor: used for the current limiting purpose on the RGB LED, and it was connected to the power rail in order to safeguard the component.


  1. Active piezo buzzer: was utilized to provide a loud sound as feedback when the system goes into intruder mode.


  1. I2C OLED display (128 × 64): used to display live distance values and system status messages.


  1. Solderless breadboard: the base for the circuit setup and neat distribution of components.


  1. Male-to-male jumper wires of different lengths both short and long: were employed to facilitate trustworthy electrical connections between the components and the Arduino.


  1. Wire cutter/stripper: used to prepare jumper wires for clean and secure connections

Functional Logic Breakdown (IDEATE)

Screenshot 2026-01-21 175811.png

Before any wiring or coding, the system logic must be clearly defined.

Distance Zone Logic Explanation


The system employs three distance zones to signal rising security risk. The area is deemed secure when the measured distance exceeds 100 cm. The system remains in its normal condition, indicating it with a green RGB light, no sound, and a status message on the OLED screen.

A situation occurs, when the distance between the object and sensor varies from 40 cm to 100 cm, the system shifts to the warning state. This is an indication that something is getting closer, hence the RGB LED turns yellow and OLED screen shows an alert message, while the buzzer remains silent to prevent false alarms.


In the case of the distance being less than 40 cm, the system categorizes it as an invasion. The RGB LED lights up red, the buzzer goes off, and the OLED distinctly indicates an intruder with the current distance displayed.

Output Mapping

  1. RGB LED: Communicates system state at a glance
  2. OLED: Displays both numeric distance and warning text
  3. Buzzer: Activates only in high-risk conditions

This logic is later translated directly into conditional statements in code.

Research and Planning (Before Building)

Screenshot 2026-01-21 192115.png
RgbLEDS_CommonAnodeVsCommonCathode.png
buzzer buzzer pinout.png

Before starting any wiring or coding, complete the following research and planning steps to make sure the system works correctly.


Understand how the HC-SR04 ultrasonic sensor operates:

  1. Sends a sound pulse
  2. Measures time for echo to return
  3. Distance is determined by time-of-flight


Confirm the reliable sensing range of the HC-SR04 and set reasonable thresholds:

  1. 100 cm+ = safe
  2. 40-100 cm = warning
  3. <40 cm = intruder


State the visual indicators for each condition clearly:

  1. Green RGB LED → safe
  2. Yellow RGB LED → warning
  3. Red RGB LED → intruder


Audio feedback plan:

  1. Buzzer in the only mode of intruder to silence the constant noise or to avoid false alarms


Review OLED display requirements:

  1. Finding the right display type (OLED vs LCD)
  2. Choosing the right Arduino library
  3. Showing both text and numeric distance


Research RGB LED types:

  1. Find out if the LED is a common anode or common cathode
  2. Plan wiring and inverted logic accordingly
  3. Choose a fixed resistor value (330 Ω recommended)
  4. Prepare to use analog input to control LED brightness


Outline a simple system flow:

  1. Read sensors
  2. Identify distance zone
  3. Update LEDs, buzzer, and display


Wiring is to be done only after all components, pin assignments, and logic states are understood clearly

Optional – Visualize the Circuit in Tinkercad

Screenshot 2026-01-21 185547.png

Open Tinkercad Circuits

in case you need to see the virtual breadboard view before making the actual build.


Put the following components into Tinkercad:

  1. Arduino Uno
  2. HC-SR04 ultrasonic sensor
  3. RGB LED (common anode)
  4. OLED display
  5. Buzzer


Make connections of components exactly as planned for the physical assembly.


Optional: simulate the system to see what happens with the RGB LED, buzzer, and OLED before the actual wiring takes place.


Purpose: To familiarize the beginners with the connections, pin assignments, and logic flow.


Note: This step is not mandatory — the project can still be done completely on a real breadboard. Tinkercad is just for visualizing and testing ideas.

Wiring the Ultrasonic Sensor

ultrasonic distance sensor.jpg
Screenshot 2026-01-21 192115.png
  1. Connect Arduino 5V → breadboard power rail (+)
  2. Connect Arduino GND → breadboard ground rail (−)
  3. Connect HC-SR04 VCC → breadboard power rail (+)
  4. Connect HC-SR04 GND → breadboard ground rail (−)
  5. Connect TRIG → Digital Pin 9 on Arduino
  6. Connect ECHO → Digital Pin 10 on Arduino
Tip: Using the breadboard rails simplifies wiring for multiple components and keeps the build organized. Keep sensor wires short to ensure stable distance readings.


Wiring the RGB LED

rgb led.jpg
rgb pinout.png
RgbLEDS_CommonAnodeVsCommonCathode.png
  1. Connect the common anode pin → breadboard power rail (+)
  2. Connect Red pin → Pin 3 (PWM) through 330 Ω resistor
  3. Connect Green pin → Pin 5 (PWM) through 330 Ω resistor
  4. Connect Blue pin → Pin 6 (PWM) through 330 Ω resistor


You can use any Arduino digital pins that support PWM for the RGB LED; the exact pin numbers don’t matter as long as the code is updated to match your chosen pins.

Note: Common anode LED requires LOW to turn a color ON, so code logic inverts PWM values.

Wiring the OLED Display

unnamed.jpg
  1. Connect OLED VCC → breadboard power rail (+)
  2. Connect OLED GND → breadboard ground rail (−)
  3. Connect SDA → A4 and SCL → A5 (for I2C OLED)
  4. Test the OLED with a “Hello World” sketch before integrating into the full system
Tip: Using the breadboard rails keeps power consistent with other components, reducing wiring errors.


Wiring the Buzzer

IMG_8663.jpg
Screenshot 2026-01-21 195046.png
buzzer buzzer pinout.png
  1. Positive terminal → Pin 8 on Arduino
  2. Negative terminal → breadboard ground rail (−)
Tip: Activate the buzzer only in intruder mode in code to prevent unnecessary noise. Test separately before integration.

Uploading and Testing Code

Connect Arduino to computer via USB

Open Arduino IDE and install required libraries (OLED, etc.)

Download and upload the code that:

  1. Reads distance from HC-SR04
  2. Updates RGB LED, buzzer, and OLED

Test components individually:

  1. Distance readings on Serial Monitor
  2. LED colors and brightness
  3. Buzzer sound
  4. OLED messages

Integrate everything after all individual tests pass

Troubleshooting

9c02a612-6bb1-4663-a279-7d914ce7116c.png

OLED blank → Inspect power, GND, SDA/SCL pins, library

RGB colors wrong → Confirm LED is common anode and PWM values are reversed

Buzzer silent → Look at the ground rail connection and Pin 8 for wiring issues

Fluctuating distance readings → Ensure objects are directly in front of sensor; minimize interference


Tip: Using breadboard rails for power/GND reduces wiring mistakes and makes debugging easier.

Final Testing

final circuit.jpg

Test distance zones:

  1. 100 cm → Green LED, no buzzer, OLED shows “SAFE”
  2. 40–100 cm → Yellow LED, no buzzer, OLED shows “WARNING”
  3. <40 cm → Red LED, buzzer ON, OLED shows “ALERT”

Confirm real-time response as objects/potential obstacles move

Optional Extensions

  1. In code, modify distance thresholds to obtain different detection ranges.


  1. Utilize more than one sensor in order to achieve a wider coverage area.


  1. Record distance data or set up mobile alerts as a part of the system.


  1. Adjust the mapping of LED brightness through the usage of an LDR (Light Dependent Resistor) output.