Enhancing security and automation in homes and offices has never been more straightforward with today’s advancements in microcontroller technology. This project details the development of a Smart Motion-Activated Lighting and SMS Alert System using an Arduino Uno, a PIR sensor, and a GSM module. The system not only lights up a room when motion is detected but also sends an SMS alert to notify the user, providing an added layer of security and convenience.

Project Overview

The main objective of this project is to detect motion using a PIR sensor and respond by:

  • Turning on a light (bulb/LED).
  • Sending an SMS notification to the user’s phone.

Components Required

  • Arduino Uno: The microcontroller that manages the system logic.
  • PIR Sensor: Detects movement by measuring infrared radiation.
  • GSM Module: Sends SMS alerts to a designated phone number.
  • Relay Module: Controls the bulb’s power supply.
  • Bulb or LED: Illuminates when motion is detected.
  • Power Supply: Powers the Arduino and other components.

How It Works?

The PIR sensor detects motion by monitoring infrared radiation changes in its environment. Once motion is detected, the Arduino triggers the relay module to switch on the connected bulb. Simultaneously, the GSM module sends an SMS alert to notify the user of the motion event.

Circuit Diagram and Connections

 

PIR Sensor:

  • VCC → 5V on Arduino
  • GND → GND on Arduino
  • OUT → Digital Pin 2 on Arduino

GSM Module:

  • VCC → 5V or separate power source for better stability
  • GND → GND on Arduino
  • TX → RX on Arduino (Pin 7)
  • RX → TX on Arduino (Pin 8)

Relay Module:

  • IN → Digital Pin 3 on Arduino
  • VCC and GND connected to Arduino

Explanation Of Code

