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

Constant in `readCO2PWM` depends on range #27

Closed v-borovikov closed 2 years ago

v-borovikov commented 2 years ago

Hi!

Just started working with MH-Z sensors family. After reading MH-Z19 sensors datasheet and examing some libraries I've noticed that every one copies exactly same mistake.

ppm_pwm = 2000 * (th - 2) / (th + tl - 4);
          ^^^^--- depends on range                                     

Instead of constant 2000 there should be current range. So, if sensor was configured with i.e. 5000 range, this formula will lower the value of measurement.

drleavsy commented 2 years ago

Yes, I've got the same problem with wrong PWM results, when updated the library to the latest version 1.3.0.

tobiasschuerg commented 2 years ago

You're right!

I currently don't have much capacity left - feel free to submit a PR

drleavsy commented 2 years ago

Hi Tobias, PR has been created , please merge it when you will have time: #33

tobiasschuerg commented 2 years ago

Initially it was already 5000 ;-)

As pointed out by @v-borovikov the range will not be the same for everyone. To address this issue the range (2000 or 5000) should be configurable - ideally it is initially read from the sensor (command 0x99) and then used...

drleavsy commented 2 years ago

New PR is https://github.com/tobiasschuerg/MH-Z-CO2-Sensors/pull/34