Built-in project · Arduino Uno R3

Six MQ gas sensors on A0 to A5, on an Arduino Uno

An MQ-135, MQ-7, MQ-4, MQ-5, MQ-6 and MQ-9 on A0 to A5, printed once a second: each reads its own gas, higher for more, after its heater has warmed up. The whole build, an Arduino Uno and 6 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Beginner About 15 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $43. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • Analog gas sensors and what MQ numbers mean
  • Reading six analog pins with arrays and a loop
  • Heater warm-up before readings settle
  • Why heaters need a proper 5 V supply
  • Printing a tidy line of values
  • Difficulty: Beginner
  • Time: About 15 minutes
  • Board: Arduino Uno R3
  • Parts: 8

How it works

Each MQ sensor has a small heated element whose resistance drops in the gas it was made for. A load resistor on its board turns that into a voltage on its AO pin, so more gas reads higher. The six sensors are MQ-135, MQ-7, MQ-4, MQ-5, MQ-6 and MQ-9, on A0 to A5.

Once a second the sketch reads all six pins and prints one line with each name and its 0 to 1023 value. The numbers start high while the heaters warm up, so wait for them to settle.

Parts list

8 parts, plus the jumper wires. They retail for about $43; on Mokxi they're free.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Air quality sensor, MQ-135 pin GNDArduino Uno R3 pin GND
Carbon monoxide sensor, MQ-7 pin GNDArduino Uno R3 pin GND
Methane sensor, MQ-4 pin GNDArduino Uno R3 pin GND
LPG and natural gas sensor, MQ-5 pin GNDArduino Uno R3 pin GND
LPG sensor, MQ-6 pin GNDArduino Uno R3 pin GND
CO and flammable gas sensor, MQ-9 pin GNDArduino Uno R3 pin GND
Air quality sensor, MQ-135 pin VCCArduino Uno R3 pin 5V
Carbon monoxide sensor, MQ-7 pin VCCArduino Uno R3 pin 5V
Methane sensor, MQ-4 pin VCCArduino Uno R3 pin 5V
LPG and natural gas sensor, MQ-5 pin VCCArduino Uno R3 pin 5V
LPG sensor, MQ-6 pin VCCArduino Uno R3 pin 5V
CO and flammable gas sensor, MQ-9 pin VCCArduino Uno R3 pin 5V
Air quality sensor, MQ-135 pin AOArduino Uno R3 pin A0
Carbon monoxide sensor, MQ-7 pin AOArduino Uno R3 pin A1
Methane sensor, MQ-4 pin AOArduino Uno R3 pin A2
LPG and natural gas sensor, MQ-5 pin AOArduino Uno R3 pin A3
LPG sensor, MQ-6 pin AOArduino Uno R3 pin A4
CO and flammable gas sensor, MQ-9 pin AOArduino Uno R3 pin A5

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// Six MQ gas sensors on A0 to A5.
//
//   MQ-135 AO -> A0   MQ-7 AO -> A1   MQ-4 AO -> A2
//   MQ-5 AO -> A3     MQ-6 AO -> A4   MQ-9 AO -> A5
//   every VCC -> 5V   every GND -> GND
//
// Each one is a heated element whose resistance falls in the gas it is made
// for, over a load resistor on its board, so more gas reads higher. They read
// high at first, while the heaters warm up: wait for the numbers to settle.
//
// Six heaters draw about 900 mA, more than a USB port gives: a real build
// powers them from a 5 V supply of their own, its ground joined to the Uno's.
//
// Drag any sensor's gas level up.

const char *names[] = {"MQ-135", "MQ-7", "MQ-4", "MQ-5", "MQ-6", "MQ-9"};
const int pins[] = {A0, A1, A2, A3, A4, A5};

void setup() {
  Serial.begin(9600);
  Serial.println("Mokxi Uno: six MQ gas sensors on A0 to A5");
}

void loop() {
  for (int i = 0; i < 6; i++) {
    Serial.print(names[i]);
    Serial.print(' ');
    Serial.print(analogRead(pins[i]));
    Serial.print(i < 5 ? "  " : "\n");
  }
  delay(1000);
}

Code walkthrough

The key functions of the sketch, and what each one does.

setup sketch.ino

Opens the serial port at 9600 and prints a hello line. The analog pins need no pinMode, because analogRead sets them up itself.

void setup() {
  Serial.begin(9600);
  Serial.println("Mokxi Uno: six MQ gas sensors on A0 to A5");
}

loop sketch.ino

Loops over the six entries of names and pins. For each, it prints the name and analogRead(pins[i]), with two spaces between sensors and a newline after the last. Then it waits one second before the next set.

void loop() {
  for (int i = 0; i < 6; i++) {
    Serial.print(names[i]);
    Serial.print(' ');
    Serial.print(analogRead(pins[i]));
    Serial.print(i < 5 ? "  " : "\n");
  }
  delay(1000);
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • All values start high and then drop This project
    That is the heater warming up. Wait for the numbers to settle before trusting them.
  • Readings are unstable on a real board This project
    Six heaters draw about 900 mA, more than USB gives. Use a separate 5 V supply with its ground joined to the Uno's.
  • Readings are high and unstable at power up. Air quality sensor
    The sensor needs a warm up of several minutes, and a long burn in the first time, before readings settle. Its heater also draws about 150 mA, so use a good 5 V supply.
  • The numbers do not tell you which gas is present. Air quality sensor
    The MQ-135 reacts to many gases and cannot tell them apart. Use the analog output as a relative level, and calibrate it in clean air. Do not use it for safety alarms.
  • The reading is high or drifts right after power-up. Carbon monoxide sensor
    The heater needs a long warm-up, often 24 hours the first time, and a few minutes on each start. Wait for a stable baseline before trusting numbers.
  • The value does not match real ppm. Carbon monoxide sensor
    AO is a voltage that needs calibration. Convert with the datasheet curve and your load resistor, in clean air. Real MQ-7 use cycles the heater between 5 V and 1.4 V. It draws about 150 mA, so use a solid 5 V supply.
  • Readings are high and drift at first. Methane sensor
    The heater needs a warm up, ideally 24 hours the first time and a few minutes later. Ignore readings until it settles.
  • The numbers are not true gas concentrations. Methane sensor
    The sensor needs calibration in clean air with load resistor values. Use AO for relative values, and DO with the trimmer for a threshold. It needs 5 V and draws about 150 mA.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.