Ardunio Smoke and Gas Sensor(MQ-2)
by rmikel in Circuits > Arduino
14472 Views, 34 Favorites, 0 Comments
Ardunio Smoke and Gas Sensor(MQ-2)
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.
Parts List / Connection
-Arduino micro controller
-MQ-2 sensor
-RGB LED indicator
- Breadboard and jumpers
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.
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=(analogRead(MQ2)); 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); }
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. :)