Skip to Content

How to use the MQ-4 Methane Gas Sensor With Arduino

How to use the MQ-4 Methane Gas Sensor With Arduino

In this tutorial, we will learn how to use the MQ-4 Methane Gas Sensor with Arduino to build a gas detection alarm system. This system will utilize an LED and a buzzer to alert us when methane gas is detected in the environment.

Gas sensors are essential in various applications, including industrial settings, home safety, and environmental monitoring. The MQ-4 gas sensor is specifically designed to detect methane gas, making it ideal for applications where methane leakage needs to be monitored.

To create our gas detection alarm system, we will connect the MQ-4 gas sensor to an Arduino board and program it to trigger the LED and buzzer when methane gas is detected. Let’s get started by gathering the required parts for this project.

Required Parts

Makerguides.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to products on Amazon.com. As an Amazon Associate we earn from qualifying purchases.

Below you will find the parts required for this project. If you already have a resistor set and some LEDs you will not need the suggested kit. Only a single red LED and a 220 Ohm resistor are needed for this specific project.

Arduino Uno

Dupont wire set

Dupont Wire Set

Half_breadboard56a

Breadboard

USB Data Sync cable Arduino

USB Cable for Arduino UNO

Resistor & LED kit

Passive Buzzer

MQ-4 Gas Sensor

Introducing the MQ-4 Gas Sensor

The MQ-4 Gas Sensor is a popular sensor used to detect methane gas. It is commonly used in gas leakage detection systems and can also detect alcohol, cooking fumes and cigarette smoke. It is very easy to use and can be nicely integrated with Arduino boards for various projects.

Features of the MQ-4 Gas Sensor

The MQ-4 Gas Sensor has the following features:

  • Operating Voltage: The sensor operates on a voltage range of 5V to 24V DC.
  • Heater Voltage: It requires a heater voltage of 5V ±0.2V.
  • Heater Resistance: The sensor has a heater resistance of 31Ω ±3Ω.
  • Power Consumption: Low power consumption (100mA)
  • Sensitivity: The MQ-4 gas sensor has a high sensitivity to methane (CH4) gas, making it suitable for detecting natural gas leaks.
  • Detection Range: The sensor has a detection range of 300 to 10,000 ppm (parts per million) for methane gas.
  • Warm-up Time: It requires a warm-up time of approximately 48 hours before it can provide accurate readings.
  • Response Time: The response time of the sensor is less than 10 seconds.
  • Recovery Time: The recovery time of the sensor is less than 30 seconds.
  • Operating Temperature: The sensor can operate within a temperature range of -10°C to 50°C.
  • Dimensions: The MQ-4 gas sensor module has dimensions of approximately 32mm x 20mm x 22mm.
  • Interface: It can be easily connected to microcontrollers or development boards using analog or digital pins.

Please note that these specifications may vary slightly depending on the manufacturer or specific model of the MQ-4 gas sensor.

Internal Workings of the MQ-4 Gas Sensor

In this section we have a quick look at the internal workings of the MQ-4 sensor. At the heart of the sensor is the gas sensing layer composed of a metal oxide (SnO2). When methane gas comes into contact with this layer, it reacts with the oxygen present on the surface of the SnO2. This results in a change in the material’s resistance. We measure this to detect the presence and concentration of the gas.

The picture below are taken from the datasheet of the MQ-4 sensor and show its components and internal circuitry.

Wrapped around the sensing layer (1) is a heater coil (4). This is an important component because the SnO2 layer needs to be heated for the chemical reaction with methane to occur. That’s why you will feel that the sensor gets warm.

Encasing these components is the tubular ceramic (5). It physically protects the sensing layer and the heater coil. It also helps to maintain a stable temperature within the sensor by providing some thermal insulation.

Finally, we have the electrodes (2, 3), made of gold (Au) and platinum (Pt). These are attached to the sensing layer. The electrodes measure the changes in the resistance of the sensing layer.

Sensitivity Characteristics of the MQ4 Gas Sensor

The MQ-4 gas sensor is primarily designed to detect methane (CH4). But it also has varying levels of sensitivity to a range of other gases including LPG (liquefied petroleum gas), hydrogen (H2), carbon monoxide (CO), alcohol, and smoke. The sensor’s sensitivity refers to the ratio of the sensor resistance in various gases to the sensor resistance in clean air. The following graph shows you the sensitivity characteristics for those gases.

For methane, you can see that the MQ-4 has an excellent sensitivity. This means we can accurately detect and measure changes in methane concentration in the atmosphere. Even small changes in the amount of methane will result in noticeable changes in the sensor resistance.

