Arduino

Interfacing MQ-2 Gas Sensor with ESP32 Using Arduino IDE

Sensors are an important part of designing IoT based projects as they feed the data to the system. Microcontroller based IoT boards gained popularity because of their ability to interface different sensors and upload data to the cloud or generate an emergency email.

The board we are talking about is ESP32 which due to its limitless feature helps users to interface multiple sensors. Gas sensor is among the widely used sensors with ESP32 which can detect fire eruption or gas leakage inside a room. Let’s find out the possible way of interfacing MQ-2 gas sensor with ESP32.

MQ-2 Gas Sensor

MQ-2 is one of the widely available gas sensors with greater precision compared to others as it is a MOS (Metal Oxide Semiconductor) sensor. Sensors like these are known as Chemiresistors because their gas sensing is based upon the change in resistance value once exposed to gas particles.

MQ-2 sensor operates on 5V. It can detect gasses like LPG, Propane, Methane and Carbon Monoxide. It is important to note that MQ-2 sensors can check the presence of gasses but cannot identify them. Therefore, it’s best for measuring changes in gas density in a certain place and generating an output signal accordingly.

Following are some important highlights of MQ-2 sensor:

  • Operates at +5V
  • Analog output voltage: 0V to 5V
  • Digital Output Voltage: High or Low (0V or 5V) TTL Logic
  • MQ-2 can be used with both analog and digital sensors
  • Potentiometer is there to set the sensitivity
  • Can be used to detect LPG, Alcohol, Propane, Hydrogen, Carbon Monoxide and even methane

MQ-2 Pinout

MQ-2 sensor comes with four different pins:

  • Vcc: Power pin for gas detection sensor it can be connected to 5V.
  • GND: Ground pin of sensor connected to ESP32 GND pin.
  • Dout: Digital output pin indicates gas presence. It can output either in HIGH or LOW state like 1 and 0.
  • Aout: Analog output pin indicates gas presence in analog signal. Output data gives a continuous value between Vcc and GND based on the level of gas detected.

 

Interfacing MQ-2 with ESP32

MQ-2 sensor is an easy-to-use gas sensor which can give output in both analog and digital. Digital output only gives HIGH or LOW value indicating gas detection however here we will be using analog output which gives more detailed reading and helps to note gas level.

Analog pin output is proportional to gas concentration more is the gas available higher is the analog output value. It is important to note that the MQ-2 sensor has Op Amp with a high precision comparator (LN393) which takes the analog signal and digitizes it to be available at digital output of the sensor.

MQ-2 sensors can detect gas concentration ranging from 200 ppm to 10000 ppm. Here ppm denotes Parts-per-million which is a unit to indicate concentration of gas.

To interface MQ-2 with ESP32 follow the below pin configuration.

MQ-2 Pins with ESP32

MQ-2 sensors have three pins two of them are GND and Vcc while the third pin will be Aout which will give measured gas value in analog signal.

ESP32 PIN MQ-2 PIN
GND GND
Vin Vcc
GPIO 4 Aout

 

LED Pins with ESP32

We have connected a LED at GPIO 32 of ESP32. LED will indicate if gas concentration is increased from a certain threshold.

ESP32 PIN LED
GPIO 32 Vcc
GND GND

 Below is the circuit of ESP32 with a gas sensor and LED:

Code For Interfacing MQ-2 Gas Sensor with ESP32

int LED = 32;            /*LED pin defined*/
int Sensor_input = 4;    /*Digital pin 5 for sensor input*/
void setup() {
  Serial.begin(115200);  /*baud rate for serial communication*/
  pinMode(LED, OUTPUT);  /*LED set as Output*/
}
void loop() {
  int sensor_Aout = analogRead(Sensor_input);  /*Analog value read function*/
  Serial.print("Gas Sensor: ");  
  Serial.print(sensor_Aout);   /*Read value printed*/
  Serial.print("\t");
  Serial.print("\t");
  if (sensor_Aout > 1800) {    /*if condition with threshold 1800*/
    Serial.println("Gas");  
    digitalWrite (LED, HIGH) ; /*LED set HIGH if Gas detected */
  }
  else {
    Serial.println("No Gas");
    digitalWrite (LED, LOW) ;  /*LED set LOW if NO Gas detected */
  }
  delay(1000);                 /*DELAY of 1 sec*/
}

Here in the above code a LED is defined at pin 32 of ESP32 and its pin 4 is set to take input from Gas sensor. Next serial communication begins by defining baud rate. LED is set output using pinMode function.

In the loop part of the sketch first we will read the analog reading through the sensor and the read value will be printed. Next a threshold of 1800 is set if the value surpasses this threshold LED connected at pin 32 will turn ON.

Output

Serial monitor prints the read analog value. Here when the value is below threshold that is 1800 it will show No gas message, once the threshold is crossed Gas detected message will appear in the serial monitor.

LED OFF: No Gas

In normal condition no gas will be detected so LED will remain off.

LED ON: Gas Detected

Now we will apply butane gas using the cigarette lighter. LED will turn on once the gas value surpasses the threshold value.

Conclusion

MQ-2 is a gas detection sensor which can sense the gas leakage and generate signals accordingly. Using an ESP32 microcontroller board we can easily interface it and can use it as a fire alarm detector or can generate an emergency email notification. Here in this article, we connected ESP32 with the MQ-2 sensor using the three pins of the sensor. An LED is used for indication purposes once gas is detected.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.