drivers: Add driver for MH-Z19 (CO2 sensor)#3778
drivers: Add driver for MH-Z19 (CO2 sensor)#3778frestr wants to merge 2 commits intomicropython:masterfrom
Conversation
|
Thanks for the contribution. Do you know about |
|
Thanks for the feedback. I've made some changes to the code. |
There was a problem hiding this comment.
Thanks for updating. It's quite simple now which is good! In general I'm not sure if these drivers are suited for this repo because it adds to the testing and maintenance burden. But for now we can keep it here as a PR, that's fine.
| ''' | ||
| # Wait until a new cycle starts | ||
| self._wait_on_condition(lambda: self.pin.value() == 0) | ||
| self._wait_on_condition(lambda: self.pin.value() == 1) |
There was a problem hiding this comment.
This second wait shouldn't be needed because the time_pulse_us that follows will wait until the pin goes high.
There was a problem hiding this comment.
Oh, I must've overlooked that in the docs.
|
|
||
| # Measure high and low duration during cycle | ||
| t_h = machine.time_pulse_us(self.pin, 1, 1500000) / 1000 | ||
| t_l = machine.time_pulse_us(self.pin, 0, 1500000) / 1000 |
There was a problem hiding this comment.
Did you test that this works and gives accurate readings?
There was a problem hiding this comment.
I did and yes, it does. The results are slightly different than before because of the higher precision in the time readings, but this is negligible compared to the accuracy of the sensor itself. In the datasheet the pulses are given in 2 ms increments though, so it may be possible to round the reading to the nearest increment for more consistent readings.
There was a problem hiding this comment.
Ok, that's good to know that it works well. I think this is a good demonstration of how to use the time_pulse_us function.
That's understandable. As long as others can find it here, I'm fine with that.
Cool. I'll check it out. |
|
@frestr Was searching for an example to interface with MH-Z19 and came across this PR. Thanks for sharing. |
|
This would be a candidate to put in |
Done in micropython/micropython-lib#575 |
The unit also has an UART interface, but because of the issue described in #3763 I'm unable to implement this for the time being.
I still think the driver can be useful with only the PWM interface though, as the only main difference between the PWM and UART interfaces is that with UART you can also programatically calibrate the unit.