Listen to this Post
As part of my Raspberry Pi testing, me and my classmate Deivin P built a smoke detection system using Python to explore its capabilities in sensor integration and automation. This project helps detect smoke or LPG leaks and triggers immediate alerts, with my contribution being on the programming part.
🔹 How It Works:
✅ MQ-2 Sensor detects smoke/gas levels in the environment.
✅ Relay Activation to control safety measures (e.g., exhaust fans or alarms).
✅ Buzzer & LED Alerts for immediate on-site warnings.
✅ Real-Time Monitoring to ensure quick response in critical situations.
🔹 Technologies & Modules Used:
🖥️ Programming Language: Python
📦 Packages: RPi.GPIO, time
🔌 Hardware: Raspberry Pi, MQ-2 Gas Sensor, Relay Module, Buzzer, LED
Through this project, we tested the GPIO functionalities, real-time response, and automation potential of Raspberry Pi using Python. Looking forward to further improvements and optimizations!
You Should Know:
Here are some practical steps, commands, and code snippets to replicate or extend this project:
1. Setting Up Raspberry Pi GPIO:
Ensure the RPi.GPIO library is installed:
sudo apt-get update sudo apt-get install python3-rpi.gpio
2. Python Code for Smoke Detection:
import RPi.GPIO as GPIO
import time
<h1>Pin Setup</h1>
MQ2_PIN = 17
BUZZER_PIN = 18
LED_PIN = 27
GPIO.setmode(GPIO.BCM)
GPIO.setup(MQ2_PIN, GPIO.IN)
GPIO.setup(BUZZER_PIN, GPIO.OUT)
GPIO.setup(LED_PIN, GPIO.OUT)
try:
while True:
if GPIO.input(MQ2_PIN):
print("Smoke detected!")
GPIO.output(BUZZER_PIN, GPIO.HIGH)
GPIO.output(LED_PIN, GPIO.HIGH)
time.sleep(1)
else:
GPIO.output(BUZZER_PIN, GPIO.LOW)
GPIO.output(LED_PIN, GPIO.LOW)
time.sleep(0.1)
except KeyboardInterrupt:
GPIO.cleanup()
3. Real-Time Monitoring with Linux Commands:
Use `htop` to monitor Raspberry Pi’s resource usage:
sudo apt-get install htop htop
4. Automating Alerts with Cron Jobs:
Schedule a script to run at startup:
crontab -e
Add the following line to run your Python script on boot:
@reboot python3 /path/to/your_script.py
5. Testing Relay Module:
Use the `gpio` command to test relay functionality:
gpio -g mode 17 out gpio -g write 17 1 # Turn on relay gpio -g write 17 0 # Turn off relay
What Undercode Say:
This project demonstrates the power of Raspberry Pi in IoT and automation. By integrating sensors like MQ-2 and using Python for real-time monitoring, you can build robust safety systems. Extend this project by adding cloud integration for remote alerts or using machine learning to analyze gas patterns. Explore more Raspberry Pi projects at Raspberry Pi Projects.
For further learning, check out these resources:
References:
Reported By: Aakash Sk – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass ✅