For LPG and hydrogen, the MQ-4 sensor we also note a fairly high sensitivity, although not as high as it is for methane. This means, while we can detect these gases, the readings might not be as accurate or as responsive as they would be for methane.

Finally, the MQ-4 sensor has a lower sensitivity to carbon monoxide, alcohol, and smoke. This means that while the sensor will react to these gases but the changes in resistance will be smaller.

Humidity and Temperature Dependency of the MQ-4 Gas Sensor

Note that the accuracy of the MQ-4 gas sensor is influenced not only by the presence of target gases but also by environmental factors such as humidity and temperature. The following graph shows the change in resistance depending on ambient temperature and humidity:

Humidity can affect the sensor’s performance, mainly because the water vapor can occupy the active sites on the tin dioxide (SnO2) sensing layer where gas detection reactions occur. At high humidity levels, the sensor’s sensitivity to methane decreases, which can lead to less accurate readings.

Temperature is another important factor when using the MQ-4 gas sensor. The MQ-4 sensor includes a built-in heater to maintain the necessary temperature for the SnO2 sensing layer. However, ambient temperature can still influence the sensor readings.

For accurate and stable readings a combination of the MQ-4 gas sensor with a temperature and a humidity sensor could be considered. Also note, that the MQ4 needs to warm up (remember the heating element) for at least 20 seconds before its readings become stable and reliable.

Other MQ Gas Sensors

The MQ-4 Gas Sensor is just one of the many gas sensors available in the MQ series. Each sensor in the series is designed to detect a specific gas. Below you will find a comparison between the MQ-4 Gas Sensor and some other commonly used MQ gas sensors:

  1. MQ-2 Gas Sensor: The MQ-2 Gas Sensor is designed to detect multiple gases, including methane, propane, butane, and alcohol. It has a wider detection range compared to the MQ-4 Gas Sensor, making it suitable for applications where detection of multiple gases is required.
  2. MQ-5 Gas Sensor: The MQ-5 Gas Sensor is specifically designed to detect natural gas and LPG (liquefied petroleum gas). It has a higher sensitivity to these gases compared to the MQ-4 Gas Sensor, making it ideal for gas leakage detection systems.
  3. MQ-6 Gas Sensor: The MQ-6 Gas Sensor is designed to detect LPG, butane, and propane gases. It has a similar sensitivity to methane as the MQ-4 Gas Sensor but is more focused on detecting these specific gases.
  4. MQ-9 Gas Sensor: The MQ-9 Gas Sensor is designed to detect carbon monoxide, flammable gases, and smoke. It has a wider detection range compared to the MQ-4 Gas Sensor and is commonly used in fire detection systems.

Note that we have a tutorial for a Air Pollution Monitoring and Alert System Using MQ-135 and another tutorial on the MQ3 Sensor & Arduino: Building An Alcohol Detector.

MQ-4 Gas Sensors Pinout

A typically available MQ-4 gas sensor module is shown in the image below. It usually comes with 4 pins A0, D0, VCC and GND.

Pinout of MQ-4 Gas Sensor
Pinout of MQ-4

Here is the summary of the methane gas sensor module pins.

Pin NamesPin DescriptionRemarks
VCCPositive Power5 V is the recommended power supply
A0Analog outputConnect this pin to an analog input of the Arduino
D0Digital OutputConnect to any GPIO pin of the Arduino
GNDGround  

On the backside of the module you will find a trimmer to adjust the sensitivity of the detection. There is also a power LED (red) that is lit when the module has power and a detection LED (green) that comes on when gas is detected.

Backside of MQ-4 gas sensor module
Backside of the MQ-4 gas sensor module

Testing the MQ-4 Gas Sensor

You can easily test the function of the module without an Arduino. Connect VCC and GND to a 5V power supply and the red power LED at the back of the module should come on. Now wait a few seconds (for stable readings >20 seconds). You will notice that the sensor module gets slightly warm. This is normal. There is a heating element inside that is needed for the sensor to work.

Testing the MQ-4 gas sensor
Testing the MQ-4 gas sensor

If you now place a lighter nearby and push the button to let the gas out (don’t ignite it!), the green detection LED at the back should come on. If not, adjust the trimmer at the back of the module.

In the next section, we will discuss how to connect the MQ-4 Gas Sensor with Arduino and write the necessary code to start detecting methane gas.

Connecting the Parts

