How to make a Gas and Smoke leakage warning system using Arduino

How to make a Gas and Smoke leakage warning system using Arduino

Hello and welcome back. In this project, we will learn how to make a gas and smoke leakage warning system using Arduino. For this, I mainly used an MQ2 sensor to detect gas and smoke in the environment. This system is only for demonstration and educational purposes, so you can easily modify and improve it as you like. If you want to know more info about the MQ2 sensor, please use this link.

For this, I added buzzers and LEDs to give both sound and visual indications. In addition, I included an automatic control door in this project. When gas leakage is detected, the door will open automatically.  For this mechanism, I used an SG90 servo motor.  Specially, I designed a custom PCB for this project using JLCPCB to make the circuit more compact, reliable, and professional.

Ok, let’s do this project step by step. The required components are given below.

Disclosure: These Amazon links are Affiliate links. As an Amazon Associate, I earn from qualifying purchases.

Step 1

Firstly, identify these components.

Step 2

Secondly, let’s order PCBs for this project.

  • Click the “Instant Quote” button and upload the Gerber file, which you can download from the link below.
  • Gerber file – Download
  • For this project, I ordered five Blue PCBs. Next, select the build time and shipping method. Finally, click “Save to Cart” and complete the payment.

Step 3

Thirdly, let’s unbox the PCB package for this project.

Step 4

Next, solder all the components onto the PCB.

Step 5

Afterward, connect the Arduino Nano board and the MQ2 sensor to the PCB. Then, connect the Arduino board to your computer.

Step 6

Now, copy and paste the following program into the Arduino IDE.

#define RLED 4
#define GLED 5
#define Buzzer1 6
#define Buzzer2 7
#define Button 8
#define MQ2 A0

#include <Servo.h>

Servo myServo;

int gasValue = 0;
int threshold = 300;

bool alarmState = false;
bool doorOpened = false;

void setup() {
  pinMode(RLED, OUTPUT);
  pinMode(GLED, OUTPUT);
  pinMode(Buzzer1, OUTPUT);
  pinMode(Buzzer2, OUTPUT);
  pinMode(Button, INPUT_PULLUP);

  myServo.attach(3);

  // ⭐ Normal startup condition
  digitalWrite(RLED, LOW);
  digitalWrite(GLED, HIGH);
  digitalWrite(Buzzer1, LOW);
  digitalWrite(Buzzer2, LOW);

  myServo.write(0);  // Door closed

  Serial.begin(9600);
}

void loop() {

  gasValue = analogRead(MQ2);
  Serial.println(gasValue);

  // Gas detected
  if (gasValue > threshold && alarmState == false) {
    alarmState = true;
  }

  // 🔥 ALARM MODE
  while (alarmState == true) {

    if (digitalRead(Button) == LOW) {
      resetSystem();
      break;
    }

    digitalWrite(RLED, HIGH);
    digitalWrite(GLED, LOW);

    // Loud pulses
    for (int i = 0; i < 3; i++) {

      if (digitalRead(Button) == LOW) {
        resetSystem();
        return;
      }

      digitalWrite(Buzzer1, HIGH);
      digitalWrite(Buzzer2, HIGH);
      delay(200);

      digitalWrite(Buzzer1, LOW);
      digitalWrite(Buzzer2, LOW);
      delay(150);
    }

    delay(300);

    // Open door once
    if (doorOpened == false) {
      for (int pos = 0; pos <= 90; pos++) {

        if (digitalRead(Button) == LOW) {
          resetSystem();
          return;
        }

        myServo.write(pos);
        delay(15);
      }
      doorOpened = true;
    }
  }

  delay(100);
}


// Reset function
void resetSystem() {

  alarmState = false;

  digitalWrite(RLED, LOW);
  digitalWrite(GLED, HIGH);
  digitalWrite(Buzzer1, LOW);
  digitalWrite(Buzzer2, LOW);

  // Close door
  for (int pos = 90; pos >= 0; pos--) {
    myServo.write(pos);
    delay(15);
  }

  doorOpened = false;
  delay(300);
}
  • Next, select the board and port. After, click the upload button.

Step 7

Now, remove the USB cable and create a door structure. For that, use the image shown below as a reference.

Step 8

Afterward, install the servo motor and the PCB on the structure.

Step 9

Finally, connect an external 5V power supply to this system. Now, you can test this project using smoke or gas.

Ok, enjoy this project. The full video guide is given below. So, we hope to see you in the next project.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *