From 14b0f908f3858d48bcc80d70d012e043e35c2c0a Mon Sep 17 00:00:00 2001 From: Miguel Angel Califa Urquiza Date: Wed, 4 Jun 2025 17:46:08 -0500 Subject: [PATCH 1/3] Add voltage resolution unit test for MQ303A --- examples/UnitTesting/UnitTesting.ino | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/examples/UnitTesting/UnitTesting.ino b/examples/UnitTesting/UnitTesting.ino index 94ab895..24cf712 100644 --- a/examples/UnitTesting/UnitTesting.ino +++ b/examples/UnitTesting/UnitTesting.ino @@ -237,15 +237,24 @@ test(MQ303A_Ethanol) { MQ303A.setRegressionMethod(1); //_PPM = a*ratio^b MQ303A.setA(3.4916); MQ303A.setB(-2.432); // Configure the equation to to calculate Ethanol concentration - MQ303A.init(); + MQ303A.init(); MQ303A.setR0(10); - + int ppmExp=944; int PPM = MQ303A.setRsR0RatioGetPPM(0.1); // Send and Rs/R0 and return PPM (Using datasheet) - + assertEqual(PPM,ppmExp); } +test(MQ303A_VoltResolution) +{ + float vRes = 4.7; + MQ303A.setVoltResolution(vRes); + MQ303A.setADC(100); // provide dummy ADC value + MQ303A.readSensor(true); // dummy read for MQ303A + assertEqualFloat(MQ303A.getVoltResolution(), vRes); +} + test(MQ309A_CO) { MQ309A.setRegressionMethod(1); //_PPM = a*ratio^b From da3adb7335af3b7b8a0da8c453273b8f03fa62dd Mon Sep 17 00:00:00 2001 From: Miguel Angel Califa Urquiza Date: Wed, 4 Jun 2025 17:46:18 -0500 Subject: [PATCH 2/3] fix: preserve voltage resolution --- src/MQUnifiedsensor.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/MQUnifiedsensor.cpp b/src/MQUnifiedsensor.cpp index 928210d..05b4492 100644 --- a/src/MQUnifiedsensor.cpp +++ b/src/MQUnifiedsensor.cpp @@ -142,10 +142,11 @@ float MQUnifiedsensor::validateEcuation(float ratioInput) float MQUnifiedsensor::readSensor(bool isMQ303A, float correctionFactor, bool injected) { //More explained in: https://jayconsystems.com/blog/understanding-a-gas-sensor + float voltRes = _VOLT_RESOLUTION; // preserve global resolution if(isMQ303A) { - _VOLT_RESOLUTION = _VOLT_RESOLUTION - 0.45; //Calculations for RS using mq303a sensor look wrong #42 + voltRes = voltRes - 0.45; //Calculations for RS using mq303a sensor look wrong #42 } - _RS_Calc = ((_VOLT_RESOLUTION*_RL)/_sensor_volt)-_RL; //Get value of RS in a gas + _RS_Calc = ((voltRes*_RL)/_sensor_volt)-_RL; //Get value of RS in a gas if(_RS_Calc < 0) _RS_Calc = 0; //No negative values accepted. if(!injected) _ratio = _RS_Calc / this->_R0; // Get ratio RS_gas/RS_air _ratio += correctionFactor; From 92f3c30e82422d8b293eb6a6330ca4fcdd4d185c Mon Sep 17 00:00:00 2001 From: Miguel Angel Califa Urquiza Date: Wed, 4 Jun 2025 17:46:28 -0500 Subject: [PATCH 3/3] Fix MQ303A voltage offset bug --- src/MQUnifiedsensor.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/MQUnifiedsensor.cpp b/src/MQUnifiedsensor.cpp index 928210d..c3be898 100644 --- a/src/MQUnifiedsensor.cpp +++ b/src/MQUnifiedsensor.cpp @@ -142,10 +142,14 @@ float MQUnifiedsensor::validateEcuation(float ratioInput) float MQUnifiedsensor::readSensor(bool isMQ303A, float correctionFactor, bool injected) { //More explained in: https://jayconsystems.com/blog/understanding-a-gas-sensor + float voltageResolution = _VOLT_RESOLUTION; if(isMQ303A) { - _VOLT_RESOLUTION = _VOLT_RESOLUTION - 0.45; //Calculations for RS using mq303a sensor look wrong #42 + // Issue #42: MQ303A requires a 0.45V offset for the RS calculation. The + // previous implementation permanently decreased _VOLT_RESOLUTION every time + // the sensor was read which produced incorrect results after multiple calls. + voltageResolution -= 0.45; } - _RS_Calc = ((_VOLT_RESOLUTION*_RL)/_sensor_volt)-_RL; //Get value of RS in a gas + _RS_Calc = ((voltageResolution*_RL)/_sensor_volt)-_RL; //Get value of RS in a gas if(_RS_Calc < 0) _RS_Calc = 0; //No negative values accepted. if(!injected) _ratio = _RS_Calc / this->_R0; // Get ratio RS_gas/RS_air _ratio += correctionFactor;