Everything about Internet of Things and more

MQ4 Gas Sensor – Methane Natural Gas monitor with MQ3 / MQ4 sensors

Can we monitor natural gas leakage with Arduino, without spending allot of money? There are many sensors available everywhere over the internet and most of them can be found very affordable in Asia largest online marketplace. They are quite cheap if you choose to buy them directly from manufacturers and not from the closest re-sellers. For instance, I bought the MQ4 just for 1.29$ from a well known China electronics marketplace.

Arduino MQ4 gas sensor – review and tutorial with video example

MQ4 Gas sensor overview (UPDATES: New code examples added for gas concentration)

MQ series sensors use a small heater inside with an electro-chemical sensor in order to measure different kind of gases combinations. They can be calibrated, but, in order to do that a know concentration of the measured gas or gasses is needed for that. For industrial purpose calibrations are done in special metrology laboratories with accurate probes and tests. In our case, we will test it as it comes from the producer without any additional calibration or settings. The main idea before ordering this kind of sensor was to build a homemade alarm sensor, which should make some alarming noise or light whenever someone forgot the cooker stove one, or my little kid learned to play with stove switches, or there is a leak in my gas installation. To accomplish that, I wanted a methane gas, simple to use, and also compatible with arduino platform. Producer says that MQ4 sensor can sense methane / natural gas easily with a range sensitivity from 300 to 10000ppm , costs are very low, and can be easily plugged with Arduino boards.

MQ4 sensor pinout connections and power consumption

Arduino MQ4 sensor specifications

This model comes with 4 pins:

MQ4 model must be powered with stable 5v and needs at least 150mA (best to have 250mA) according to the datasheet declaration, to be able to work properly. Also before getting stable measurements, this model needs at least 1 minute to heat up. Be aware that in some datasheets use the term “preheat”, which means that some versions needs from 12h to 24h to burn-in the sensor. Only after this you will be able to get consistent data. Also this kind of devices, which have internal heater, are pretty sensible to ambient influences like humidity or moisture.

You can find technical datasheet in PDF format provided by sparkfun.com at the end of article.

Hardware and software used for testing

  1. Arduino UNO R3 compatible board
  2. MQ4 gas sensor
  3. breadboard
  4. breadboard compatible power supply (5v, min 300mA)
  5. dupont wires / cables
  6. arduino IDE 1.6.xx

Here is the arduino code used for demostration:

MQ4 Gas Sensor - Arduino tutorial
const int gasPin = A0; //GAS sensor output pin to Arduino analog A0 pin

