DEV Community

Kolawole Yusuf
Kolawole Yusuf

Posted on

Building a Security System, with motion detection and time based settings using Arduino

Home security is a priority in the modern world and setting up a personalized do it yourself motion sensing security setup can enhance the safety of your residence and provide you with a sense of assurance. In this project, we will construct a Motion Sensitive Security System utilizing an Arduino Uno along with a 6-channel relay module that manages lighting and alerts triggered by motion detection. The special feature of this project involves adjusting the systems actions based on the time of day and whether its a weekday or weekend.

You can use a 2-channel relay module for this project. Opting for a relay module with more than 2 channels allows us to expand the project later in the future. We will be able to control more devices without having to replace the existing modules.

Here is an explanation of how the system is going to function

  1. If there is any motion sensed, between 19:00 and 6:00; the lights and alarms will activate automatically.
  2. During weekdays, from 6:00 and 18:00; the alarm will be activated if there is any movement detected.
  3. During weekends the alarm will relaxed from 8:00 and 18:00; however the lights will continue working after 19:00.

This initiative serves as a foundation, for tailoring home security setups and can be effortlessly enhanced to include smartphone alerts and advanced security features for both home and away modes.

  • Items Required:
  • Arduino Uno board
  • PIR motion sensor (for motion detection)
  • 6-channel relay module
  • Buzzer or siren (for alarm)
  • LED lights or 12V lights (to simulate external lighting)
  • RTC module (Real Time Clock for keeping track of time)
  • Jumper wires
  • Breadboard
  • Power source (Arduino-compatible)

Step 1. Setting up and wiring the circuits.

PIR Motion Sensor

  • Connect the VCC pin to the Arduino 5 volt pin.
  • Connect the sensors GND pin to the GND pin of the Arduino board.
  • Connect the signal pin to pin 2 on the Arduino (use any digital pin that is free, for use).

The 6 Channel Relay Module is a tool, for controlling multiple circuits or devices

6-Channel Relay Module:

  • Connect the VCC wire to the 5 volt pin, on the Arduino board.
  • Connect the ground (abbreviated as GND) of the sensor, to the ground (also labeled as GND) on the Arduino board.
  • Connect IN 01 to the pin 3 on the Arduino board, for controlling the lights.
  • Connect IN 1 and IN 3 to the pin 4 on Arduino, for alarm control.
  • Alarm/Buzzer
  • Connect to a relay channel in the 6-channel relay module
  • Lights
  • Connect to another replay channel on the 6-channel replay module
  • RTC Module (DS3231)
  • Connect the SDA pin to Arduino A4
  • Connect SCL pin to Arduni A5

Sept 2. Writing the Arduino code

  • Lets now develop the program that combines detecting movement with controlling the relays based on time intervals.
  • Open a new sketch in your Arduino IDE
  • Select your board and run the code below

You have the option to replicate the build process and run the code on tinkercard.com without requiring an Arduino board or motion sensor and RTC module.

#include <Wire.h>
#include <RTClib.h>
RTC_DS3231 rtc; //Real Time Clock object

int motionSensor = 2; //the PIR sensor input pin
int relayLight = 3; //the relay controlling the light
int relayAlarm = 4; //the relay controlling the alarm
bool motionDetected = false; //initial motion state

void setup() {

pinMode(motionSensor, INPUT);
pinMode(relayLight, OUTPUT);
pinMode(relayAlarm, OUTPUT);

if (!rtc.begin()) {
Serial.println("Couldn not find RTC"); //RTC might not be connected to the Arduino board
while (1);
}

if (rtc.lostPower()) {
//Set the current date and time
rtc.adjust(DateTime(F(DATE), F(TIME)));
}

Serial.begin(9600);

}

void loop() {
DateTime now = rtc.now();
int dayOfWeek = now.dayOfTheWeek(); //Sunday = 0, Monday = 1 and Saturday = 6

int hour = now.hour(); //Get the current hour
motionDetected = digitalRead(motionSensor) == HIGH;

if (motionDetected) {
//If current time is between 19:00 and 6:00, turn on light and alarm
if (hour >= 19 || hour < 6) {

digitalWrite(relayLight, HIGH); //Turn on the light
digitalWrite(relayAlarm, HIGH); //Turn on the alarm

}

//If it's between 8:00 and 18:00 on weekdays, only trigger the alarm
else if (hour >= 8 && hour < 18 && (dayOfWeek >= 1 && dayOfWeek <= 5)) {
digitalWrite(relayAlarm, HIGH); //Only turn on the alarm
}

}
else {
//Turn everything off when no motion is detected
digitalWrite(relayLight, LOW);
digitalWrite(relayAlarm, LOW);

}
delay(500); //Add a slight delay for debouncing

}

Enter fullscreen mode Exit fullscreen mode

How the code works

RTC integration: The RTC module retrieves the day of the week and the current hour, enabling us to set up actions depending on the time of day and whether it is a weekday or weekend.

Motion detection: When the PIR motion sensor senses/detects movement, Arduino then verifies the time, applies varying logic depending on the time of day.

Relay control: Is used for turning lights or the alarm system based on the time of day or day of the week.

Potential Future Developments

There are ways to boost the effectiveness and user friendliness of this DIY security system.

  1. Home/Away Mode: Set up the system on a display panel or mobile app, for the homeowner to set the system to home or away mode. In home mode (between 8:00 and 18:00) if there is motion detected, a notification will be sent to the user’s phone. If no response is received within a time period (like 3 minutes) the alarm will go off automatically. The away mode will operate as it currently does without any changes.

  2. Integrate a Wi-Fi module, like the ESP8266 to send notifications, to your smartphone when motion is detected while in home mode so you can respond before the alarm goes off.

  3. Include more sensors, like door/window sensors to boost the security system’s effectiveness, providing an additional safeguard to protect all access points effectively.

  4. Adding a surveillance camera for video recording or live streaming when motion is sensed could be useful for monitoring the property. The display panel or mobile app can automatically stream and switch to the camera where motion was detected.

By expanding the setup you can develop a reliable home security system tailored to your individual requirements.

Top comments (0)