A gas sensor is an electronic sensor that detects different types of gases present in its surrounding. In this article, we will learn the working of gas sensors, connection with Arduino, and coding with LED output.
How does the gas sensor work ?
Clean air
A gas sensor detects gas relies on the chemiresistor to conduct current. Tin dioxide (SnO₂), heated on high-temperature oxygen present in the atmosphere donor electrons in tin dioxide are attracted toward oxygen which is adsorbed on the surface of the sensing material. This prevents electric current flow. result resistance increase.
Presense of gas
In the presence of smoke(or any other) gases, the surface density of adsorbed oxygen decreases as it reacts with the reducing gases. Electrons are then released into the tin dioxide (SnO₂), allowing current to flow freely through the sensor. Result resistance decrease and that change is measured by the Arduino.
Material Requires
Name | Quantity |
Arduino | 1 |
Breadboard | 1 |
Jumper wires | as per requirement |
LED | 1 |
MQ2 Gas sensor | 1 |
220ohm Resistor | 1 |
Pinout of gas sensor
Mq-2 module has 4 pins, Vcc, Gnd, Digital output, and analog output. Most of the other modules of this series have similar pinouts.
Connection
For digital Output connect D0 to Arduino pin no. 8, For analog output connect A0 to Arduino pin A0.
Code
#define MQ2pin (0)
#define INDICATORled 9
#define PREHEATled 7
float sensorValue; //variable to store sensor value
void setup()
{
Serial.begin(9600); // sets the serial port to 9600
Serial.println("Gas sensor warming up!");
digitalWrite(PREHEATled, HIGH);
delay(20000); // allow the MQ-6 to warm up
digitalWrite(PREHEATled, LOW);
}
void loop()
{
sensorValue = analogRead(MQ2pin); // read analog input pin 0
Serial.print("Sensor Value: ");
Serial.print(sensorValue);
if(sensorValue > 300)
{
Serial.print(" | Smoke detected!");
digitalWrite(INDICATORled, HIGH);
}
else
{
digitalWrite(INDICATORled, LOW);
}
Serial.println("");
delay(2000); // wait 2s for next reading
}
Code explanation
The sketch starts by defining the Arduino pin to which the analog pin of the MQ2 gas sensor is connected. An indicator led to pin no.9 and another preheat duration indicator led to pin no. 7.
#define MQ2pin (0)
#define INDICATORled 9
#define PREHEATled 7
A variable called sensorValue is also defined to store sensor value.
float sensorValue; //variable to store sensor value
Here we enable serial communication at baud rate 9600 for serial output. Pre heat time of 20 seconds to warm up the sensor.
Serial.begin(9600); // sets the serial port to 9600
Serial.println("Gas sensor warming up!");
digitalWrite(PREHEATled, HIGH);
delay(20000); // allow the MQ-6 to warm up
digitalWrite(PREHEATled, LOW);
Read the sensor value and store it to sensorValue variable.
sensorValue = analogRead(MQ2pin); // read analog input pin 0
Serial.print("Sensor Value: ");
Serial.print(sensorValue);
When the gas concentration is high enough, the sensor usually outputs a value greater than 300. We can monitor this value using the if statement. And when the sensor value exceeds 300, we will display the ‘Smoke Detected!’ message and indicator led turn on. Indicator led remain off if the concentration of smoke is low.
if(sensorValue > 300)
{
Serial.print(" | Smoke detected!");
digitalWrite(INDICATORled, HIGH);
}
else
{
digitalWrite(INDICATORled, LOW);
}
Similarly, you can use other gas sensors to check different gas concentrations like alcohol detectors, LPG gas, etc.
Also, you can use a buzzer to make a smoke alarm or an LPG gas leakage alarm.
sensors series and gases they detect
There is a wide range of sensors available to detect different kinds of gases. Here I am sharing some of the most popular and easily available gas sensors along with their names, operational voltages, and the gas they detect.
Name | voltage use by the heater | Gas detected by the sensor |
MQ-2 | 5v | combustible gasses like Methane, Butane, LPG, smoke |
MQ-3 | 5v | Sensitive for Alcohol, Ethanol, smoke |
MQ-4 | 5v | Sensitive for Methane, CNG Gas |
MQ-5 | 5v | Natural gas, LPG |
MQ-6 | 5v | LPG, butane gas |
MQ-7 | 1.4v – 5v | Carbon Monoxide |
MQ-8 | 5v | Hydrogen Gas |
MQ-9 | 1.5v – 5v (depends on gas Ex:- 1.5v only for carbon monoxide) | Carbon Monoxide, flammable gasses |
MQ-131 | 6v | Ozone |
MQ-135 | 5v | Benzene, Alcohol, smoke |
MQ-136 | 5v | Hydrogen Sulfide gas |
MQ-137 | 5v | Ammonia |
MQ-138 | 5v | Benzene, Toluene, Alcohol, Acetone, Propane, Formaldehyde gas, Hydrogen gas |
MQ-214 | 6v | Methane, Natural gas |
MQ-216 | 6v | LPG, i-butane, propane, methane, alcohol, smoke |
MQ-303A | 0.9v | Alcohol, Ethanol, smoke |
MQ-306A | 0.9v | LPG, butane gas |
MQ-307A | 0.2v-0.9v | Carbon Monoxide |
MQ-309A | 0.2v-0.9v | Carbon Monoxide, flammable gasses |
MG-811 | 6v | Carbon Dioxide |
AQ-104 | 5v | air quality |
SP3S-AQ-2 | 5v | Air Quality |
AQ-3 | 5v | suitable to detect cigarette smoke |