-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
drivers: Add driver for MH-Z19 (CO2 sensor) #3778
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I must've overlooked that in the docs.
|
||
8000 # 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you test that this works and gives accurate readings?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.