Overview & Objective The tutorial guides you through building a basic yet effective home security alarm system using the Arduino Mega. It combines motion detection, visual and audible alerts, and serial output to monitor activity and trigger alarms—ideal for beginners building a simple security setup
Why Arduino Mega? Although the tutorial could also run on an Uno, the Mega provides extra I/O pins—useful if you want to expand with more sensors or outputs later. The key emphasis is on ease and flexibility.
Components Required
Arduino Mega
- Arduino Mega
PIR motion sensor (for detecting movement)
- PIR motion sensor (for detecting movement)
Relay module (for controlling high-voltage loads like AC bulbs)
- Relay module (for controlling high-voltage loads like AC bulbs)
LED with 220 Ω resistor (as a visual indicator)
- LED with 220 Ω resistor (as a visual indicator)
Jumper wires, solderless breadboard, 5 V power supply
- Jumper wires, solderless breadboard, 5 V power supply
AC bulb (optional, triggered via relay)
AC bulb (optional, triggered via relay)
Circuit Setup
Power connections: 5 V and GND from Mega to both the PIR sensor and relay module.
- Power connections: 5 V and GND from Mega to both the PIR sensor and relay module.
Sensor to board: PIR sensor’s output pin connects to digital pin D2 on the Mega.
- Sensor to board: PIR sensor’s output pin connects to digital pin D2 on the Mega.
Control outputs: LED on D9 (via resistor); Relay control on D10.
- Control outputs: LED on D9 (via resistor); Relay control on D10.
AC output wiring: Relay’s common (COM) and normally open (NO) contacts wired in series with the AC bulb and mains. The tutorial provides a clear schematic diagram to assist in wiring
- AC output wiring: Relay’s common (COM) and normally open (NO) contacts wired in series with the AC bulb and mains. The tutorial provides a clear schematic diagram to assist in wiring
Code Explanation
Setup function
Initializes the serial monitor at 9600 baud.
- Initializes the serial monitor at 9600 baud.
Input mode for PIR (D2); output modes for LED (D9) and relay (D10).
- Input mode for PIR (D2); output modes for LED (D9) and relay (D10).
Relay initialized HIGH to remain off until triggered
- Relay initialized HIGH to remain off until triggered
- Setup functionInitializes the serial monitor at 9600 baud.Input mode for PIR (D2); output modes for LED (D9) and relay (D10).Relay initialized HIGH to remain off until triggered
Main loop
Reads PIR value (val = digitalRead(2)
).
- Reads PIR value (
val = digitalRead(2)
).
Prints value to serial monitor for monitoring.
- Prints value to serial monitor for monitoring.
A short delay (100 ms) slows polling.
- A short delay (100 ms) slows polling.
If PIR output is HIGH (motion detected):
LED ON (digitalWrite D9 HIGH)
- LED ON (digitalWrite D9 HIGH)
Relay ON (digitalWrite D10 LOW) – energizing coil to close connection.
- Relay ON (digitalWrite D10 LOW) – energizing coil to close connection.
- If PIR output is HIGH (motion detected):LED ON (digitalWrite D9 HIGH)Relay ON (digitalWrite D10 LOW) – energizing coil to close connection.
Otherwise:
LED OFF and Relay OFF (D10 HIGH), cutting power
LED OFF and Relay OFF (D10 HIGH), cutting power
- Otherwise:LED OFF and Relay OFF (D10 HIGH), cutting power
- Main loopReads PIR value (
val = digitalRead(2)
).Prints value to serial monitor for monitoring.A short delay (100 ms) slows polling.If PIR output is HIGH (motion detected):LED ON (digitalWrite D9 HIGH)Relay ON (digitalWrite D10 LOW) – energizing coil to close connection.Otherwise:LED OFF and Relay OFF (D10 HIGH), cutting power
How It Works
When motion enters the PIR’s detection zone, its output goes HIGH.
- When motion enters the PIR’s detection zone, its output goes HIGH.
Arduino detects this, giving both visual feedback (LED ON) and triggering the relay.
- Arduino detects this, giving both visual feedback (LED ON) and triggering the relay.
The relay activates the AC bulb (or any connected load), serving as an alarm.
- The relay activates the AC bulb (or any connected load), serving as an alarm.
The serial monitor logs sensor output for debugging or oversight
- The serial monitor logs sensor output for debugging or oversight
Safety Notes & Best Practices
AC safety: Wiring AC circuits involves serious hazards. The tutorial advises caution when dealing with mains voltage.
- AC safety: Wiring AC circuits involves serious hazards. The tutorial advises caution when dealing with mains voltage.
Component tolerance: Use correct resistor values (220 Ω for LED) and a relay rated for the AC bulb’s voltage/current.
- Component tolerance: Use correct resistor values (220 Ω for LED) and a relay rated for the AC bulb’s voltage/current.
Scalability: The Mega’s extra I/O allows adding sensors like door contacts, multiple LEDs, or even GSM modules to send alerts.
- Scalability: The Mega’s extra I/O allows adding sensors like door contacts, multiple LEDs, or even GSM modules to send alerts.
Potential Enhancements
Add more motion sensors or integrate door/window sensors for broader coverage.
- Add more motion sensors or integrate door/window sensors for broader coverage.
Use a buzzer for audible alarms.
- Use a buzzer for audible alarms.
Log events with timestamps or SD cards.
- Log events with timestamps or SD cards.
Send notifications via Wi‑Fi or GSM modules.
- Send notifications via Wi‑Fi or GSM modules.
Introduce keypad input to arm/disarm the system.
- Introduce keypad input to arm/disarm the system.
Integrate smartphone alerts or IoT platforms for remote monitoring.
- Integrate smartphone alerts or IoT platforms for remote monitoring.
ConclusionTechatronic’s tutorial offers a practical introduction to creating a simple home alarm system with Arduino Mega using PIR motion detection, LED indicators, relay-switched loads, and serial monitoring. It balances clarity and expandability—ideal for newcomers, while allowing scaling to more advanced setups like IoT security or multi-sensor configurations.
Comments