Distance-Triggered Security + Smart Lighting System
by pareesaochani in Circuits > Arduino
15 Views, 1 Favorites, 0 Comments
Distance-Triggered Security + Smart Lighting System
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:
- Sensors (HC-SR04 ultrasonic sensor)
- Digital outputs (RGB LEDs, buzzer)
- Visual feedback (RGB LED + OLED)
- 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
- Distance > 100 cm
- RGB LED = Green
- OLED displays: Clear -- 128 cm
WARNING MODE
- Distance between 40–100 cm
- RGB LED = Yellow
- OLED displays: Approaching -- 73 cm
INTRUDER MODE
- Distance < 40 cm
- RGB LED = Red
- Buzzer alarm turns ON
- OLED displays: INTRUDER -- 22 cm
Supplies
- Arduino Uno (ATmega328P): performed the role of the principal microcontroller for processing sensor signals and controlling the outputs of the system.
- HC-SR04 ultrasonic distance sensor: applied for measuring the distance to the nearest object by utilizing the principles of ultrasonic time-of-flight.
- Common-anode RGB LED: employed for the purpose of signaling the system's state clearly (safe, warning, intruder).
- 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.
- Active piezo buzzer: was utilized to provide a loud sound as feedback when the system goes into intruder mode.
- I2C OLED display (128 × 64): used to display live distance values and system status messages.
- Solderless breadboard: the base for the circuit setup and neat distribution of components.
- 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.
- Wire cutter/stripper: used to prepare jumper wires for clean and secure connections
Functional Logic Breakdown (IDEATE)
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
- RGB LED: Communicates system state at a glance
- OLED: Displays both numeric distance and warning text
- Buzzer: Activates only in high-risk conditions
This logic is later translated directly into conditional statements in code.
Research and Planning (Before Building)
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:
- Sends a sound pulse
- Measures time for echo to return
- Distance is determined by time-of-flight
Confirm the reliable sensing range of the HC-SR04 and set reasonable thresholds:
- 100 cm+ = safe
- 40-100 cm = warning
- <40 cm = intruder
State the visual indicators for each condition clearly:
- Green RGB LED → safe
- Yellow RGB LED → warning
- Red RGB LED → intruder
Audio feedback plan:
- Buzzer in the only mode of intruder to silence the constant noise or to avoid false alarms
Review OLED display requirements:
- Finding the right display type (OLED vs LCD)
- Choosing the right Arduino library
- Showing both text and numeric distance
Research RGB LED types:
- Find out if the LED is a common anode or common cathode
- Plan wiring and inverted logic accordingly
- Choose a fixed resistor value (330 Ω recommended)
- Prepare to use analog input to control LED brightness
Outline a simple system flow:
- Read sensors
- Identify distance zone
- 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
Open Tinkercad Circuits
in case you need to see the virtual breadboard view before making the actual build.
Put the following components into Tinkercad:
- Arduino Uno
- HC-SR04 ultrasonic sensor
- RGB LED (common anode)
- OLED display
- 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
- Connect Arduino 5V → breadboard power rail (+)
- Connect Arduino GND → breadboard ground rail (−)
- Connect HC-SR04 VCC → breadboard power rail (+)
- Connect HC-SR04 GND → breadboard ground rail (−)
- Connect TRIG → Digital Pin 9 on Arduino
- 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
- Connect the common anode pin → breadboard power rail (+)
- Connect Red pin → Pin 3 (PWM) through 330 Ω resistor
- Connect Green pin → Pin 5 (PWM) through 330 Ω resistor
- 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
- Connect OLED VCC → breadboard power rail (+)
- Connect OLED GND → breadboard ground rail (−)
- Connect SDA → A4 and SCL → A5 (for I2C OLED)
- 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
- Positive terminal → Pin 8 on Arduino
- 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:
- Reads distance from HC-SR04
- Updates RGB LED, buzzer, and OLED
Test components individually:
- Distance readings on Serial Monitor
- LED colors and brightness
- Buzzer sound
- OLED messages
Integrate everything after all individual tests pass
Downloads
Troubleshooting
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
Test distance zones:
- 100 cm → Green LED, no buzzer, OLED shows “SAFE”
- 40–100 cm → Yellow LED, no buzzer, OLED shows “WARNING”
- <40 cm → Red LED, buzzer ON, OLED shows “ALERT”
Confirm real-time response as objects/potential obstacles move
Optional Extensions
- In code, modify distance thresholds to obtain different detection ranges.
- Utilize more than one sensor in order to achieve a wider coverage area.
- Record distance data or set up mobile alerts as a part of the system.
- Adjust the mapping of LED brightness through the usage of an LDR (Light Dependent Resistor) output.