Incorporating sensors into our homes can enhance safety by automatically monitoring for hazards. In this project, we’ll combine a PIR motion sensor and an MQ2 gas sensor with an Arduino to create a system that detects both gas leaks and motion. This setup can be especially useful in spaces like basements, kitchens, or garages, where a gas leak and unexpected movement might indicate a potential safety issue.
For those new to PIR sensors, check out PIR Sensor with Arduino Code to understand its basics and coding requirements. This project is ideal for anyone interested in creating a smarter home environment or gaining hands-on experience with gas and motion sensors.
Project Overview
This project integrates a PIR motion sensor to detect movement and an MQ2 gas sensor to monitor the air for smoke or gas leaks. When gas levels exceed a safe threshold or motion is detected, the system can activate a fan or buzzer. This approach adds a layer of safety to areas where unexpected gas leaks or movement could indicate a problem.
For additional details on setting up an automatic gas detection system, refer to my previous post on building an automatic gas detection and alarm system, which covers the MQ2 gas sensor in more depth.
Components Needed
- Arduino Uno
- MQ2 Gas Sensor – for detecting smoke and gases
- PIR Motion Sensor – to detect movement
- MOSFET or Relay Module – to control the fan or buzzer
- Fan or Buzzer – for ventilation and alert
- Jumper Wires and Breadboard
System Connections
- MQ2 Gas Sensor: Connect the analog output to Arduino’s analog pin (A0).
- PIR Sensor: Connect the output to a digital input pin (D2).
- Fan/Buzzer Control: Use a MOSFET or relay to control the fan or buzzer. For more on using MOSFETs with Arduino, see the IRF540N Guide for Arduino Users, which explains how to safely connect high-current loads.
Code for Motion and Gas Detection
Here’s sample code to bring the system to life:
In this code, the Arduino monitors the PIR and MQ2 sensors to detect motion and gas levels, respectively. If gas levels exceed the defined threshold, the system activates the fan or buzzer.
const int pirPin = 2; // Digital pin for PIR sensor
const int mq2Pin = A0; // Analog pin for MQ2 sensor
const int alertPin = 5; // Digital pin for fan/buzzer
const int gasThreshold = 300; // Set gas detection threshold
void setup() {
pinMode(pirPin, INPUT);
pinMode(alertPin, OUTPUT);
Serial.begin(9600);
Serial.println("Home Safety System Initialized");
}
void loop() {
int gasLevel = analogRead(mq2Pin);
int motionDetected = digitalRead(pirPin);
// Gas detection logic
if (gasLevel > gasThreshold) {
digitalWrite(alertPin, HIGH); // Turn on fan/buzzer
Serial.println("Warning: High gas level! Ventilation activated.");
} else {
digitalWrite(alertPin, LOW); // Turn off fan/buzzer
}
// Motion detection logic
if (motionDetected == HIGH) {
Serial.println("Motion detected! Alert triggered.");
digitalWrite(alertPin, HIGH); // Activate alert
delay(1000); // Keep alert on for a short period
}
delay(200); // Stability delay
}
How It Works
Gas Detection: The system uses an MQ2 sensor to detect gas concentrations. When gas levels rise above the safe threshold, the fan or buzzer activates, alerting occupants and ventilating the area.
For more information on using the MQ2 sensor, see Simplest DC Motor Controller with Arduino for guidance on controlling motors with Arduino.
Motion Detection: The PIR sensor detects movement. If any motion is detected, an alert is triggered, providing an extra layer of security. This is particularly useful for identifying unauthorized activity in high-risk areas.
Learn more about setting up and coding PIR sensors in PIR Sensor with Arduino Code.
Automatic Fan Control: The fan or buzzer is controlled using a MOSFET or relay, which allows for efficient and safe switching of higher currents. For a deeper dive into how to control high-power devices with Arduino, refer to IRF540N MOSFET Guide.
Circuit Diagram
The following shows the circuit diagram of Arduino-Based Smoke and Motion Detection System for Home Safety.
Testing and Adjusting Sensor Thresholds
Start by testing your system in a controlled environment to ensure it responds correctly. Adjust the gas threshold based on the desired sensitivity.
For more advanced applications, consider implementing a PID controller for precise control of the ventilation fan speed in response to gas levels. Check out my post on using Arduino as a PID controller to see how a PID system can improve response times and accuracy.
Enhancements and Additional Features
- Remote Alerts: Use an ESP8266 or similar Wi-Fi module to send notifications via a mobile app or email.
- Data Logging: Add an SD card module to store sensor readings over time for safety analysis.
- Temperature Monitoring: Integrate a temperature sensor and apply PID control to maintain safe room conditions, as demonstrated in Building a PID-Controlled Temperature System.
Conclusion
By combining gas and motion sensors, this project provides a robust solution for home safety monitoring. It’s an excellent example of how Arduino-based automation can enhance safety, with applications ranging from detecting gas leaks to alerting occupants to unexpected movements.
Whether you’re aiming to make a safer home or want a new hands-on project, this motion and gas detection system is both practical and informative. Be sure to check out how to build an automatic gas detection and alarm system for more advanced gas detection setups.