Overview
Smoke detectors are crucial devices that play a pivotal role in ensuring the safety of homes and workplaces. They serve as early warning systems, detecting the presence of smoke or potentially harmful gases in the air. In this article, we’ll explore the creation of a smoke detector using the MQ-2 gas sensor and XIAO RP2040 microcontroller, providing an affordable and efficient solution for detecting smoke and various combustible gases.
In today’s tutorial, we are going to make a “Smoke Detector” using the XIAO RP2040 Microcontroller & MQ-2 Gas Sensor.
What is a Smoke Detector?
A smoke detector is an electronic device designed to sense the presence of smoke or combustion products in the air. It operates on the principle of identifying particles or gases that indicate the presence of a fire or potential hazard, triggering an alarm to alert individuals within the vicinity. Smoke detectors are crucial components of fire safety systems in residential, commercial, and industrial settings, serving as early warning systems to mitigate the risks associated with fires.
PCBGOGO has offered high-quality PCBs and the best PCB assembly service all over the world since 2015. Discover an incredible offer that puts PCB prototyping within reach for just $1. Yes, you read that right – for only a dollar, you can bring your innovative ideas to life with a PCB prototype. Don’t miss out on this fantastic opportunity to turn your electronics projects into reality. Explore the campaign here: PCBGOGO $1 PCB Prototype and pave the way for your next big creation!
Hardware Components
You’ll need the following hardware components to get started:
Components | Value / Model | Qty |
---|---|---|
PCB | PCB-GOGO | |
XIAO | RP2040 | 1 |
OLED Display Module | SSD1306 | 1 |
LDR | – | 1 |
Resistor | 10kΩ | 1 |
Buzzer | 12v | 1 |
Jumper Wires | – | 1 |
XIAO RP2040 Pinout
For details on the datasheet of the XIAO RP2040 Microcontroller visit this link.
Pin No | Pin Name |
---|---|
1 | P26 / A0 / D0 |
2 | P27 / A1 / D1 |
3 | P28 / A2 / D2 |
4 | P29 / A3 / D3 |
5 | P6 / SDA/ D4 |
6 | P7 / SCL / D5 |
7 | P0 / TX / D6 |
8 | P1 / RX / CSN / D7 |
9 | P2 / SCK / D8 |
10 | P3 / MISO / D9 |
11 | P4 / MOSI / D10 |
12 | 3V3 |
13 | GND |
14 | 5V |
MQ-2 Gas Sensor Pinout
Steps-by-Step Guide
(1) Setting up Arduino IDE
Download Arduino IDE Software from its official site. Here is a step-by-step guide on “How to install Arduino IDE“.
(2) XIAO RP2040 in Arduino IDE
There’s an add-on that allows you to program the XIAO RP2040 using the Arduino IDE. Here is a step-by-step guide on “How to Install XIAO RP2040 on Arduino IDE“.
(3) Include Libraries
Before you start uploading a code, download and unzip the Wire.h, Adafruit_GFX.h, Adafruit_SSD1306.h library at /Program Files(x86)/Arduino/Libraries (default). Here is a step-by-step guide on “How to Add Libraries in Arduino IDE“.
(4) Schematic
Make connections according to the circuit diagram given below.
Wiring / Connections
XIAO RP2040 | Relay | MQ-2 Gas Sensor |
---|---|---|
VCC | 5V | VCC |
GND | GND | GND |
D0 | VIN | |
D1 | DOUT |
(5) Uploading Code
Now copy the following code and upload it to Arduino IDE Software.
// Circuits DIY
// For Complete Details Visit -> https://www.circuits-diy.com/smoke-detector-using-mq-2-gas-sensor-xiao-rp2040/
int Relay = D0; // for 12volt buzzer
int Sensor = A1; // MQ2 smoke sensor
int sensorThres = 370; // Your sensor threshold value
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
pinMode(Relay, OUTPUT);
pinMode(Sensor, INPUT);
Serial.begin(9600);
}
void loop() {
int analogSensor = analogRead(Sensor);
Serial.print("Pin A1: ");
Serial.println(analogSensor);
// Checks if it has reached the threshold value
if (analogSensor > sensorThres)
{
digitalWrite(LED_BUILTIN, HIGH);
digitalWrite(Relay,HIGH);
delay(3000);
}
else
{
digitalWrite(LED_BUILTIN, LOW);
digitalWrite(Relay,LOW);
}
delay(100);
}
nt Relay = D0; // for 12volt buzzer
int Sensor = A1; // MQ2 smoke sensor
int sensorThres = 370; // Your sensor threshold value
Relay
: Represents the pin for a 12-volt buzzer or relay.Sensor
: Indicates the pin connected to the MQ-2 smoke sensor.sensorThres
: Stores the threshold value for the sensor to trigger the alarm.
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
pinMode(Relay, OUTPUT);
pinMode(Sensor, INPUT);
Serial.begin(9600);
}
- Sets the pin modes for LED, Relay, and Sensor.
- Begins serial communication at a baud rate of 9600.
void loop() {
int analogSensor = analogRead(Sensor);
Serial.print("Pin A1: ");
Serial.println(analogSensor);
// Checks if it has reached the threshold value
if (analogSensor > sensorThres)
{
digitalWrite(LED_BUILTIN, HIGH);
digitalWrite(Relay,HIGH);
delay(3000);
}
else
{
digitalWrite(LED_BUILTIN, LOW);
digitalWrite(Relay,LOW);
}
delay(100);
}
- Reads the analog value from the sensor using
analogRead(Sensor)
. - Prints the sensor reading to the Serial Monitor.
- If the sensor reading surpasses the predefined threshold (
sensorThres
), it:- Turns on the built-in LED (
LED_BUILTIN
). - Activates the relay or buzzer (
Relay
) for 3 seconds.
- Turns on the built-in LED (
- If the sensor reading is below the threshold, it turns off the LED and relay.
This code continuously monitors the analog sensor readings and triggers an alarm (LED and relay activation) if the sensor detects smoke or gases above the specified threshold value (sensorThres
). Adjustments to sensorThres
might be necessary based on environmental conditions or specific gas levels for accurate detection.
Applications
- Residential Fire Safety
- Commercial Buildings and Offices
- Industrial Safety
- Laboratory and Research Facilities
- Smart Home and IoT Integration
Conclusion
Creating a smoke detector using the MQ-2 gas sensor and XIAO RP2040 microcontroller offers an accessible and cost-effective solution for monitoring air quality and detecting potential hazards. This project not only enhances safety but also demonstrates the practical use of IoT and sensor technology in everyday life. With further enhancements and customization, this smoke detector can be tailored to meet specific needs and provide a reliable means of early detection, potentially preventing disasters caused by gas leaks or smoke.