Introduction
Methane is among the most significant greenhouse gases, with human activity accounting for over 70% of methane emissions. Gas provides advantages, such as the ability to be used for cooking regularly. However, its discharge is hazardous to both individuals and the environment. As a solution, sensors capable of detecting leakage must be installed. MQ-4 gas sensor is one of the sensors. Hence, In this tutorial, we are going to “Interface MQ4 Methane Gas Sensor with Arduino”.
MQ-4 Smoke Sensor Module
The MQ4 sensor is a small gas sensor that detects methane gas. This methane gas sensor generates an output signal based on the concentration of CH4 detected in the surrounding environment.
Hardware Overview
A micro-ceramic tube of AL2O3, a sensitive layer of Tin Dioxide, a measuring electrode, and a heater is placed into a crust made of plastic and stainless steel net in the MQ4 gas sensor’s construction and configuration. The heater offers fundamental working conditions for sensitive components’ precision. There are six pins on the sensor. Four of them are utilized to receive signals, while the other two are employed to provide a heating current.
Pin Configuration
Pin Name | Description |
Vcc | Power Pin requires an operating voltage of 5V. |
GND | Ground pin. |
DO | Digital output pin |
AO | Analog output pin |
Hardware Required
S.no | Component | Value | Qty |
---|---|---|---|
1. | Arduino UNO | – | 1 |
2. | USB Cable Type A to B | – | 1 |
3. | Methane Gas Sensor | MQ4 | 1 |
4. | Jumper Wires | – | – |
5. | Resistor | – | 1 |
6. | LED | – | 1 |
Circuit Diagram
Connection Table
Arduino UNO | MQ4 Methane Gas Sensor |
---|---|
GND | GND |
5v | Vcc |
A0 | A0 |
D8 | D0 |
Arduino Code
/* MQ-4 Methane Sensor module with Arduino */
/* Interfacing with Arduino */
const int AO_Pin=0; // Connect AO of MQ4 with Analog channel 0 pin (A0) of Arduino
const int DO_Pin=8; // Connect DO of MQ4 with Digital pin 8 (D8) of Arduino
const int Led_Pin=13; // Connect an LED with D13 pin of Arduino
int threshold_value; // A variable to store digital output of MQ4
int AO_Out; // stores analog output of MQ4 sensor
void setup() {
Serial.begin(115200); // Initialize serial communictation with a baud rate of 115200
pinMode(DO_Pin, INPUT); // Configure D8 pin as a digital input pin
pinMode(Led_Pin, OUTPUT); //Configure D3 pin as a digital output pin
}
void loop()
{
AO_Out= analogRead(AO_Pin); // Take Analog output measurement sample from AO pin of MQ4 sensor
threshold_value= digitalRead(DO_Pin); //Read digital output of MQ4 sensor
Serial.print("Methane Conentration: ");
Serial.println(AO_Out);//prints the methane value
Serial.print("threshold_value: ");
Serial.print(threshold_value);//prints the threshold_value reached as either LOW or HIGH (above or underneath)
delay(100);
if (threshold_value== HIGH){
digitalWrite(Led_Pin, HIGH);//if threshold_value has been reached, LED turns on as status indicator
}
else{
digitalWrite(Led_Pin, LOW);//if threshold not reached, LED remains off
}
}
Working Explanation
Following the provided connection table and diagram, connect the circuit. Observe the readings after uploading the code. After the module has been powered on for two to three seconds, the sensor will detect properly.
Code Explanation
- Initially, we must define module pins that are wired to Arduino. For the Arduino to read the data, they must be specified. The AO Pin variable is associated with analog channel 0, and the DO Pin variable is associated with digital output pin 8. D13 is also given the variable name LedPin. Two variables are defined to hold the analog and digital outputs of the sensor.
- In the void setup, the sketch sets the pin mode of the pins and initializes the serial monitor at 115200 bps. DO Pin will be the microcontroller’s input, and Led Pin will be the output to signify based on the executions.
- The void loop reads analog signals using the analogRead() function and puts them in the variable “AO Out.” Likewise, digital values are read by using the digitalRead() function and stored in the field “threshold value.” The “threshold value” can be LOW or HIGH. Coded messages are shown on the Serial monitor. Following that, it determines whether the led should be turned on or off. If the limit is set to HIGH, the LED will glow to show that the methane level is higher than typical. Otherwise, it will be turned off.
Application and Uses
- Alarm systems
- Gas detection circuits
- Environmental monitoring systems