MQ135 CO2 sensor odd readings

Hi,
I’m learning to program using Particle Photon.
I have a MQ135 Gas sensor.

I use the example code from the library I imported, but the readings I get are around 4-8 PPM, where it should be around 400 PPM.

When I breathe on the sensor, I can see a change in the values so the sensor does read something,
The sensor is connected directly to A0, VIN, GND.

I have tried to leave it plugged in for the night.

Attached is the

include "MQ135/MQ135.h"

int number = 0;
int state = 0;
MQ135 gasSensor = MQ135(0);
int led = D7;
float rzero = gasSensor.getRZero();
int ppm = gasSensor.getPPM();

// SETUP
void setup() {

}

// LOOP
void loop() { 

    float rzero = gasSensor.getRZero();
    digitalWrite(led,HIGH);             // turn the ledPin on
    delay(100);                         // stop the program for some time
    digitalWrite(led, LOW);             // turn the ledPin off
    delay(100);                         // stop the program for some time

    int co2_ppm = gasSensor.getPPM();
    int ppm = co2_ppm / 4;
    Spark.variable("ppm", &ppm, INT);   
    Particle.publish("PPM", String(ppm));

    delay(10000);
}

That’s not how you use Particle.variable() - the variable only gets set up once in setup().
Have you allowed for enough time for the sensor to heat up?
How have you got the sensor wired?

That library was not ported properly! It still uses the Arduino conversion with 1023 @ 5V where for Photon it would be 4095 @ 3.3V.

I left the sensor plugged in overnight, so it should have pre heated. I also tried to adjust the potentiometer on the back of it. However I am still not getting anything close to 400 ppm.

How do I go about and change the Arduino conversion to 4095 @3.3v?

You’d need to copy/paste the contents of the library files into your own project (or fork the library on GitHub) and in MQ135.cpp replace this

float MQ135::getResistance() {
  int val = analogRead(_pin);
  return ((1023./(float)val) * 5. - 1.)*RLOAD;
}

with

float MQ135::getResistance() {
  int val = analogRead(_pin);
  return ((4095.0/(float)val) * 3.3 - 1.0)*RLOAD;
}

But make sure that the output of the sensor doesn’t exceede 3.3V

1 Like

Thanks for your help so far! I forked the library and tried my sketch again.
Link to Library: https://github.com/Jacob-ls/MQ135/tree/master/firmware

Now my readings are 0 PPM.

I have connected the sensor directly to VIN, GND and A0. I still use the same code as in my first post.

Without applying this?

Since you forked the lib, you can add some Serial.print() statements to get the raw analog reading and also some of the sub-results in the calculation.

Should I move the Particle.publish(“ppm”,String(ppm)); to setup instead of loop?

I’m sorry if this is a stupid question, but I just got my photon a month ago :blush:

Particle.publish() needs to stay there, but Spark.variable("ppm", &ppm, INT); should be Particle.variable("ppm", ppm); and move to setup().

Thanks, my readings still say 0PPM.

When I use the original Git library, my readings are around 4-5ppm

My sketch looks like this now:

include "MQ135/MQ135.h"

int number = 0;
int state = 0;
MQ135 gasSensor = MQ135(0);
int led = D7;
float rzero = gasSensor.getRZero();
int ppm = gasSensor.getPPM();

// SETUP
void setup() {
    Particle.variable("ppm", ppm);

}

// LOOP
void loop() { 

    float rzero = gasSensor.getRZero();
    digitalWrite(led,HIGH);             // turn the ledPin on
    delay(100);                         // stop the program for some time
    digitalWrite(led, LOW);             // turn the ledPin off
    delay(100);                         // stop the program for some time

    int co2_ppm = gasSensor.getPPM();
    int ppm = co2_ppm / 4;
    Particle.publish("ppm", String(ppm));

    delay(10000);
}

Here is a picture of my setup:

Have you tried this?

Should I add it to one of the files from Github, or directly in my sketch?

Have you had success with the MQ-135 and the library in the web IDE? I am a little bit nurvous, that there is a mistake in the library, makes me wonder if there is more than one. I am struggling to find a set of sample code for the photon, that works out of the box like for the DHT sensor or audiosensors.

I have no such sensor, so I can’t check.
But the library isn’t really that complicated. Most functions are one- or two-liners.
Pulling the relevant ones directly into your project file and adding the debug statements would be what I’d do.
There are also other threads in this forum dealing with this sensor - just have a search.

Did you ever figure out the readings? I’m running into the same issue.

Your picture shows a “Flying Fish” <> MQ135.


The Flying Fish you can control via I2C without Library.