MQ Sensors Series usage with Arduino R4 Wi-Fi (Overview)

AndroidCrypto
8 min readJul 29, 2024

--

The Arduino Rev 4 Wi-Fi microcontroller is the perfect choice when collecting and analyzing sensor data from MQ-x sensor modules. This article series is about connecting several sensors to the Arduino device — this is part 1 with an overview about the following articles.

I will write about 8+ sensors that collect data about gases in the air, like Carbon Monoxide, Carbon Dioxide or Alcohol.

Reading the gas concentration

The MQ-Sensor series offers to read several gases with one sensor, but how does this work ? I write an article about this topic: MQ Sensors Series Read the Gas Concentration with Arduino R4 Wi-Fi. I know it sounds strange, but it is really important to read the datasheet for your specific sensor to understand what gases it can detect.

Wiring interfaces used for the Sensors

All the sensors are connected using the Analog Data Pin interface, which is being able to work with a 5 volt input. The Analog Data Pins are connected to an Analog-Digital Converter (“ADC”) that — usually — has a resolution of 10 bits. The Arduino Rev. 4 allows increasing the resolution up to 14 bits, and I’m using this resolution within my sketches.

Displaying the Sensor data

All data received from a sensor is displayed on an OLED display (“SSD1306”) using a 128 * 64 pixel resolution that allows to print out 7 lines of data. The data on the display is updated every 3 seconds. As I’m using an I2C interface based OLED that runs on 3.3 volt, the display is wired as follows:

OLED - Arduino
GND - GND pin
VCC - 3.3 Volt pin
SCL - SCL pin
SDA - SDA pin
MQ-135 Sensor measuring

Uploading the Sensor data to ThingSpeak.com

The sensor data displayed on the OLED are one time values, but if you want to see progress over some hours or days, you need to store the data anywhere. As I do not want to add a SD-Card reader to the setup, I’m using a web service (ThingSpeak.com) that is doing all the hard work for me, including an easy to use plotting of data:

Carbon Monoxide values in ppm from a MQ-135 sensor

There is a free plan available that includes 4 channels with each 8 fields for collecting and displaying data. A perfect explanation on how to set up the account is available on the RandomNerdTutorials.com website “ESP32 HTTP GET with Arduino IDE”. I’m using NOT the ThingSpeak library but a simple “HTTP GET” call for this feature. If you don’t want to use this service, or you don’t have an internet connection, you can simply comment out the WI-FI-connection and “updateThingSpeakChannel” calls.

The free plan is limited to a minimum update of 15 seconds, and I’m using a 30 seconds interval to publish the data to ThingSpeak.

Libraries used for the sketches

Beneath the libraries used for WI-FI (“WiFiS3.h”) and the OLED display (“SPI.h”, “Wire.h”, “Adafruit_GFX.h” and “Adafruit_SSD1306.h”) I’m using the library “MQSensorsLib” by Miguel Angel Califa Urquiza (“miguel5612”) for all MQ-x Sensor related tasks. A second repository contains some more information (e.g. datasheets) about the sensors (“MQSensorsLib_Docs”).

Short overview about the specific gases the sensors are sensitive to

  • MQ-2: Methane, Butane, LPG, Smoke
  • MQ-3: Alcohol, Ethanol, Smoke
  • MQ-4: Methane, CNG
  • MQ-5: Natural gas, LPG
  • MQ-6: LPG, Butane gas
  • MQ-7: Carbon monoxide gas *2)
  • MQ-8: Hydrogen gas
  • MQ-9: Carbon monoxide, flammable gases *2)
  • MQ-131: Ozone *1)
  • MQ-135: Carbon monoxide, Benzene, Ammonia, Alcohol, Smoke
  • MQ-136: Hydrogen Sulfide *1)
  • MQ-137: Ammonia *1)
  • MQ-138: Benzene, Toluene, Alcohol, Acetone, Propane, Formaldehyde, Hydrogen *1)
  • MQ-214: Methane, Natural gas *1)

*1) this sensor is not covered by this article series

*2) this sensor requires a changed technical setup and wiring

Articles belonging to this article series

