Overview
MQ-4 sensors detect the presence of methane gas (CH4) in the air. Methane gas is the largest component of natural gas. There are many applications for detecting CH4, such as detecting rotting foods (especially produce). This tutorial walks through the process of connecting an MQ-4 sensor to an Arduino Uno microcontroller (or equivalent – like an Elegoo) and printing out an analog output representing the methane concentration in the air. It also uses a digital output to trigger a red LED light to illuminate when methane levels reach and exceed a threshold.
What is an MQ-4 Sensor and How Does it Work?
An MQ-4 sensor detects the concentration of methane/natural gas in the air (in ppm, or parts-per-million). The analog pin of the sensor generates an analog signal proportional to the amount of gas in the air (through what is called signal conditioning), and outputs the reading as an analog voltage. There is also a digital output (DO) pin of the sensor that can be used for different activities commanded by the Arduino.
Note that the way this specific MQ-4 sensor comes, the potentiometer on the underside of it is preset based on a certain threshold, triggering a digital output of HIGH (1) or LOW (0).
In this tutorial, we will set it up so that when methane gas is detected (and the digital pin is HIGH), an external LED lights up.
While there are inbuilt LEDs on the bottom side of this MQ-4 sensor, they are very small and dim and difficult to see – especially if the sensor is hooked up to a complex circuit or even positioned so that the underside is covered.
Pin Configuration
The MQ-4 sensor has four types of pins. The table below shows each of the unique pins, their functions, and the corresponding pins they will connect to on the Arduino.
Sensor Pin | Sensor Pin Function | Arduino Pin |
---|---|---|
AO | Generates an analog signal proportional to the intensity of methane detected in the air | Analog pin 0 |
DO | Generates a digital signal with a limit set using a potentiometer | Digital pin 8 |
GND | Reference potential (the “ground”) | GND |
VCC | Positive power supply | 5V |
Parts list
- Arduino Uno (or equivalent)
- Arduino-to-USB connector cable
- Power supply of 5V
- Breadboard
- Red LED bulb
- 220-ohm resistor
- Seven jumper wires
- MQ-4 sensor
Tutorial
The circuit diagram below shows how the parts should be configured.
I recommend following these steps – divided into three main parts – in order.
Part I: Connect the MQ-4 Sensor and Arduino
We start by connecting the MQ-4 and the Arduino.
- Start by connecting the MQ-4 sensor to the breadboard. Its four pins should fit easily into four adjacent holes. On the back of the sensor, there are labels for what the four different pins are – take note of the order, as this will be important for connecting the wires. You can always look at the back of the sensor for reference as you are assembling the circuit!
- Now, we move on to connecting the four different pins. Start with the analog output. In the same row on the breadboard that the sensor’s A0 pin is set, place a blue wire in a hole in the same row on the breadboard. Connect the other end of the wire to the “A0” pin on the Arduino.
- Next, we move to connecting with the “DO” pin – the digital output. Using a wire (shown as green in the diagram), place one end in a hole in the same row as the DO pin o the sensor. Connect the other end of the green wire to digital pin 8.
- Then, we set up a connection to the ground pin (“GND”) of the sensor. Use a wire (shown as white in the diagram), and place one in in a whole in the same row on the breadboard that the sensor’s GND pin is set, and place the other end the negative rail of the breadboard.
- The last pin on the sensor is “VCC” – this will connect to power. Connect one end of a wire (shown as red in the diagram) into a hole in the same row on the breadboard as the VCC pin of the sensor, and place the other end in the positive rail of the breadboard. We will be adding one more thing to this circuit – and LED light, so we do not want to close the circuit here.
Part II: LED
Now we will move on to connecting a red LED that will light up when the methane detected by the sensor exceeds a threshold.
- Start by placing a red LED into the breadboard. I suggest placing it further over to the left of the sensor (as shown in diagram) so that the wiring won’t get too tangled. Place the LED so that the shorter end ()the anode) is in a breadboard hole to the left; you will put the longer end (the cathode) directly to the right of it in the same column.
- Connect a 220-ohm resistor. Place one end of the resistor in a hole in the same row as the LED’s cathode and place the other end in the negative rail of the breadboard.
- Then, using a wire (shown as yellow in the diagram), connect one end to a hole in the same row as the LED’s cathode, and the other end to Digital pin 13 on the Arduino.
- Now we connect this back to the ground. Use a wire (shown as white in the diagram) to connect from the negative rail to GND (ground) on the Arduino on the side with digital pins.
Part III: Connect to Power and Load Code
- Using our last wire (longer red wire shown in diagram), connect one end to the positive rail of the breadboard, and the other end to the 5V on the Arduino.
- Connect the Arduino to your computer using the USB connect cable, and upload the Arduino sketch shown below. After about 10 seconds, open up your serial monitor to check the output!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
/* Using a MQ-4 Sensor with Arduino to detect Methane (CH4) gas levels in the air */ /* Jenna Epstein */ /* Sensing the City - Project Tutorial Component | University of Pennsylvania */ /* MQ-4 sensors detect the presence of methane gas (CH4) in the air. This code establishes a connection between an MQ-4 sensor and an Arduino and prints out an analog output to the serial monitor. The analog signal that is generated and printed is proportional to the amount of gas detected in the air. The code also establishes a routine for a digital output. When digital output is HIGH (representing a high amount of gas detected), this triggers a red LED light to illuminate. */ /* This code was adapted from https://microcontrollerslab.com/mq4-methane-gas-sensor-pinout-interfacing-with-arduino-features/ */ const int AO_Pin=0; // Connect the AO of MQ-4 sensor with analog channel 0 pin (A0) of Arduino const int DO_Pin=8; // Connect the DO of MQ-4 sensor with digital pin 8 (D8) of Arduino const int LED_Pin=13; // Connect an LED with D13 pin of Arduino int threshold; // Create a variable to store the digital output of the MQ-4 sensor int AO_Out; // Create a variable to store the analog output of the MQ-4 sensor // Set up void setup() { Serial.begin(115200); // Initialize serial monitor using a baud rate of 115200 pinMode(DO_Pin, INPUT); // Set the D8 pin as a digital input pin pinMode(LED_Pin, OUTPUT); //Set the D13 pin as a digital output pin } // Main loop void loop() { AO_Out= analogRead(AO_Pin); // Read the analog output measurement sample from the MQ4 sensor's AO pin threshold = digitalRead(DO_Pin); // Read the digital output of MQ-4 sensor's DO pin Serial.print("Threshold: "); // Print out the text "Threshold: " Serial.print(threshold);// Print the threshold reached - with will either print be LOW or HIGH (above or underneath) Serial.print(", "); // print a comma and space Serial.print("Methane Conentration: "); // Print out the text "Methane Concentration: " Serial.println(AO_Out); // Print out the methane value - the analog output - beteewn 0 and 1023 if (threshold == HIGH){ digitalWrite(LED_Pin, HIGH);// If the threshold is reached, then the LED lights up to indicate a higher concentration } else{ digitalWrite(LED_Pin, LOW);// If the threshold is not reached (stays at 0), the LED stays off } delay(1000); // Set a 10 second delay } |
Note: I did not have any rotting food to test this with – the closest I had was a old(ish) russet potato. I waved the potato around the sensor and did observe the output on the serial monitor get higher – when it exceeded a threshold of 490 in my current environment, the LED light lit up red, therefore indicating a higher relative level of methane compared to the clean air in my location (before bringing over the potato). After moving the potato away, the LED light went off.
2 replies on “Using an MQ-4 Sensor with Arduino to Detect Methane Gas”
please help ,i want to link this to LCD
I liked this article about using the MQ-4 methane sensor. Would you be interested in discussing a partnership with my organization, the Institute for Earth Science Research and Education? I draw your attention in particular to this website about my book Monitoring Your Environment with Arduino Microcontrollers. See https://instesre.org/Arduino/index.htm