Roboelectrixx

LPG Gas Leakage Detector using MQ2 sensor and controller using SG90 Servo and Arduino

This is a simple project with one sensor used to detect the Gas Leak and one motor used to indicate the control. The microcontroller used is of course the simplest, Arduino Uno. In this project we’ll see how to use MQ2 sensor and how to control SG90 Servo motor and also use a buzzer and an LED light to ring an alarm and indicate the gas leak using LED.

Working of LPG Gas Leakage Detector

The working of the project is quite intuitive. When the Gas Leak Sensor, MQ2 sensor detects any gas leakage, it immediately changes the position of the servo motor; this is to indicate that the valve is being closed to avoid any further leakage. Along with this, the user has to be notified, so an alarm starts ringing. This is using the buzzer. And to indicate with light, a red LED is used to indicate that there is a leak.

LPG Gas Leakage Detector

Components required

MQ2 sensor

MQ2 sensor is a popularly used gas detecting or smoke detecting sensor. This sensor is capable of sensing LPG, Alcohol, Smoke, Hydrogen, Methane, Propane and Carbon Monoxide concentrations in the Air. Hence, we can use this sensor to detect the LPG gas leakage and program Arduino to buzz an alarm if it crosses some threshold.

Circuit Connections

Circuit Connection for LPG Gas Leakage Detector using MQ2 sensor, SG90 Servo and Arduino

The circuit connections are as shown in the circuit diagram above. Connect the 5V pin of Arduino to VCC pin of MQ2 sensor and also to red wire of Servo. Connect GND pin of Arduino to servo black wire, GND pin of MQ2 sensor, Negative lead of LED (through a current limiting resistor) and to negative lead of buzzer. Connect positive lead of buzzer to pin 12 of Arduino. Connect the positive lead of LED to pin 11 of Arduino. Connect the control pin of Servo, Orange wire to pin 8 of Arduino. And finally, connect pin A0 of Arduino to A0 of MQ2 sensor.

Code Explanation

#include<Servo.h>  //include Servo header file
Servo servo;
int smokeA0=A0;
int buzzer =11;
int led = 12;
float sensorValue;
/* Initialize the pins A0, 11, 12 and create a float datatype to store sensor value*/
void setup() {
  pinMode(buzzer,OUTPUT);
  pinMode(led, OUTPUT);
  pinMode(smokeA0,INPUT);
   Serial.begin(9600); // sets the serial port to 9600
  Serial.println(“Gas sensor warming up!”);
  delay(20000); // allow the MQ-6 to warm up
  servo.attach(8);
  servo.write(0);
  delay(2000);
  /* In void setup, buzzer, led and A0 pins are declared as ouputs and input respectively. Baud rate is set to 9600 to display messages in serial monitor. Pin 8 is for servo and it is made sure that it is at 0 degrees. */
}
 
void loop() {
  sensorValue=analogRead(smokeA0);
  if(sensorValue > 600)
  {
    Serial.print(” | Smoke detected!”);
    digitalWrite(buzzer,HIGH);
    digitalWrite(led,HIGH);
    servo.write(90);
    delay(1000);
/* In void loop function, we read the value from smoke sensor by using the analogRead function. Then, if the value is greater that a threshold (600) we display that smoke is detected and hence buzzer, led Is turned ON and the servo is turned to 90 degrees. A delay of 1ms is created. */
  }
  else
  {
     Serial.print(” | Smoke  not detected!”);
 
    digitalWrite(buzzer, LOW);
    digitalWrite(led,LOW);
    servo.write(0);
    delay(1000);
 /*  if the above condition is not satisfied then, we display that smoke is not detected and hence buzzer, led Is turned OFF and the servo is turned to 0 degrees which was its original position. A delay of 1ms is created. */
  }
  delay(2000); // wait 2s for next reading
}

Code

#includeServo.h
Servo servo;
int smokeA0=A0;
int buzzer =11;
int led = 12;
float sensorValue;
void setup() {
  pinMode(buzzer,OUTPUT);
  pinMode(led, OUTPUT);
  pinMode(smokeA0,INPUT);
   Serial.begin(9600);  sets the serial port to 9600
  Serial.println(Gas sensor warming up!);
  delay(20000);  allow the MQ-6 to warm up
  servo.attach(8);
  servo.write(0);
  delay(2000);
}
void loop() {
  sensorValue=analogRead(smokeA0);
  if(sensorValue  600)
  {
    Serial.print(  Smoke detected!);
    digitalWrite(buzzer,HIGH);
    digitalWrite(led,HIGH);
    servo.write(90);
    delay(1000);
  }
  else
  {
     Serial.print(  Smoke  not detected!);
    digitalWrite(buzzer, LOW);
    digitalWrite(led,LOW);
    servo.write(0);
    delay(1000);
  }
  delay(2000);  wait 2s for next reading
}