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.
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 thanpause
. 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