DIY ELECTRONICSTR

← All projects

LPG Gas Measurement with the MQ-5 (PPM)

Intermediate30 min

A project that reads the MQ-5 gas sensor's analog output, turns it into a voltage and the sensor's present resistance (Rs), measures Ro itself in clean air at start-up, and writes an estimated LPG concentration (PPM), worked out with the LPG curve in the MQ-5 datasheet, to the Serial Monitor every two seconds. At start-up it waits 10 seconds for the heater to settle, then calibrates for 5 seconds: the sensor must be in clean air then, with no gas near it. In clean air the Rs/Ro ratio is about 6.5 and the PPM below 1; with gas around, the ratio drops and the PPM rises clearly. The PPM value is an estimate: by its datasheet, a new sensor gives steady readings only after more than 24 hours of heating on first use. No extra library needed.

Get it on Google Play

What you need

Wiring steps

  1. 1

    Power the sensor

    Power the sensor

    Plug a red jumper wire into the 5V pin on the Arduino's POWER header. Connect its other end to the top pin, pin 1 (VCC), in the MQ-5 module's pin row. The sensor's heater runs on 5V and gets warm while it runs.

    • Arduino 5V → MQ-5 VCC (red wire)
  2. 2

    Ground (GND) connection

    Ground (GND) connection

    Plug a black jumper wire into the GND pin on the Arduino's POWER header. Connect its other end to the module's pin 2 (GND).

    • Arduino GND → MQ-5 GND (black wire)
  3. 3

    Connect the digital output (DOUT)

    Connect the digital output (DOUT)

    Plug a blue jumper wire into the Arduino's digital pin 2. Connect its other end to the module's pin 3 (DOUT). This pin changes state when the gas level crosses the threshold set with the trimmer on the board. The code in this project only uses the analog output; this connection is there in case you want to add a threshold alarm.

    • Arduino pin 2 → MQ-5 DOUT (blue wire)
  4. 4

    Connect the analog output (AOUT)

    Connect the analog output (AOUT)

    Plug a green jumper wire into pin A0 on the Arduino's ANALOG IN header. Connect its other end to the module's bottom pin, pin 4 (AOUT). The code reads the gas concentration on this pin as a value from 0 to 1023 and works out the PPM from it (sensorPin = A0).

    • Arduino A0 → MQ-5 AOUT (green wire)

Arduino sketch

Download the sketch (.ino)

const int sensorPin = A0;

// The value of the load resistor (RL)
// on the back of the sensor module,
// in kOhm (usually between 1k and 20k)
const float RL = 10.0;

// Rs/Ro in clean air: from the
// curve in the MQ-5 datasheet (the
// MQUnifiedsensor library uses 6.5 too)
const float CLEAN_AIR_RATIO = 6.5;

// The LPG curve in the MQ-5 datasheet:
// ppm = CURVE_A * (Rs/Ro) ^ CURVE_B
// (the numbers are the MQUnifiedsensor
// library's fit to that curve)
const float CURVE_A = 80.897;
const float CURVE_B = -2.431;

// Ro: by the datasheet, the sensor's
// resistance in 1000 ppm of H2, in kOhm.
// Measured in clean air at start-up:
// Ro = Rs in clean air / CLEAN_AIR_RATIO
float Ro = 0;

void setup() {
  Serial.begin(9600);
  Serial.println("Sensor warming up, please wait...");
  // time for the heater inside
  // the sensor to settle
  delay(10000);

  // Calibration: average of 10 readings
  // while the sensor is in clean air
  Serial.println("Calibrating in clean air...");
  float total = 0;
  int readings = 0;
  for (int i = 0; i < 10; i++) {
    float voltage = analogRead(sensorPin) * (5.0 / 1023.0);
    if (voltage > 0) {
      total += RL * (5.0 - voltage) / voltage;
      readings++;
    }
    delay(500);
  }
  if (readings > 0) {
    Ro = (total / readings) / CLEAN_AIR_RATIO;
  }
  Serial.print("Ro = ");
  Serial.print(Ro, 2);
  Serial.println(" kOhm");
}

void loop() {
  int adc_value = analogRead(sensorPin);

  // Step 1: turn the 0-1023 ADC
  // reading into a 0-5V voltage
  float voltage = adc_value * (5.0 / 1023.0);

  // No dividing by zero: the
  // sensor cannot be read,
  // or calibration failed
  if (voltage == 0 || Ro == 0) {
    Serial.println("No data from the sensor, or a bad connection.");
    delay(2000);
    return;
  }

  // Step 2: work out the sensor's
  // present resistance (Rs) with:
  // Rs = RL * (5V - Voltage) / Voltage
  float Rs = RL * (5.0 - voltage) / voltage;

  // Step 3: find the Rs / Ro ratio (about
  // 6.5 in clean air, lower as gas rises)
  float ratio = Rs / Ro;

  // Step 4: PPM from the datasheet curve:
  // ppm = CURVE_A * (Rs/Ro) ^ CURVE_B
  float ppm = CURVE_A * pow(ratio, CURVE_B);

  // Print the result
  Serial.print("Raw ADC: ");
  Serial.print(adc_value);
  Serial.print(" | Voltage: ");
  Serial.print(voltage, 2);
  Serial.print("V | Rs/Ro ratio: ");
  Serial.print(ratio, 2);
  Serial.print(" | Estimated PPM: ");
  Serial.println(ppm, 2);

  // one reading every 2 seconds
  delay(2000);
}

What the Serial Monitor shows

SERIAL MONITOR · 9600 BAUD
Sensor warming up, please wait...
Calibrating in clean air...
Ro = 9.09 kOhm
Raw ADC: 146 | Voltage: 0.71V | Rs/Ro ratio: 6.61 | Estimated PPM: 0.82
Raw ADC: 148 | Voltage: 0.72V | Rs/Ro ratio: 6.50 | Estimated PPM: 0.85
Raw ADC: 151 | Voltage: 0.74V | Rs/Ro ratio: 6.35 | Estimated PPM: 0.90
Raw ADC: 410 | Voltage: 2.00V | Rs/Ro ratio: 1.65 | Estimated PPM: 24.12
Raw ADC: 620 | Voltage: 3.03V | Rs/Ro ratio: 0.72 | Estimated PPM: 182.75
Raw ADC: 760 | Voltage: 3.71V | Rs/Ro ratio: 0.38 | Estimated PPM: 846.05

If a wire is loose

SERIAL MONITOR · 9600 BAUD
No data from the sensor, or a bad connection.

Similar projects

In the app, tick off every wire, match the parts to your inventory and pick up where you left off.

Get it on Google Play