Gas sensors are very useful devices for controlling pollutants in the air. It is similar to the MQ4 methane gas sensor. The MQ4 sensor is a compressed gas sensor, which is highly sensitive to methane and less sensitive to alcohol and other gases. The methane gas sensor provides an output signal according to the detected surrounding environment or the concentration of CH4 in the environment. The sensor has been proven effective in alarm systems and gas detection circuits in commercial scales and coal mines.
MQ-4 pinout :
The methane gas sensor module contains an internal voltage regulator, a pull-down resistor for default state settings, a sensitivity potentiometer, and a noise filter capacitor. It has two built-in LEDs, power LED and digital output LED. The pin arrangement of the MQ-4 methane sensor module is shown in the figure:
MQ-4 working principle
Once the unit is turned on, the power light lights up. MQ-4 works by sensing the concentration of methane in the air. Then the sensor's analog pin generates an analog signal proportional to the amount of methane in the air. We can measure the analog output of the sensor using an ADC.
Nowadays, most microcontrollers come with a built-in ADC terminal that can be used to read the analog output of such sensors as the MQ4. The microcontroller reads the analog output signal of the methane gas sensor and adapts the signal to convert the measured analog voltage to the methane concentration in the air. Based on this analogy, we can take appropriate actions such as triggering the alarm, etc., what ever you have coded to do.
Similarly, the same applies to the digital output pin of the MQ4 methane sensor. When methane is detected, the digital pin goes up and lights up the inbuilt digital output. This digital pin can also be used for various activities commanded by a microcontroller.
How to connect Arduino with MQ4
Arduino code:
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
}
}