int calibrationTime = 30;

  • calibrationTime: The duration (in seconds) the PIR sensor takes to stabilize when powered on, as recommended by the datasheet.

    long unsigned int lowIn;
    long unsigned int pause = 5000;

    • lowIn: Stores the time when the PIR sensor's output goes low, marking the start of a motionless period.
    • pause: The time (in milliseconds) that the sensor output must remain low before assuming that motion has ceased.

    boolean lockLow = true;
    boolean takeLowTime;

    • lockLow: Prevents continuous triggering by ensuring the system responds only once per HIGH signal until the output goes LOW.
    • takeLowTime: A flag used to capture the time when the sensor output transitions to LOW.

    int pirPin = 2;
    int relayPin = 3;

    • pirPin: Digital pin of Arduino Uno connected to the PIR sensor output.
    • relayPin: Digital pin of Arduino Uno connected to the relay module that controls a connected device (e.g., light).

    #include <SoftwareSerial.h>
    SoftwareSerial gsm(7, 8);

    • The SoftwareSerial library allows communication on digital pins 7 (RX) and 8 (TX) with the GSM module, enabling SMS capabilities.
    void setup() {
    Serial.begin(9600);
    gsm.begin(9600);
    pinMode(pirPin, INPUT);
    pinMode(relayPin, OUTPUT);
    digitalWrite(pirPin, LOW);

    Serial.print("calibrating sensor ");
    for(int i = 0; i < calibrationTime; i++) {
    Serial.print(".");
    delay(1000);
    }
    Serial.println(" done");
    Serial.println("SENSOR ACTIVE");
    delay(50);
    }
    • Purpose: Initializes the serial communication, sets up pin modes, and calibrates the PIR sensor.
    • GSM Initialization: Starts communication with the GSM module at 9600 baud.
    • Calibration Routine: Waits for calibrationTime seconds, displaying progress in the Serial Monitor to allow the PIR sensor to adjust to its environment.

    if(digitalRead(pirPin) == HIGH) {
    digitalWrite(relayPin, HIGH);
    if(lockLow) {
    lockLow = false;
    Serial.println("---");
    Serial.print("motion detected at ");
    Serial.print(millis()/1000);
    Serial.println(" sec");

    // GSM SMS sending
    gsm.println("AT+CMGF=1");
    delay(100);
    gsm.println("AT+CMGS=\"+1234567890\""); // Replace with your phone number
    delay(100);
    gsm.print("Motion detected at ");
    gsm.print(millis()/1000);
    gsm.println(" sec");
    delay(100);
    gsm.write(26); // CTRL+Z to send the message
    delay(5000);

    delay(50);
    }
    takeLowTime = true;
    }

    When Motion is Detected:

    • Sets relayPin to HIGH, activating the connected device.
    • Prints a message to the Serial Monitor showing when motion was detected.
    • Sends an SMS alert using the GSM module.

    if(digitalRead(pirPin) == LOW) {
    if(takeLowTime) {
    lowIn = millis(); // Capture the time when the output goes LOW
    takeLowTime = false;
    }
    if(millis() - lowIn <= pause) {
    digitalWrite(relayPin, HIGH); // Keep the relay ON
    } else {
    digitalWrite(relayPin, LOW); // Turn the relay OFF after the pause duration
    if(!lockLow) {
    lockLow = true;
    Serial.print("motion ended at ");
    Serial.print(millis()/1000);
    Serial.println(" sec");
    }
    }
    }

    When No Motion is Detected:

    • Captures the time when the PIR sensor transitions to LOW.
    • Checks if the current time minus lowIn is less than pause. If so, it keeps the relay activated.
    • Turns off the relay if the motionless period exceeds pause, and prints a message when motion ends.

     

    Code

    //the time we give the sensor to calibrate (10-60 secs according to the datasheet)
    int calibrationTime = 30;        

     

    //the time when the sensor outputs a low impulse
    long unsigned int lowIn;        

     

    //the amount of milliseconds the sensor has to be low
    //before we assume all motion has stopped
    long unsigned int pause = 5000;  

     

    boolean lockLow = true;
    boolean takeLowTime;  

     

    int pirPin = 2;    //the digital pin connected to the PIR sensor's output
    int relayPin = 3;  //the digital pin connected to the relay switch

     

    // GSM module setup (connect TX and RX pins appropriately)
    #include <SoftwareSerial.h>
    SoftwareSerial gsm(7, 8); // RX, TX

     

    /////////////////////////////
    //SETUP
    void setup(){
      Serial.begin(9600);
      gsm.begin(9600); // Start GSM module communication
      pinMode(pirPin, INPUT);
      pinMode(relayPin, OUTPUT);
      digitalWrite(pirPin, LOW);

     

      //give the sensor some time to calibrate
      Serial.print("calibrating sensor ");
        for(int i = 0; i < calibrationTime; i++){
          Serial.print(".");
          delay(1000);
          }
        Serial.println(" done");
        Serial.println("SENSOR ACTIVE");
        delay(50);
      }

     

    ////////////////////////////
    //LOOP
    void loop(){

     

         if(digitalRead(pirPin) == HIGH){
           digitalWrite(relayPin, HIGH);   //activates the relay when motion is detected
           if(lockLow){  
             //makes sure we wait for a transition to LOW before any further output is made:
             lockLow = false;            
             Serial.println("---");
             Serial.print("motion detected at ");
             Serial.print(millis()/1000);
             Serial.println(" sec");
             
             // Send SMS when motion is detected
             gsm.println("AT+CMGF=1");    // Set GSM to text mode
             delay(100);
             gsm.println("AT+CMGS=\"+1234567890\""); // Replace with your phone number
             delay(100);
             gsm.print("Motion detected at ");
             gsm.print(millis()/1000);
             gsm.println(" sec");
             delay(100);
             gsm.write(26); // ASCII code for CTRL+Z to send the SMS
             delay(5000); // Wait for the message to be sent
             
             delay(50);
             }        
             takeLowTime = true;
           }

     

         if(digitalRead(pirPin) == LOW){      
           if(takeLowTime){
            lowIn = millis();          //save the time of the transition from high to LOW
            takeLowTime = false;       //make sure this is only done at the start of a LOW phase
            }
           //if the sensor is low for less than the given pause, keep the relay ON
           if(millis() - lowIn <= pause){
               digitalWrite(relayPin, HIGH); // keep relay ON as long as motion is detected within the pause period
           } else {
               digitalWrite(relayPin, LOW);  // turn relay OFF when no motion is detected for longer than pause
               if(!lockLow){  
                   lockLow = true;                        
                   Serial.print("motion ended at ");      //output
                   Serial.print((millis() - pause)/1000);
                   Serial.println(" sec");
                   delay(50);
               }
           }
         }
      }

    Leave a comment

    Please note, comments must be approved before they are published

    Your cart

    ×