Skip to content

Commit 04a6f85

Browse files
committed
Temperature fix (Firmware > 5.0)
The temperature of the sensor for firmware versions greater than 5 has received a fix. This changes have only been tested on the MHZ19C. Please note: older versions no longer offer a float argument for the getTemperature() function. Instead, the value returned is in accordance with the data sheet. This is due to the float version being somewhat unreliable. As a result, temperature values received in older library versions will see an increase by 2C as the offset used in the datasheet differed. The firmware version (the major version) is obtained on initiation and used to determine which temperature code to run. Please report any issues as there are many firmware versions and I have only two sensors.
1 parent b0f7fb1 commit 04a6f85

File tree

2 files changed

+22
-55
lines changed

2 files changed

+22
-55
lines changed

src/MHZ19.cpp

+17-44
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ void MHZ19::begin(Stream &serial)
3939
Serial.println("!ERROR: Initial communication errorCode recieved");
4040
#endif
4141
}
42+
43+
/* What FW version is the sensor running? */
44+
char myVersion[4];
45+
this->getVersion(myVersion);
46+
47+
/* Store the major version number (assumed to be less than 10) */
48+
this->storage.settings.fw_ver = myVersion[1];
4249
}
4350

4451
/*########################-Set Functions-##########################*/
@@ -197,61 +204,27 @@ float MHZ19::getTransmittance(bool force)
197204
return 0;
198205
}
199206

200-
float MHZ19::getTemperature(bool isFloat, bool force)
207+
float MHZ19::getTemperature(bool force)
201208
{
202-
if(isFloat)
203-
{
204-
static byte baseTemp = 0;
205-
static bool isSet = false;
206-
207-
if(!isSet)
208-
{
209-
provisioning(CO2LIM);
210-
byte buff = (this->storage.responses.CO2LIM[4] - TEMP_ADJUST);
211-
212-
baseTemp = buff - (byte)getTemperatureOffset(true);
213-
isSet = true;
214-
}
215-
216-
if(force)
217-
provisioning(CO2UNLIM);
218-
219-
if(this->errorCode == RESULT_OK || force == false)
220-
{
221-
float buff = baseTemp;
222-
buff += getTemperatureOffset(false);
223-
return buff;
224-
}
225-
}
226-
227-
else if(!isFloat)
209+
if(this->storage.settings.fw_ver < 5)
228210
{
229211
if (force == true)
230212
provisioning(CO2LIM);
231213

232214
if (this->errorCode == RESULT_OK || force == false)
233215
return (this->storage.responses.CO2LIM[4] - TEMP_ADJUST);
234216
}
235-
236-
return -273.15;
237-
}
238-
239-
float MHZ19::getTemperatureOffset(bool force)
240-
{
241-
if (force == true)
242-
provisioning(CO2UNLIM);
217+
else
218+
{
219+
if (force == true)
220+
provisioning(CO2UNLIM);
243221

244-
if (this->errorCode == RESULT_OK || force == false)
245-
{
246-
/* Value appears to be for CO2 offset (useful for deriving CO2 from raw?) */
247-
/* Adjustments and calculations are based on observations of temp behavour */
248-
float calc = (((this->storage.responses.CO2UNLIM[2] - 8) * 1500) + ((this->storage.responses.CO2UNLIM[3] * 100) * 1 / 17));
249-
calc /= 100;
250-
return calc;
222+
if (this->errorCode == RESULT_OK)
223+
return (float)(((int)this->storage.responses.CO2UNLIM[2] << 8) | this->storage.responses.CO2UNLIM[3]) / 100;
251224
}
252225

253-
return -273.15;
254-
}
226+
return -273.15;
227+
}
255228

256229
int MHZ19::getRange()
257230
{

src/MHZ19.h

+5-11
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,10 @@
1111
#endif
1212

1313
#define MHZ19_ERRORS 1 // Set to 0 to disable error prints
14-
15-
#define TEMP_ADJUST 38 // This is the value used to adjust the temeperature.
16-
// Older datsheets use 40, however is likely incorrect.
14+
#define TEMP_ADJUST 40 // This is the value used to adjust the temeperature.
1715
#define TIMEOUT_PERIOD 500 // Time out period for response (ms)
18-
1916
#define DEFAULT_RANGE 2000 // For range function (sensor works best in this range)
20-
21-
#define MHZ19_DATA_LEN 9 // Data protocl length
17+
#define MHZ19_DATA_LEN 9 // Data protocol length
2218

2319
// Command bytes -------------------------- //
2420
#define MHZ19_ABC_PERIOD_OFF 0x00
@@ -73,8 +69,8 @@ class MHZ19
7369
/* returns Raw CO2 value as a % of transmittance */ //<--- needs work to understand
7470
float getTransmittance(bool force = true);
7571

76-
/* returns temperature using command 134 or 135 if isFloat = true */
77-
float getTemperature(bool isFloat = false, bool force = true);
72+
/* returns temperature using command 133 or 134 */
73+
float getTemperature(bool force = true);
7874

7975
/* reads range using command 153 */
8076
int getRange();
@@ -155,6 +151,7 @@ class MHZ19
155151
bool filterCleared = true; // Additional flag set by setFiler() to store which mode was selected
156152
bool printcomm = false; // Communication print options
157153
bool _isDec = true; // Holds preferance for communication printing
154+
uint8_t fw_ver = 0; // holds the major version of the firmware
158155
} settings;
159156

160157
byte constructedCommand[MHZ19_DATA_LEN]; // holder for new commands which are to be sent
@@ -180,9 +177,6 @@ class MHZ19
180177
/* generates a checksum for sending and verifying incoming data */
181178
byte getCRC(byte inBytes[]);
182179

183-
/* Returns what appears to be an offset */ //<----- knowledgable people might have success using against the raw value
184-
float getTemperatureOffset(bool force = true);
185-
186180
/* Sends commands to the sensor */
187181
void write(byte toSend[]);
188182

0 commit comments

Comments
 (0)