Working and interface of gas sensor with Arduino

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
Arduino1
Breadboard1
Jumper wiresas per requirement
LED1
MQ2 Gas sensor1
220ohm Resistor1

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.

Namevoltage use by the heaterGas detected by the sensor
MQ-25vcombustible gasses like  Methane, Butane, LPG, smoke
MQ-35vSensitive for Alcohol, Ethanol, smoke
MQ-45vSensitive for Methane, CNG Gas
MQ-55vNatural gas, LPG
MQ-65vLPG, butane gas
MQ-71.4v – 5v Carbon Monoxide
MQ-85vHydrogen Gas
MQ-91.5v – 5v
(depends on gas
Ex:- 1.5v only for carbon monoxide)
Carbon Monoxide, flammable gasses
MQ-1316vOzone
MQ-1355vBenzene, Alcohol, smoke
MQ-1365vHydrogen Sulfide gas
MQ-1375vAmmonia
MQ-1385vBenzene, Toluene, Alcohol, Acetone,
Propane, Formaldehyde gas, Hydrogen gas
MQ-2146vMethane, Natural gas
MQ-2166vLPG, i-butane, propane, methane, alcohol, smoke
MQ-303A0.9vAlcohol, Ethanol, smoke
MQ-306A0.9vLPG, butane gas
MQ-307A0.2v-0.9v Carbon Monoxide
MQ-309A0.2v-0.9vCarbon Monoxide, flammable gasses
MG-8116vCarbon Dioxide
AQ-1045vair quality
SP3S-AQ-25vAir Quality
AQ-35vsuitable
to detect cigarette smoke

Leave a Comment