void setup(){
  Serial.begin(9600); //Initialize serial port - 9600 bps

void loop(){
  delay(150); // Print value every 1 sec.

[IMG-Gal id=32]

UPDATE – New code examples for gas concentration

Because this topic is pretty hot, I decided to dig more and find better resources in order to get the best from this sensor. Just reading the Arduino analog input is not enough to make a reliable gas monitoring system. In order to do that we need a new approach to calculate the gas PPM by implementing the formulas provided by the datasheet. Below you can find two code examples which I found and may help you to get better results from this module:

 Arduino MQ4 gas sensor - Geekstips.com
 This example is for calculating R0 which is
 the resistance of the sensor at a known concentration 
 without the presence of other gases, or in fresh air
void setup() {
  Serial.begin(9600); //Baud rate

void loop() {
  float sensor_volt; //Define variable for sensor voltage
  float RS_air; //Define variable for sensor resistance
  float R0; //Define variable for R0
  float sensorValue; //Define variable for analog readings
  for (int x = 0 ; x < 500 ; x++) //Start for loop
    sensorValue = sensorValue + analogRead(A0); //Add analog values of sensor 500 times
  sensorValue = sensorValue / 500.0; //Take average of readings
  sensor_volt = sensorValue * (5.0 / 1023.0); //Convert average to voltage
  RS_air = ((5.0 * 10.0) / sensor_volt) - 10.0; //Calculate RS in fresh air
  R0 = RS_air / 4.4; //Calculate R0

  Serial.print("R0 = "); //Display "R0"
  Serial.println(R0); //Display value of R0
  delay(1000); //Wait 1 second
 Arduino MQ4 gas sensor - Geekstips.com
 This example is to calculate the gas concentration using the R0
 calculated in the example above
 Also a OLED SSD1306 screen is used to display data, if you do not
 have such a display, just delete the code used for displaying

#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#include <gfxfont.h>

#include <SPI.h> //Library for SPI interface 
#include <Wire.h> //Library for I2C interface 

#define OLED_RESET 11 //Reset pin 
Adafruit_SSD1306 display(OLED_RESET); //Set Reset pin for OLED display

int led = 10; //LED pin
int buzzer = 9; //Buzzer pin
int gas_sensor = A0; //Sensor pin
float m = -0.318; //Slope
float b = 1.133; //Y-Intercept
float R0 = 11.820; //Sensor Resistance in fresh air from previous code

void setup() {
  Serial.begin(9600); //Baud rate
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //Initialize screen
  display.setTextColor(WHITE); //Set text color
  display.setTextSize(3); //Set text size
  pinMode(led, OUTPUT); //Set LED as output
  digitalWrite(led, LOW); //Turn LED off
  pinMode(buzzer, OUTPUT); //Set buzzer as output
  digitalWrite(buzzer, LOW); // Turn buzzer off
  pinMode(gas_sensor, INPUT); //Set gas sensor as input

void loop() {
  display.clearDisplay(); //Clear display
  display.setCursor(0, 5); //Place cursor in (x,y) location
  float sensor_volt; //Define variable for sensor voltage
  float RS_gas; //Define variable for sensor resistance
  float ratio; //Define variable for ratio
  float sensorValue = analogRead(gas_sensor); //Read analog values of sensor
  sensor_volt = sensorValue * (5.0 / 1023.0); //Convert analog values to voltage
  RS_gas = ((5.0 * 10.0) / sensor_volt) - 10.0; //Get value of RS in a gas
  ratio = RS_gas / R0;   // Get ratio RS_gas/RS_air

  double ppm_log = (log10(ratio) - b) / m; //Get ppm value in linear scale according to the the ratio value
  double ppm = pow(10, ppm_log); //Convert ppm value to log scale
  double percentage = ppm / 10000; //Convert to percentage
  display.print(percentage); //Load screen buffer with percentage value
  display.print("%"); //Load screen buffer with "%"
  display.display(); //Flush characters to screen

  if (ppm > 2000) {
    //Check if ppm value is greater than 2000
    digitalWrite(led, HIGH); //Turn LED on
    digitalWrite(buzzer, HIGH); //Turn buzzer on
  } else {
    //Case ppm is not greater than 2000
    digitalWrite(led, LOW);
    //Turn LED off
    digitalWrite(buzzer, LOW);
    //Turn buzzer off

! Note – Now both code pieces above may not be clear for many of you. Therefore to understand the logic behind this calculations you should check here out how this guy got to this solution by trying to implement the Datasheet calculation formulas. Code examples from that website are not working because of the bad encoding (you will get Arduino IDE a C/C++ related error (error/stay 302)) but the formulas and math demonstration are pretty interesting. Use the code from the above examples.

The sensor is quite small and can be fitted easy in any prototype with 4 screws in the board corners. This version comes with a rotating lever from which you can adjust sensitivity by rotating it left or right. On each lateral edge you can find two LEDs which indicates power availability and digital output status. Open this link to read datasheet.

I will come back with more feedback after checking behavior in my house. Until then, thank you for your time.

Hoping that this article inspired you, i kindly invite you share this articlesubscribe my YouTube channel and join the communities on social networks. Feel free to comment or send suggestions / remarks so i can improve the content quality.

Share on: Facebook Twitter Google+

15 Replies to “MQ4 Gas Sensor – Methane Natural Gas monitor with MQ3 / MQ4 sensors”

  1. Josh Harris says:

    does this formula work for MQ-8?

  2. GeeksTips says:

    Hi Josh, honestly I don’t think it will work, MQ-8 is a H2 Hydrogen sensor, a gas which needs different chemical reactivity. Formulas should be adapted considering the producer datasheet specifications.

  3. Samuel Marcondes Puker De Sousa says:

    Hi GeeksTips,
    the load resistance “RL” in this module is 10kΩ? Because you used 10k in the sketch.

    RS_gas = ((5.0 * 10.0) / sensor_volt) – 10.0; //Get value of RS in a gas

    Greetings from Brazil.

  4. Falcon says:

    can you explain me 2 of this ?
    sensor_volt = sensorValue * (5.0 / 1023.0); //Convert average to voltage
    RS_air = ((5.0 * 10.0) / sensor_volt) – 10.0; //Calculate RS in fresh air

  5. In the equation:

    RS_air = ((5.0*10.0)/sensor_volt)-10.0

    What is the 10.0 value and where does it come from?

  6. Also when calculating the RO value, Ro=Rs/4.4, Where does the 4.4 value come from?

  7. tita says:

    you find ?
    float m = -0.318; //Slope
    float b = 1.133; //Y-Intercept

  8. Shahed says:

    in the equation Ro=Rs/4.4, Where does the 4.4 value come from?
    float m = -0.318; //Slope
    float b = 1.133; //Y-Intercept
    where you got those?

  9. Pieck4444 says:


    Value 4.4 is (probably) clean air factor (from datasheet MQ4).
    More here:
    link1) https://www.sparkfun.com/datasheets/Sensors/Biometric/MQ-4.pdf
    link2) https://os.mbed.com/users/renanbmx123/code/MQ4/file/401959f72658/MQ4.h/

  10. Sathya Sai says:

    How do you calculate the Rl of MQ-4?

  11. muhammad quraish says:

    i am quraish from pk.
    i am doing project on mq4.mq7 and some other sensors soo will you plz guide me.

  12. Alvaro Ferrari says:

    I need help, when I run the program to calibrate, it always gives the result of R0 a negative value.

  13. OMAR MOHAMED says:

    I am would like to know if you have MQ4 sensor FC-22-SBX version
    I am looking for this version specifically

Leave a Reply

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