These are the planned articles that will be published soon:

  • application using sensor MQ-2: get values for Hydrogen (H2), Liquid Petroleum Gas (LPG), Carbon Monoxide (CO), Alcohol and Propane (C 3H8). Product Overview: Ideal for home and factory gas leakage monitoring, this module detects gases such as natural gas, hydrogen, butane, propane, methane, and smoke.
  • application using sensor MQ-3: get values for Liquid Petroleum Gas (LPG), Methane (CH4), Carbon Monoxide (CO), Alcohol, Benzene (C6H6) and Hexane (C6H14). Product Overview: Sensitive to alcohol, this module is resistant to interference from gasoline, smoke, and water vapor. It’s suitable for breathalyzer applications and on-site ethanol vapor detection.
  • application using sensor MQ-4: get values for Liquid Petroleum Gas (LPG), Methane (CH4), Carbon Monoxide (CO), Alcohol and Smoke. Product Overview: Offering high sensitivity to methane gas and other combustible gases like propane and butane, this module is perfect for home and factory leakage monitoring.
  • application using sensor MQ-5: get values for Hydrogen (H2), Liquid Petroleum Gas (LPG), Methane (CH4), Carbon Monoxide (CO) and Alcohol. Product Overview: Featuring LM393 and ZYMQ-5 gas sensor, this module excels in detecting liquefied petroleum gas (LPG), natural gas, and coal gas, making it suitable for home and industrial applications.
  • application using sensor MQ-6: get values for Hydrogen (H2), Liquid Petroleum Gas (LPG), Methane (CH4), Carbon Monoxide (CO) and Alcohol. Product Overview: With sensitivity to propane, butane, LPG, and natural gas, this module is versatile and suited for various combustible gas detection needs.
  • application using sensor MQ-7: get values for Hydrogen (H2), Liquid Petroleum Gas (LPG), Methane (CH4), Carbon Monoxide (CO) and Alcohol. Product Overview: Designed for carbon monoxide detection, this module employs high-low temperature cycling and conductivity sensing. It’s apt for home and factory gas leakage monitoring, capable of detecting carbon monoxide and other gases.
  • application using sensor MQ-8: get values for Hydrogen (H2), Liquid Petroleum Gas (LPG), Methane (CH4), Carbon Monoxide (CO) and Alcohol. Product Overview: Specializing in hydrogen gas sensing, this module is highly sensitive and suitable for monitoring hydrogen-containing gases in home and industrial settings.
  • application using sensor MQ-9: get values for Liquid Petroleum Gas (LPG), Methane (CH4) and Carbon Monoxide (CO). Product Overview: Equipped with high-low temperature cycling detection, this module is adept at sensing carbon monoxide, methane, and liquefied petroleum gas. It’s a cost-effective choice for diverse gas detection applications.
  • application using sensor MQ-135: get values for Carbon Monoxide (CO), Carbon Dioxide (CO2), Alcohol, Toluen (Methylbenzol, C6H5CH3), Ammonium (NH4) and Acetone (CH₃COCH₃). Product Overview: Designed for monitoring home and industrial gas leaks, this module is suitable for detecting gases like liquefied petroleum gas, butane, propane, methane, smoke, ammonia, sulfide, and benzene.
  • Burn-In an MQ-Sensor: it is necessary to burn-in a brand-new sensor or a sensor that was not in usage for a long period of time. This article describes how to run such a burn-in period, including a sketch that documents the sensor readings during this burn-in period.

Note: the Product Overviews are taken from Auscom, Austria. A nice overview including datasheets about the MQ-x Sensors series can be found here.

There are some more sensors within the MQ-x series (e.g. MQ-131 [Ozone], MQ-136 [Hydrogen Sulfide gas], MQ-137 [Ammonia] or MQ-138 [Benzene, Toluene, Alcohol, Acetone, Propane, Formaldehyde gas, Hydrogen]), but they are not covered by this article series so far. If you like to get a tutorial about MQ-131, MQ-136, MQ-137 or MQ-138 sensors, I’m open that you send me the sensor and I will publish a tutorial.

Important information about the source of the application

Most of the sensor specific source code is taken from the examples of the MQSensorsLib.

Source code of the sketches

At the end of each article, you will find a link to my GitHub repository containing all scripts and additional files.

Technical data of the MQ-x Sensors

All my set-ups are based on MQ-x sensor modules and not the bare sensors. I’m using the “Flying Fish” modules series that have a 4 pin connection. Please check your module for the pinout to avoid any damage to the sensor and/or the microcontroller:

MQ-x sensor pinout from the back side

I’m using just 3 pins of the 4 pins as follows:

Upper pin        = VCC -> connect to 5 Volt pin of the Arduino
Second upper pin = GND -> connect to GND pin of the Arduino
Third upper pin = DO ("Digital Output") - is not connected
Fourth pin = AO ("Analog Output") -> connect to A0 pin of the Arduino

The “DO” is giving an “on/off” signal, depending on the values read and the potentiometer setting. I’m using the Analog Output instead, as it gives us more fine-tuned data.

WARNING: please do NOT contact this sensor with an ESP8266 or ESP32 device, as the Analog Output pin can give more volts than allowed as input voltage. Using the unmodified sensor with an ESP-xx device requires a voltage divider.

Important Burn-In Period of new MQ-x Sensors

You may think that it is not important to run a “Burn-In-Period” but this is essential to get good results. An MQ-x Sensor is a sensor with a heater element with a resistor that resistance depends on the gas concentration in the air. When these sensors are “off” electricity for some days or months (e.g. produced in China, stored for some months, send by ship to Europe) the surface of the resistor isn’t calibrated anymore. You need to run the sensor for up to 48 hours (2 days !) with 5 Volt to “burn-in” the resistor (again). Don’t trust the measures of the sensor without the burn-in period. See this article that explains the procedure: Link to the article.

Calibration of the Sensor

The library is using a “self-service” calibration on startup. Please wait for about 30 seconds after startup before you are using any measures.

Use MQ-x Sensors together with other sensors like a DHT11 Temperature Sensor

As the MQ-x Sensor is using a heater, you should NOT place a temperature sensor beneath a MQ-x Sensor to avoid any wrong measurements.

General note on different gases measurement with one Sensor

Most MQ-x Sensors are sensitive for more than one gas, and the library is using a specific dataset for each gas. This dataset is responsible to get a ppm (“parts per million”) value for the specific gas, but all values are calculated from one data source (the measured voltage on the Analog Output Pin of the Sensor / the Analog Input Pin of the Arduino device). For that reason, all “measured” values follow the same timeline — below is the measured data of a MQ-135 sensor displayed in ThingSpeak. You can clearly see that all data is following the same progress:

MQ-135 Sensor Measurement

Source code

The source code (“sketches”) for all sensor applications are published on my GitHub repository that has subfolders for each sensor. This way, you can easily download the examples for all sensors in one click.

Data Sheets of the sensor modules

I’m enclosing the data sheets — if available — in my GitHub repository in the sub folder “datasheets”.

--

--