Ardunio Smoke and Gas Sensor(MQ-2)

12,037

32

6

Hello everyone , I have got another super easy arduino tutorial, this time is on the MQ-2 gas and smoke detector. Mostly combustible gases, propane and hydrogen.Its very easy to connect and program. Just using the map function.

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: Parts List / Connection

The parts needed are :-
-Arduino micro controller
-MQ-2 sensor
-RGB LED indicator

The connection is easy as well.
The MQ-2 has gnd, vcc +5v, Do(digital out) and Ao(analog out). This is because it can work with both analog and digital signal, even though I worked with analog signal here but I can tell you that if you want to work with digital pins using this sensor you should use the arduino pwm pins if not it might not work.
See the image for connections.

Step 2:

So the code is simple to understand, the G lights when gas level is greater than 20 and less than or equal to 30, i started from 20 because i noticed that when you map it in an open space the initial values without the presence of combustible gas or smoke is often between 14-19, so safe so zone.

And the B lights up when gas level is greater than 30 and less than 60, the not so good zone. while the R lights up when gas level is greater than 60 and less then or equal to 90, so danger zone. This is my code and you can choose your range of values as you wish.

```//GAS sensor output pin to Arduino analog A0 pin#define R 2
#define G 7
#define B 5```
`#define MQ2 A0`
`int gaslevel;`
```void setup()
{
Serial.begin(9600); //Initialize serial port - 9600 bps```
```pinMode(MQ2,INPUT);
pinMode(R,OUTPUT);
pinMode(G,OUTPUT);
pinMode(B,OUTPUT);
}```
```void loop()
{
gaslevel=map(gaslevel,0,1023,0,255);

if(gaslevel > 20 && gaslevel <= 30){//gaslevel is greater than 20 and less than 30
digitalWrite(R,LOW);//green led is off
digitalWrite(B,LOW);//blue led is off
_delay_ms(500);//delay
digitalWrite(G,HIGH);//red led is on
_delay_ms(500);
}
else if(gaslevel > 30 && gaslevel <= 60){//gaslevel is greater than 30 and less than 60
digitalWrite(R,LOW);//red led is off
digitalWrite(G,LOW);//green led is off
_delay_ms(500);
digitalWrite(B,HIGH);//blue led is on

}

else if(gaslevel > 60 && gaslevel <= 90 ){//gaslevel is greater than 60 and less than 90
digitalWrite(G,LOW);//red led is off
digitalWrite(B,LOW);//blue led is off
_delay_ms(500);
digitalWrite(R,HIGH);//green led is on
}
else
{
digitalWrite(G,LOW);//red led is off
digitalWrite(B,LOW);//blue led is off
digitalWrite(R,LOW);//green led is off
}
Serial.println(gaslevel);//print values on serial monitor
_delay_ms(100);
}

```

Step 3: Test Video

you can also change your indicator, maybe a piezo buzzer or motor instead an LED. I made a minute video you can watch it and see how it works.Thank you and have a good day. :)

Recommendations

• Internet of Things Class

22,821 Enrolled

6 Discussions

Thanks for sharing, but i have one question:

The MQ-2 sensor needs 150mA, and a arduino pin can only provide max of 40mA.
Are you not afraid to burn your Arduino port?

Does the above coding calibrate the output because there is no coding for converting the analog read value into ppm provided. can u please tell what the output units displayed on the serial monitor .

can someone tell me how to use a motor with mq2 smoke sensor

So how sensitive would you say it is? Do you think it would detect smoke from downstairs neighbors?

2 replies

This will depend on how dense the smoke is and the sensor settings. if you set your sensor reading to start from a very low value 5 perhaps, then it is going to detect every little presence of smoke and combustible gas in the air around it. It is a very sensitive sensor, but more sensitive to some gases than others. :)