Hello all,
I have an arduino using an MH-Z16 CO2 sensor: https://sandboxelectronics.com/?product=mh-z16-ndir-co2-sensor-with-i2cuart-5v3-3v-interface-for-arduinoraspeberry-pi
I was experiencing problems with the system seemingly disappearing from our network, but remained running, at random periods of time and for no reason. Although yesterday afternoon I got a hint that it had something to do with the sensor.
What seems to happen is for the last 15 hours I was running a sketch that would output the ppm value to an LCD(16x2) display, while also accepting udp packets from an rPi node server, then reply with a packet returning other values (temp, and rh%). For the purposes of trouble shooting I had disabled it sending a the actual ppm reading and only left it displaying the ppm on the LCD.
So this morning I got back and it was still running. So my next step in trouble shooting was to input the ppm value to an an if statement, that flips a relay on or off, looking like this:
if (mySensor.ppm >= maxCO2*10) {
if (digitalRead(relay3) == 1) {
digitalWrite(relay3, LOW); // relay 'on'
}
} else {
if (digitalRead(relay3) == 0) {
digitalWrite(relay3, HIGH); // relay 'off'
}
}
The reason the max CO2 is multiplied by ten is I wanted to fit the ppm in the packet as a single byte so the max value is 255. So when it receives a packet setting the maxCO2 value to 250, the max CO2 level is actually 2500ppm, and when it sends a packet it divides the ppm value by ten (1755/10=176 as a single byte). At least that is my intent.
So last night the "mySensor.ppm" parameter was actually just hard coded as 2500 and the maxCO2 setting was 220. This permanently turned the relay on. We have an analogue dial after the relay to set the fan speed, and since we could read the ppm value on the lcd, we would adjust the fan manually.
Since it was still on our network this morning my next step in trouble shooting was to replace the hard coded 2500ppm value with "mySensor.ppm" as shown above. This worked for a few seconds, however once the ppm dropped below the threshold, the system again disappeared from the network. This seems to happen some of the time, but not always, when it cross the maxCO2 threshold going either way.
Given this behavior, my question now is, why is this? The provided library returns the ppm as a uint32_t variable. I haven't a clue as to what the matter with the if statement is.
Would any of you be able to shed some light on this?
Thanks!