Connecting MQ2 Gas Sensor Module to Arduino UNO

Connecting MQ2 Gas Sensor Module to Arduino UNO

MQ2 Gas Sensor?

MQ2 is one of the popular and most used gas sensors in MQ sensor series. It is a Metal Oxide Semiconductor (MOS) type Gas Sensor also known as Chemiresistors as the detection is based upon change of resistance of the sensing material when the Gas comes in contact with the material. Using a simple voltage divider network, concentrations of gas can be detected.

Specifications

Operating voltage 5V
Load resistance 20 KΩ
Heater resistance 33Ω ± 5%
Heating consumption <800mw
Sensing Resistance 10 KΩ – 60 KΩ
Concentration Scope 200 – 10000ppm
Preheat Time Over 24 hour

1 ppm equal to?

When measuring gases like CO2, O2, or methane, the term concentration is used to describe the amount of gas by volume in the air. Parts-per-million (ppm) is the ratio of one gas to another. For example, 1,000ppm of CO means that if you could count a million gas molecules, 1,000 of them would be of carbon monoxide and 999,000 molecules would be some other gases.

Connecting MQ2 Gas Sensor Module to Arduino UNO

It is very easy if you know how to use Arduino IDE. Wire Connections are as follows :

MQ2 NodeMCU
VCC 5V
GND GND
D0 Digital Pin #8
A0 Analog Pin#0

Code

Arduino Sketch is very simple & basically just keeps reading analog voltage on A0 pin. It also prints a message on serial monitor when smoke is detected.

#define MQ2pin (0)

float sensorValue;  //variable to store sensor value

void setup()
{
  Serial.begin(9600); // sets the serial port to 9600
  Serial.println("Gas sensor warming up!");
  delay(20000); // allow the MQ-6 to warm up
}

void loop()
{
  sensorValue = analogRead(MQ2pin); // read analog input pin 0
  
  Serial.print("Sensor Value: ");
  Serial.print(sensorValue);
  
  if(sensorValue > 300)
  {
    Serial.print(" | Smoke detected!");
  }
  
  Serial.println("");
  delay(2000); // wait 2s for next reading
}

The output on the serial monitor looks like:

serial moniter

Code Explanation:

The Arduino sketch starts by defining Arduino pin to which analog pin of MQ2 gas sensor is connected. A variable called sensorValue is also defined to store sensor value.

#define MQ2pin (0)
float sensorValue;  //variable to store sensor value

In setup function: we initialize serial communications with the PC and wait for 20 seconds to allow sensor to warm up.

Serial.begin(9600); // sets the serial port to 9600
Serial.println("Gas sensor warming up!");
delay(20000); // allow the MQ-6 to warm up

In loop function: the sensor value is read by analogRead() function and displayed on serial monitor.

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 value greater than 300. We can monitor this value using if statement. And when the sensor value exceeds 300, we will display ‘Smoke Detected!’ message.

  if(sensorValue > 300)
  {
    Serial.print(" | Smoke detected!");
  }
Hi, I'm Harshvardhan Mishra. Tech enthusiast and IT professional with a B.Tech in IT, PG Diploma in IoT from CDAC, and 6 years of industry experience. Founder of HVM Smart Solutions, blending technology for real-world solutions. As a passionate technical author, I simplify complex concepts for diverse audiences. Let's connect and explore the tech world together! If you want to help support me on my journey, consider sharing my articles, or Buy me a Coffee! Thank you for reading my blog! Happy learning! Linkedin

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top