I have a weather station and have been having some issues with my mh-z19 co2 sensor returning bad values. I actually have two different problems:
every second call the pulseIn line returns 0, when it doesn't return 0 the reading is exactly what it should be. I have tried different wiring configs with a pull down and with logic level conversion to 5v (the sensor outputs 3.3v) and neither worked (this is on a pro micro).
when i try the code on a M0, the value is roughly half what it should be, does the sam21 processor time pulseIn different? i tried a few different pins to make sure I wasn't using a reserved pin, but had the same result.
when you do pulseIn(mhz19_SENSOR_PIN, HIGH, 1004000);it waits for the pin to go HIGH, starts timing, then waits for the pin to go LOW and stops timing and returns the length of the pulse in microseconds. it will also give up and returns 0 if no pulse starts within a specified time out.
==> are you sure you'll see every time a LOW to HIGH transition within the 1.004 second you listed ?
J-M-L:
==> are you sure you'll see every time a LOW to HIGH transition within the 1.004 second you listed ?
thanks! i hadn't though about the waiting time for the high, the duty cycle is 1004ms (as per the spec sheet) so i hadn't accounted for wait time. i doubled the timeout value (worse case scenario assuming it starts waiting the moment it goes low and it is a the maximum duty time) and it now is working on the 32u4.
i tried on the M0/samd21 though and still no luck. the output is still a fraction of what it should be.
Zorac:
i tried on the M0/samd21 though and still no luck. the output is still a fraction of what it should be.
how do you power your mh-z19 co2 sensor?
On a M0 you can get 7mA as maximum DC current for I/O pins versus a maximum of 40mA (which you should limit to 20mA in practice and there are other rules for total draw) on your proMini. The mh-z19 co2 sensor requires roughly 18mA so can be powered from a digital pin of the pro-Mini but not from the M0.
could that be this?
(of course you don't need the logic level shifter on the M0 as it operates at 3.3V logic as well)
On a M0 you can get 7mA as maximum DC current for I/O pins versus a maximum of 40mA (which you should limit to 20mA in practice) on your proMini. The mh-z19 co2 sensor requires roughly 18mA so can be powered from a digital pin of the pro-Mini but not from the M0.
could that be this?
(of course you don't need the logic level shifter on the M0 as it operates at 3.3V logic as well)
No, i use a 3a 5v power supply to power everything seperately, the co2 sensor is not powered from the m0. currently i have a second 3.3v power supply to power the m0 as well (power in on the 3.3v pin, not Vin since I am already regulating it)
*edit
just tried using an interrupt instead of pulseIn and it works fine. would still like to get pulseIn working though as there is no need to have an interrupt continuously running.
But an interrupt takes almost no time away from your main sketch. pulseIn() is blocking so your sketch is locked up and unable to do anything else for up to 2 seconds at a time.