8000 drivers: Add driver for MH-Z19 (CO2 sensor) by frestr · Pull Request #3778 · micropython/micropython · GitHub
[go: up one dir, main page]

Skip to content

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

Closed
wants to merge 2 commits into from

Conversation

frestr
Copy link
@frestr frestr commented May 10, 2018

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.

@dpgeorge
Copy link
Member

Thanks for the contribution. Do you know about machine.time_pulse_us()? It may be useful to implement the functionality (or not... but worth checking). Also, it seems overkill to use an IRQ to wait for the pin to change, can't you just poll it? Polling would work across more platforms.

@frestr
Copy link
Author
frestr commented May 11, 2018

Thanks for the feedback. I've made some changes to the code.

Copy link
Member
@dpgeorge dpgeorge left a 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)
Copy link
Member

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.

Copy link
Author

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
Copy link
Member

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?

Copy link
Author
@frestr frestr May 15, 2018

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.

Copy link
Member

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.

@dpgeorge
Copy link
Member

@frestr You may be interested to test out PR #3784 because it will allow you to use the UART for the sensor reading.

@frestr
Copy link
Author
frestr commented May 15, 2018

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.

That's understandable. As long as others can find it here, I'm fine with that.

You may be interested to test out PR #3784 because it will allow you to use the UART for the sensor reading.

Cool. I'll check it out.

@balajisraghavan
Copy link
balajisraghavan commented Jul 7, 2019

@frestr Was searching for an example to interface with MH-Z19 and came across this PR. Thanks for sharing.

@dpgeorge
Copy link
Member
dpgeorge commented Jul 6, 2021

This would be a candidate to put in micropython-lib.

@jimmo
Copy link
Member
jimmo commented Nov 15, 2022

This would be a candidate to put in micropython-lib.

Done in micropython/micropython-lib#575

@jimmo jimmo closed this Nov 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
0