In this section we connect all the part. Let’ start by connecting the power supply to the breadboard and then from there to the MQ-4 sensor (blue and read wires). Make sure the 5V is connected to VCC of the MQ-4. We then connect the data analog output (A0) of the MQ-4 with a yellow wire to Pin A0 (analog input) of the Arduino.

Connecting the buzzer, led and MQ-4 to the Arduino
Connecting the buzzer, led and MQ-4 to the Arduino

When connecting the buzzer make sure to connect the positive pole (marked with a faint (+) symbol) to Pin 3 of the Arduino (purple wire). Its other pin is connected to ground.

Finally the LED. We first connect the resistor the longer pin of the LED (Anode) and then the resistor to Pin 2 of the Arduino (green wire). The other LED pin needs to be connected to ground.

And that’s it. The wiring is complete. If you need more details, have a look at the complete wiring table below.

FromPinWire colorToPin
Arduino5VRedBreadboardPositive rail
ArduinoGNDBlueBreadboardNegative rail
MQ-4GNDBlueBreadboardNegative rail
MQ-4VCCRedBreadboardPositive rail
MQ-4Out/SignalYellowArduinoA0
BuzzerPositive (+)PurpleArduino~4
BuzzerNegative (-)BlueBreadboardNegative rail
ResistorAnyLEDAnode (long pin)
ResistorAny OtherGreenArduino~3
LEDCathode (short pin)BlueBreadboardNegative rail

In the next section we write the code for our gas detection system.

Writing the Arduino Code

In this section we will write the code for our a gas detection alarm system. We want to switch on the LED and sound the buzzer if the gas levels in the environment exceed a certain threshold. Below is the complete code and in the next section we will explain its parts.

const int ledPin = 2;
const int buzzerPin = 3;
const int sensorPin = A0;
const int threshold = 500;

void alarm_on() {
  digitalWrite(ledPin, HIGH);
  tone(buzzerPin, 5000);
  delay(1000);
  alarm_off();
}

void alarm_off() {
  digitalWrite(ledPin, LOW);
  noTone(buzzerPin);
  delay(500);
}

