MQ-2 gas sensor readings all wrong on Photon but on Arduino its reading fine?


#1

Hi! I’m using a MQ-2 gas sensor with the Photon to get some readings. In theory, the buzzer goes off and then I check every minute to see if the are still gas leakage in the area. Otherwise, the buzzer stops.

The problem is, when using the Photon, I get constantly 4095 on the A0 pin (3.3V), no matter what I do. On the Arduino, the same code works fine: I get readings that vary between 300 and 330. I understand that the Arduino analog pins get readings from 0 to 1023 and the Photon goes from 0 to 4095 - but the problem is that on the Photon I get the constant, no changing value of 4095, which probably means that the heater coil of the MQ-2 is getting heated on its limit.

I wonder if this is a voltage problem, since the MQ-2 Datasheet (check it here) says it should have at least 5V to work - but that’s why I’m using Vin with a 5V source instead of the computer - it should get 5V right?

Here is the code:

int buzzerPin = D1;
int sensorPin = A0;
int valorSensor = 0;
int limit = 500;

void setup() {
    pinMode(buzzerPin, OUTPUT);
    //pinMode(sensorPin, INPUT);
    Serial.begin(9600);
}

void loop() {
    valorSensor = analogRead(sensorPin);
    Serial.println(valorSensor);
    Particle.variable("valorSensor", valorSensor);
    delay(1000); //wait for a minute to see if no more gas is being detected (make it 10 for testing without having to wait a whole minute everytime)
    gasAlarm();
}

void gasAlarm() {
    if (valorSensor > limit) { //buzzer goes off
        delay(200);
        digitalWrite(buzzerPin, HIGH);
        delay(200);
        digitalWrite(buzzerPin, LOW);
    }
    else {
        digitalWrite(buzzerPin, LOW);
    }
}

And here are the schematics and drawings:

Any help on this is greatly appreciated.


#2

Why do you have this line commended out?

//pinMode(sensorPin, INPUT);

remove the // and see if that helps.

Also take a multimeter and see what the readings are from Ground & A0 Pin.


#3

I commented it out because, if I understood correctly, on the Particle reference docs it says to not use pinMode with analogRead: https://docs.particle.io/reference/firmware/photon/#analogread-adc-

Good idea about the Multimeter, gonna try it out - also am going to try it out without the pinMode commented - can’t remember when I commented it out so maybe now it works…


#4

If the sensor output state is only High or Low then you can just try using a digital pin instead of the Analog pin and then set that pin to INPUT and see if that helps.


#5

No, the sensor is actually analog. It needs to be connected to an analog pin so I can get a threshold reading and then if it goes above that threshold it should fire the alarm.


#6

Also the way you have the sensor wired does not look right.

Is the Fritzing diagram exactly how you have it hooked up? It looks like the sensor output wiring is not right, specifically the red output wire which connects back to VIN. The Ground is what your feeding to A0 based on the diagram. Double check that.


#7

Take the output of the sensor and tie ground to GND. Take the positive output from the sensor and connect that to A0 and see if it works.


#8

Yeah, so the ground is what you’re supposed to check, that’s why there is a 20k resistance there… You actually have to check the resistance load on these sensors. That’s why between the ground and resistors there’s a wire connecting to A0.

But I think I’m starting to get it - since the Arduino operates on 5V, 20K makes sense - I changed the resistance value with the Photon to only 10K and I started to get different readings (around 2064), which looks right if I = 2 and V = 5 (R = V*I)…

If I use no resistance at all I get readings from 600 to 620 :smile:


#9

OK, without looking at the sensor specs I’m only assuming it works like other analog sensors I have used in the past.

Sounds like you have it figured out now :smile:


#10

Yes! I changed the resistors for a 10K trimpot and now it’s working perfectly. Now I can finally tweet when someone farts in the area LOL :hankey:


#11

That’s awesome :smiley:


#12

You also already got told in this thread that this is not the way to write this!

void loop() {
    valorSensor = analogRead(sensorPin);
    Serial.println(valorSensor);
    Particle.variable("valorSensor", valorSensor); // <-- DON'T DO THIS!!!
    delay(1000); //wait for a minute to see if no more gas is being detected (make it 10 for testing without having to wait a whole minute everytime)
    gasAlarm();
}

#13

Oh man you’re right :man:

I checked this later on and corrected it - I grabbed parts of this code from an older version. The code is now like this:

int buzzerPin = D1;
int sensorPin = A0;
int valorSensor = 0;
int limit = 1300;
int diff = valorSensor-limit;

    void setup() {
        pinMode(buzzerPin, OUTPUT);
        //pinMode(sensorPin, INPUT);
        Serial.begin(9600);
        Particle.variable("valorSensor", valorSensor);
        Particle.variable("diff", diff);
    }
    
    void loop() {
        valorSensor = analogRead(sensorPin);
        Serial.println(valorSensor);
        Serial.println("Difference: ");
        Serial.println(diff);
        delay(10); 
        gasAlarm();
    }
    
    void gasAlarm() {
        if (valorSensor > limit) { //buzzer goes off
            delay(200);
            digitalWrite(buzzerPin, HIGH);
            delay(200);
            digitalWrite(buzzerPin, LOW);
        }
        else {
            digitalWrite(buzzerPin, LOW);
        }
    }

#14

That looks better for the Particle.variable() part.

One other thing is here, tho’

int diff = valorSensor-limit;

This is only an initialisation but won’t update the value of diff each time valorSensor changes.
In order to have that to happen, you’d need to recalc the difference each time after a change of the base values.


#15

@guzforster, perhaps I’m missing something but how are you ensuring that the voltage on A0 is not exceeding 3.3v from the sensor?


#16

@peekay123 It wont exceed 3.3V because the Photon has a voltage regulator - but I am using a 10K trimpot (a potentiometer) so I can adjust the resistance and therefore adjust its sensibility :smile:


#17

@ScruffR Yeah. Gotta work on that!


#18

[quote=“guzforster, post:16, topic:20565”]
It wont exceed 3.3V because the Photon has a voltage regulator
[/quote] The regulator can’t power your sensor as it uses to much current. So if you power the sensor via 5V, then it is still possible to get 5V on the photon pin UNLESS you use a voltage divider. Is this how you are using the trimpot?


#19

There might be a misunderstanding.
The 3.3V regulator only regulates the supply voltage for the device but has no level shifting capabilities for the input pins.
And AFAIK the sensor wants to be powered with 5V to provide the required heating.

And a resistance in series with the input pin will only limit your current but not the voltage. For that you’d need a voltage divider as @peekay123 already pointed out.

As I see the datasheet the sensor has a resistance of 3k to 30k, so you’d need to calculate your pot and possibly protecting series resistor the prevent over-voltage even if your pot gets set to its max/min positions.


#20

I’m not quite getting it… I’m not using a voltage divider. I just assumed that the sensor did not required at least 5V to work because it is working. When I let gas out in a lighter near the sensor it will spike the readings and beep the buzzer.

I’m not an electronics expert so I didn’t quite undertand what you meant by the voltage divider.

I changed the resistors for a 10k trimpot, that’s it. All the other connections remain the same.