Understanding MQ2 Gas Sensors for Arduino: 2 modes

Discover how MQ2 gas sensors work with Arduino and learn how to integrate them into your projects effortlessly for accurate gas detection.

A gas sensor is an electronic component designed to detect the presence and measure the concentration of specific gases in the surrounding environment. It’s like a little nose that can detect if there is something like smoke, carbon monoxide, or other gases present. These sensors are handy for keeping us safe by alerting us to potential dangers, like gas leaks or air pollution. They are used in lots of places, from homes to factories, to make sure the air we breathe is clean and safe.

What is an MQ2 gas sensor?

The MQ2 gas sensor is a popular type of gas sensor used in various applications. It’s particularly well-known for its ability to detect multiple gases, including methane, propane, alcohol, smoke, and carbon monoxide. The sensor works by detecting changes in resistance when it comes into contact with specific gases, allowing it to alert users to potential hazards such as gas leaks or the presence of smoke. The MQ2 sensor is commonly used in home automation systems, industrial safety equipment, and environmental monitoring devices to ensure the safety and well-being of people and the environment.

The MQ2 gas sensor can detect multiple gases but it does not directly identify the detected gas. It Usually operates at 5V DC. It provides an analog voltage output that varies depending on the concentration of the detected gas, allowing for easy interfacing with microcontrollers or analog-to-digital converters. It has a rapid response time, providing real-time detection of gases in the environment, and enabling quick alerts in case of gas leaks or other hazards. It requires a certain amount of time (usually a few minutes) to stabilize and provide accurate readings after being powered on.

How to use it with Arduino?

MQ2 gas sesnor

Here, I will share two basic examples to let you know how to use an MQ2 gas sensor with Arduino uno. In the first example, you will learn how to detect gas using analog output and operate a buzzer once the gas is detected. On the other hand in the second example you will learn how to operate a gas sensor with digital output and operate a buzzer accordingly once the gas is identified.

Example 1: Detect the presence of smoke in analog output mode

Required components

Circuit diagram

How to use gas sensor using arduino in analog output mode

According to the circuit connection, first place the MQ2 gas sensor on the breadboard. Connect the A1, A2, and H1 terminals of the gas sensor to the 5-volt power supply of the Arduino uno board respectively. After that connect one end of the 4.7 k ohm resistor to the B2 terminal of the gas sensor. Connect the B1 terminal to the analog pin A2 of the Arduino uno board. Connect the H2 terminal and the other end of the resistor to the GND of the Arduino uno board.

Now place the buzzer on the breadboard. Buzzr is a two-terminal device one is longer and the other one is shorter. Connect the longer terminal to PIN 8 to provide input and connect the shorter terminal to the GND of the Arduino uno board.

It was all about the circuit connection. Now let’s check out the coding part.

Code

According to the code, I set the threshold value, compared it with the real-time sensor value, and displayed the identified sensor value on the serial monitor. If the sensor value is greater than the threshold value, it means the smoke is detected and the buzzer generates the beep sound. Otherwise, the buzzer remains off, and a “No smoke” message will display on the serial monitor with the measured sensor value.

const int buzzer=8; //Connect buzzer to digital pin 8
const int sensor=A2; //Connect gas sensor to analog pin A2
const int smoke_threshold=400; //set to threshold value

void setup() 
{  
    Serial.begin(9600); //set the serial port to 9600
pinMode(buzzer, OUTPUT); //set buzzer as output
  pinMode(sensor, INPUT); //set gas sensor as input
   
}

void loop() {
 
int sensor_value=analogRead(sensor);  //read the gas sensor value at pin A2 
if(sensor_value>smoke_threshold) //compare sesnor value with threshold value
  
{
 tone(buzzer,1000,100); //ON the buzzer
  Serial.print("Smoke detected! sensor value:  "); //print on serial monitor
  Serial.println(sensor_value);
}
else
{
  noTone(buzzer); //OFF the buzzer
  Serial.print("No smoke!! sensor value:"); //print on serial monitor
  Serial.println(sensor_value);
}
delay(1000); //wait
}
  

Output

Example 2: Detect the presence of smoke in Digital mode

To make things easier for you to understand, here I am using the same electronic components as the first example.

Required components

Circuit diagram

digital mode Understanding MQ2 Gas Sensors for Arduino: 2 modes

As I mentioned earlier, all the electronic components used in digital mode are the same as first example. I used the analog pin A2 of Arduino uno as an input pin and connected it to the gas sensor’s B1 terminal in the first example. But in this digital mode, I used the pin digital pin 7 of the Arduino board as the input pin and connected it to the B1 terminal of the sensor. This is the only difference in both the example’s connection circuitry, the rest all the connections are the same as in the previous example.

Code

In this digital mode sensor will read the values in terms of 0 or 1. If the sensor detects 1 means smoke is around it and the buzzer will generate a beep sound, otherwise 0 means no smoke detected and the buzzer will remain OFF.

const int buzzer=8; //Connect buzzer to digital pin 8
const int sensor=7; //Connect the gas sensor to analog pin 7

void setup() 
{  
    Serial.begin(9600); //set the serial port to 9600
pinMode(buzzer, OUTPUT); //set buzzer as output
  pinMode(sensor, INPUT); //set gas sensor as input
   
}

void loop() {
 
int sensor_value=digitalRead(sensor);  //read the gas sensor value at pin A2 
if(sensor_value) //if sensor value is 1
  
{
 tone(buzzer,1000,100); //ON the buzzer
  Serial.print("Smoke deteted! sensor value:  "); //print on serial monitor
  Serial.println(sensor_value);
}
else //if sensor value is 0
{
  noTone(buzzer); //OFF the buzzer
  Serial.print("No smoke!! sensor value:"); //print on serial monitor
  Serial.println(sensor_value);
}
delay(1000); //wait
}
  

Output

Hope you learn some thing new today.

If you have any questions, let me know in the comments.

Leave a Reply

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