void setup() {
  Serial.begin(9600);
  pinMode(sensorPin, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(buzzerPin, OUTPUT);
}

void loop() {
  int sensorValue = analogRead(sensorPin);
  Serial.print("MQ-4:");
  Serial.println(sensorValue);
  (sensorValue > threshold) ? alarm_on() : alarm_off();
}

Constants

We begin by declaring the necessary constants.

const int ledPin = 2;
const int buzzerPin = 3;
const int sensorPin = A0;
const int threshold = 500;

First we define ledPin, which represents the pin number to which the LED is connected. buzzerPin is the pin to which the buzzer is connected. Then we need a constant for the sensorPin, where the MQ-4 sensor is connected to. And finally we define a threshold that specifies the gas level the alarm system becomes active.

Alarm Functions

Next we define two functions: alarm_on() and alarm_off(). These functions control the behavior of the alarm system. The alarm_on() function is responsible for turning on the alarm.

void alarm_on() {
  digitalWrite(ledPin, HIGH);
  tone(buzzerPin, 5000);
  delay(1000);
  alarm_off();
}

In there we first set the LED pin to HIGH, turning on the LED. Then we activate the buzzer by generating a tone with a frequency of 5000 Hz. We wait for 1000ms = 1s and finally call alarm_off() to turn off the alarm. This will result in a beeping sound as long as we detect high gas levels.

void alarm_off() {
  digitalWrite(ledPin, LOW);
  noTone(buzzerPin);
  delay(500);
}

In the alarm_off() function, we first set the LED pin to LOW, turning off the LED. Then we stop the buzzer by calling the noTone() function. And finally we wait for 500ms = 0.5 seconds.

Setup Function

In the setup() function we initialize the communication and the IO modes of the pins. Noting exciting. Just make sure that the sensor pin is in INPUT mode.

void setup() {
  Serial.begin(9600);
  pinMode(sensorPin, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(buzzerPin, OUTPUT);
}

Loop Function

In the loop() function we continuously read the analog value from the MQ-4 sensor and trigger the alarm if the sensor value exceeds the threshold.

void loop() {
  int sensorValue = analogRead(sensorPin);
  Serial.print("MQ-4:");
  Serial.println(sensorValue);
  (sensorValue > threshold) ? alarm_on() : alarm_off();
}

The only interesting part here is the line (sensorValue > 500) ? alarm_on() : alarm_off(), where we use a ternary conditional operator to check if the sensor value is greater than the threshold. If it is, the alarm_on() function is called to activate the alarm. Otherwise, the alarm_off() function is called to turn off the alarm.

This loop continues indefinitely, continuously monitoring the sensor value and triggering the alarm as necessary.

Running the code

If you open the Serial Monitor and run this code, you will see a line with the label “MQ-4” appearing. When you then expose the MQ-4 sensor to methane gas (e.g. gas from a lighter), you should note a sudden increase of the sensor value and then a slower drop-off. See the picture below, where I did just that.

Serial Plotter output with gas detection peak
Serial Plotter output with gas detection peak

Congratulations, you now have a working gas detection alarm system! Enjoy playing with it ; )

Conclusion

In this tutorial, we have learned how to use the MQ-4 Methane Gas Sensor with Arduino. We started by introducing the required parts for this project, which include an Arduino board, the MQ-4 gas sensor, a buzzer, an LED, jumper wires, and a breadboard.

Next, we discussed the MQ-4 gas sensor in detail, explaining its working principle and its ability to detect methane gas. We also highlighted some of the key features of this sensor.

After that, we moved on to the practical aspect of the project and explained how to connect the parts together. We provided a clear wiring diagram and step-by-step instructions to ensure a successful connection between the MQ-4 gas sensor and the Arduino board.

Once the hardware setup was complete, we wrote the Arduino code. We explained how to use the analog input of the Arduino to read the sensor values and raise an alarm when the gas concentration exceed a given threshold.

In conclusion, the MQ-4 Methane Gas Sensor is a useful tool for detecting methane gas leaks or monitoring methane levels in various applications. By following this tutorial, you should now have a good understanding of how to use this sensor with Arduino and how to interpret the sensor readings.

Remember to always exercise caution when working with gas sensors and ensure proper ventilation in the testing environment. Safety should be a top priority when dealing with potentially hazardous gases.

We hope this tutorial has been helpful in getting you started with the MQ-4 Methane Gas Sensor. If you have any further questions or need additional assistance, please refer to the Frequently Asked Questions section or reach out to us through the provided links.

Happy experimenting and stay safe!

Frequently Asked Questions

Here are some commonly asked questions about using the MQ-4 Methane Gas Sensor with Arduino:

Q1: How does the MQ-4 Gas Sensor work?

The MQ-4 Gas Sensor works on the principle of gas conductivity. It contains a sensing element made of tin dioxide (SnO2) that reacts with methane gas. When methane gas is present, the conductivity of the sensing element increases, which can be measured by the Arduino.

Q2: Can the MQ-4 Gas Sensor detect other gases besides methane?

While the MQ-4 Gas Sensor is primarily designed to detect methane gas, it can also detect other flammable gases such as propane and butane. However, it may not be as accurate or sensitive for gases other than methane.

Q3: How accurate is the MQ-4 Gas Sensor?

The accuracy of the MQ-4 Gas Sensor can vary depending on various factors such as calibration, environmental conditions, and the concentration of the gas being detected. It is recommended to calibrate the sensor periodically for better accuracy.

Q4: How can I calibrate the MQ-4 Gas Sensor?

To calibrate the MQ-4 Gas Sensor, you can expose it to a known concentration of methane gas and adjust the sensitivity potentiometer until the sensor provides the desired response. It is important to follow the manufacturer’s guidelines for calibration.

Q5: Can I use the MQ-4 Gas Sensor for safety applications?

While the MQ-4 Gas Sensor can detect flammable gases, it is not certified for safety applications. It is primarily intended for educational, research, and prototyping purposes. For safety-critical applications, it is recommended to use certified gas sensors that meet the required standards.

Q6: Can I use the MQ-4 Gas Sensor with ESP32?

Yes, the MQ-4 Gas Sensor can be used with ESP32 as well. The wiring and code will be similar to using it with Arduino. However, make sure to check the voltage levels and pin compatibility between the sensor and the ESP32 board.

Q7: Can I run the Gas Detection System on Batteries

It is possible but requires rather large batteries and I would not recommend it. The MQ-4 sensor consumes about 100mA and needs to be warmed up for at least 30 seconds for reliable readings. This means even if we put the Arduino into deep-sleep to preserve energy and wake up only every 5 minutes, we can’t avoid that the MQ-4 will consume most of the energy.

Remember to refer to the datasheet and documentation of the MQ-4 Gas Sensor for more detailed information and specifications.

Links

Below some other useful links regarding the MQ-4 sensor and how to use it with an Arduino.