adafruit / Adafruit_BME680

117 stars 76 forks source link

cannot turn off gas sensor once turned on #17

Closed pabloandresm closed 5 years ago

pabloandresm commented 5 years ago

Hello,

If I start a project with ".setGasHeater(0, 0);" then the gas sensor returns "0" as value, which is normal because it was setted off.

If at one point I set the sensor on (with .setGasHeater(320,150) for example), it correctly goes on.

But after that it cannot be set to off anymore. The .setGasHeater(0,0) issued later does not turn the gas sensor off.

I've tried to look at the code to find for a logical 'or' not being 'anded with the opposite ~' or any error like that but found nothing so far.

What do you think? Thanks

pabloandresm commented 5 years ago

I think I've found one bug. At the end of the function ::beginReading() in file Adafruit_BME680.cpp, in the last "if" there is a missing "else gas_resistance = NAN;"

The correct lines would be: / Avoid using measurements from an unstable heating setup / if (_gasEnabled) { if (data.status & BME680_HEAT_STAB_MSK) { //Serial.print("Gas resistance: "); Serial.println(data.gas_resistance); gas_resistance = data.gas_resistance; } else { gas_resistance = 0; //Serial.println("Gas reading unstable!"); } } else gas_resistance = NAN; // <<====== MISSING LINE

So when I turn off the gas sensor in the middle of a project the reading will return zero.

But I think there is still a bug, because after turning the gas sensor off, the temperature reading does not go down as it should (because the gas heater is not turned on anymore). So there is still an error somewhere.

caternuson commented 5 years ago

The Arduino library doesn't have explicit heater on/off control. Calling .setGasHeater(0, 0) only disables gas measurement: https://github.com/adafruit/Adafruit_BME680/blob/master/Adafruit_BME680.cpp#L397 The underlying Bosch library does though: https://github.com/adafruit/Adafruit_BME680/blob/master/bme680.c#L449

I'm not sure I follow your second post. Are you wanting a return of NAN instead of 0 when the sensor is disabled?

pabloandresm commented 5 years ago

The BME680 has an explicit heater on/off control using the register 0x71 as it is said in the specification. It is very useful to control the heater because when it is on it impacts in the measure of the temperature (more) and humidity (less). So one could chose to only activate it from time to time to measure the gas. As you mentioned the Adafruit_BME680 library only disables the retrieval of the result and does not turn off the heater.

Regarding my second post, when you set the .setGasHeater(0,0) to disable the result of gas, that if is wrong. If you take a look you are doing: if () { if () { } else return NAN; }

To be extremely brief in my description......you are forgeting the first if, to give also an "else return NAN".

So you have to issues here. One that you are not allowing people to set off the heater. And the second issue is that even if you are allowing people to stop reading the gas results, you have a bug regarding the chained IFs that I've just explained.

caternuson commented 5 years ago

Are you talking about this if block? https://github.com/adafruit/Adafruit_BME680/blob/master/Adafruit_BME680.cpp#L370

If not, can you link to it directly by line.

pabloandresm commented 5 years ago

That is exactly it. To reproduce the bug simply start the bme80 with .setGasHeater(320,150).....start getting readings....and later do .setGasHeater(0,0).....you will continue to receive the latest real value instead of 0 or NAN. That is because of the missing "else gas_resistance = NAN;" for the outer if.

Regarding the shutting off of the heater, it is a shame that this library doesn't do it. And at some point it is a bug, because the library does not use this feature of the heater (according to the specification of the sensor).

jib218 commented 5 years ago

Any plans of adding this feature?

ladyada commented 5 years ago

we won't be getting to it any time soon - if you'd like to add it and submit a PR - we'd check it out :)

jib218 commented 5 years ago

HTH: #19

:-)

ladyada commented 5 years ago

thanks u!