MQ gas sensor Digital Read

Hi,
after some google and blog search, I would like to write my (I think first) entry in this blog.
I’m testing a MQ6 sensor connected to Arduino directly. I connected both analog and digital sensor output.
analog output is connected to A0 input in Arduino and digital output to pin 7 in Arduino.
the code is (is an adapted code from MQ7 example):

// Test sensor de gas tipo Butano con Arduino
// MQ-6 is a highly sensitive gas sensor to petroleum based gases but less sensitive to Alcohol and Carbon di oxide.
// https://dmohankumar.wordpress.com/2012/11/15/gas-leakage-alarm-using-mq-6-sensor-sensor-circuit-14/
// MB 07.04.2019, v1.0
// datasheet at https://www.sparkfun.com/datasheets/Sensors/Biometric/MQ-6.pdf

// MQ-6 wiring
#define analogMQ6 A0      // Signal 
#define digitalMQ6 7
//#define ledPin D7         // Device internal LED      
int MQ6sensorValue = 0;   // value read from the sensor
int MQ6digitalValue = 0;    //digital value of MQ6

// *********************************

void setup()
{
    Serial.begin(9600);
    Serial.println(F("MQ-6 Gas Sensor Flying-Fish started"));

    pinMode(analogMQ6, INPUT);
    pinMode(digitalMQ6, INPUT);

    //pinMode(ledPin, OUTPUT);
}
    
// *********************************

void loop() { 

  // A) preparation
    // turn the heater fully on
    analogWrite(analogMQ6, HIGH); // HIGH = 255
    // heat for 1 min
    delay(60000);
    // now reducing the heating power: turn the heater to approx 1,4V
    analogWrite(analogMQ6, 71.4); // 255x1400/5000
    // heat for 90 sec
    delay(90000);
    
  // B) reading    
    // CO2 via MQ7: we need to read the sensor at 5V, but must not let it heat up. So hurry!
    analogWrite(analogMQ6, HIGH); 
    delay(50); // Getting an analog read apparently takes 100uSec
    MQ6sensorValue = analogRead(analogMQ6);
    MQ6digitalValue = digitalRead(digitalMQ6);

  for (int i=0; i<10; i++) {  
  // C) print the results to the serial monitor
    Serial.print("Analogue MQ-6 PPM: ");                       
    Serial.println(MQ6sensorValue);  
    Serial.print("Digital MQ-6 PPM: ");                       
    Serial.println(MQ6digitalValue);     
  // D) interpretation
    // Detecting range: 20ppm-2000ppm carbon monoxide
    // air quality-cases: < 200 perfect, 200 - 800 normal, > 800 - 1800 high, > 1800 abnormal
  
    if (MQ6sensorValue <= 200) 
    {
        Serial.println("Air-Quality: Butano perfect");
    }
    else if ((MQ6sensorValue > 200) || (MQ6sensorValue <= 800)) // || = or
    {
        Serial.println("Air-Quality: CO normal");
    }
    else if ((MQ6sensorValue > 800) || (MQ6sensorValue <= 1800))
    {
        Serial.println("Air-Quality: CO high");
    }
    else if (MQ6sensorValue > 1800) 
    {
        //digitalWrite(ledPin, HIGH); // optical information in case of emergency
        Serial.println("Air-Quality: ALARM CO very high");
        delay(3000);
        //digitalWrite(ledPin, LOW);
    }
    else
    {
        Serial.println("MQ-6 - cant read any value - check the sensor!");
    }
delay(1000);
    }
}

on the Analog read side, everything is fine, and if I put a lighter gas near, the read change from 0 to higher levels.
But, the digital read is strange, as in normal condition it reads a 1 and with gas it reads a 0.
so I decided to check with Voltmeter the sensor digital output, and found out that in normal condition it shows -0,6V and with GAS it shows +0,6 (when I expected 0 and 5v).

So I have now 2 doubts:
1- why the Arduino reads 1 and 0 and not the opposite
2- why the sensor is sending -0,6/+0,6 instead of 0/5

Any suggestion is very appreciated.

thanks
Matteo

Hi Matteo,

This may not solve your specific query, but the MQ6 operates differently to the MQ7 sensor

As per your example, the MQ7 requires alternating 1.4v and 5v however the MQ6 simply requires a constant 5v
( See the datasheet here: https://www.sparkfun.com/datasheets/Sensors/Biometric/MQ-6.pdf)

I believe reading values from it should be as simple as:

#include <Wire.h>
#define analogMQ6 A0
#define digitalMQ6 7

int MQ6SensorValue = 0;

void setup() {

  Serial.begin(9600);
  pinMode(digitalMQ6, OUTPUT);

}

void loop() {

  MQ6SensorValue = analogRead(analogMQ6);
  Serial.println(MQ6SensorValue);
}

Hope that helps,
Jamie

Hi Jamie, and thanks for your answer.

I quickly tried an easier code, such as:

#define analogMQ6 A0      // Signal 
#define digitalMQ6 7
//#define ledPin D7         // Device internal LED      
int MQ6sensorValue = 0;   // value read from the sensor
int MQ6digitalValue = 0;    //digital value of MQ6

// *********************************

void setup()
{
    Serial.begin(9600);
    Serial.println(F("MQ-6 Gas Sensor Flying-Fish started"));

    pinMode(analogMQ6, INPUT);
    pinMode(digitalMQ6, INPUT);

    //pinMode(ledPin, OUTPUT);
}
    
// *********************************

void loop() { 

    for (int i=0; i<10; i++) {      
    analogWrite(analogMQ6, HIGH); 
    delay(50); // Getting an analog read apparently takes 100uSec

    MQ6sensorValue = analogRead(analogMQ6);
    MQ6digitalValue = digitalRead(digitalMQ6);


  // C) print the results to the serial monitor
    Serial.print("Analogue MQ-6 PPM: ");                       
    Serial.println(MQ6sensorValue);  
    Serial.print("Digital MQ-6 PPM: ");                       
    Serial.println(MQ6digitalValue);     
    delay(1000);
    }
delay(5000);
}

and results are (when using the lighter):

17:19:12.516 → Analogue MQ-6 PPM: 29
17:19:12.516 → Digital MQ-6 PPM: 1
17:19:13.555 → Analogue MQ-6 PPM: 48
17:19:13.555 → Digital MQ-6 PPM: 1
17:19:14.602 → Analogue MQ-6 PPM: 65
17:19:14.602 → Digital MQ-6 PPM: 0
17:19:20.661 → Analogue MQ-6 PPM: 108
17:19:20.661 → Digital MQ-6 PPM: 0
17:19:21.694 → Analogue MQ-6 PPM: 64
17:19:21.742 → Digital MQ-6 PPM: 1

it looks like digital has a threshold on 65, above that is marking a logical 0.
I tried switching cables on Voltmeter (I think I had them swapped).
So now the results could be according with Arduino readings, giving 0.9v without gas, and -0.9 with gas.
So my 2 questions could be results in just 1, but still not understanding.

thanks
Matteo

Hi,

sorry, after further testing, I consider i was too sleepy and connected the Voltmeter completely wrong.
Now, after correct measuring, the Digital output of sensor is sending constantly 5V with no Gas, reducing to 0V when some Gas is detected. So, Arduino is right, and sensor digital output pin has something wrong.

I'll follow using analog output, as possibly the module has some problem.

thanks
Cheers