tobiasschuerg / MH-Z-CO2-Sensors

Arduino imeplementation for CO2 sensors of the MH-Z series (Intelligent Infrared CO2 Module)
MIT License
77 stars 40 forks source link

Prevents int overflow with high CO2 value in Arduino ATmega boards #50

Closed vmarseguerra closed 10 months ago

vmarseguerra commented 1 year ago

On Arduino ATmega boards an int is stored on 2 bytes. With a CO2 value above 32 768 ppm, the value returned by the library became negative. Switched to uint32_t int32_t.

tobiasschuerg commented 1 year ago

Thank you for your contribution! I appreciate the effort you've put into this.

Your proposed changes will affect error handling. In the current implementation, negative values are used as status codes to signify errors. See https://github.com/tobiasschuerg/MH-Z-CO2-Sensors/blob/82d85c331229a6a8479ee3a56c20e6dcff92cfc0/MHZ.h#L21-L27

Is there a real-world usecase for supporting such high concentrations? As far as I know the sensors only supoprt a concentration up ro 10.000 ppm anyways: https://github.com/tobiasschuerg/MH-Z-CO2-Sensors#supported-sensors

vmarseguerra commented 1 year ago

Indeed, an int32_t also works.

MH-Z14B and MH-Z16 can have a range above 10 000ppm From the Winsen Aliexpress store: image

I have MH-Z14B sensor with a range of 0-50000 ppm, we use it in caving to check if we can go deeper or not. Since CO2 is heavier than air, it can accumulate at the bottom of certain caves.