Smoke Detector using MQ-2 Gas Sensor & XIAO RP2040

759 views

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:

ComponentsValue / ModelQty
PCBPCB-GOGO
XIAORP20401
OLED Display ModuleSSD13061
LDR1
Resistor10kΩ1
Buzzer12v1
Jumper Wires1

XIAO RP2040 Pinout

XIAO-RP2040-Pinout

For details on the datasheet of the XIAO RP2040 Microcontroller visit this link.

Pin NoPin Name
1P26 / A0 / D0
2P27 / A1 / D1
3P28 / A2 / D2
4P29 / A3 / D3
5P6 / SDA/ D4
6P7 / SCL / D5
7P0 / TX / D6
8P1 / RX / CSN / D7
9P2 / SCK / D8
10P3 / MISO / D9
11P4 / MOSI / D10
123V3
13GND
145V

MQ-2 Gas Sensor Pinout

MQ2-smoke-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 RP2040RelayMQ-2 Gas Sensor
VCC5VVCC
GNDGNDGND
D0VIN
D1DOUT

(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.
  • 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.