Listen to this Post
As part of my Raspberry Pi testing, me and my classmate Aakash SK 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.
🔹 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
You Should Know:
Here are the steps, commands, and code snippets to replicate this project:
1. Setting Up Raspberry Pi:
- Ensure your Raspberry Pi is updated:
sudo apt update && sudo apt upgrade -y
- Install the required Python library for GPIO control:
sudo apt install python3-rpi.gpio
2. Connecting the MQ-2 Sensor:
- Connect the MQ-2 sensor to the Raspberry Pi GPIO pins.
- Use the following Python script to read sensor data:
import RPi.GPIO as GPIO import time</li> </ul> <h1>Set up GPIO</h1> GPIO.setmode(GPIO.BCM) MQ2_PIN = 4 # Replace with your GPIO pin number GPIO.setup(MQ2_PIN, GPIO.IN) try: while True: if GPIO.input(MQ2_PIN): print("Smoke detected!") else: print("No smoke detected.") time.sleep(1) except KeyboardInterrupt: GPIO.cleanup()3. Activating the Relay and Buzzer:
- Connect the relay module and buzzer to the GPIO pins.
- Modify the script to trigger the relay and buzzer when smoke is detected:
BUZZER_PIN = 17 RELAY_PIN = 27 GPIO.setup(BUZZER_PIN, GPIO.OUT) GPIO.setup(RELAY_PIN, GPIO.OUT)</li> </ul> try: while True: if GPIO.input(MQ2_PIN): print("Smoke detected! Activating alarm and relay.") GPIO.output(BUZZER_PIN, GPIO.HIGH) GPIO.output(RELAY_PIN, GPIO.HIGH) else: print("No smoke detected.") GPIO.output(BUZZER_PIN, GPIO.LOW) GPIO.output(RELAY_PIN, GPIO.LOW) time.sleep(1) except KeyboardInterrupt: GPIO.cleanup()4. Real-Time Monitoring:
- Use a web server or IoT platform like ThingsBoard to monitor sensor data in real-time.
- Install Flask for a simple web interface:
sudo apt install python3-flask
- Create a basic Flask app to display sensor data:
from flask import Flask, render_template import RPi.GPIO as GPIO</li> </ul> app = Flask(<strong>name</strong>) MQ2_PIN = 4 GPIO.setup(MQ2_PIN, GPIO.IN) @app.route('/') def index(): status = "Smoke detected!" if GPIO.input(MQ2_PIN) else "No smoke detected." return render_template('index.html', status=status) if <strong>name</strong> == '<strong>main</strong>': app.run(host='0.0.0.0', port=5000)What Undercode Say:
This Raspberry Pi-based smoke detection system is a practical example of integrating hardware and software for real-world applications. By leveraging Python and GPIO functionalities, you can create automated systems for safety and monitoring. Extend this project by adding IoT capabilities, integrating with cloud platforms, or using machine learning for predictive analysis.
For further learning, explore these resources:
Experiment with additional sensors, such as temperature or humidity sensors, to expand the system’s capabilities. Happy coding! 🚀
References:
Reported By: Deivin P – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass ✅Join Our Cyber World:



