The Pico In MicroPython: A PIO Driver For The DHT22 |
Written by Harry Fairhead & Mike James | ||||
Monday, 17 May 2021 | ||||
Page 1 of 3 The DHT22 is a useful low-cost temperature and humidity sensor and you can use it via PIO assembler for the Pico. This is an extract from our latest book all about the Pico in MicroPython. Programming the Raspberry Pi Pico/W In MicroPython Second EditionBy Harry Fairhead & Mike JamesBuy from Amazon. Contents
Also of interest: <ASIN:B0BR8LWYMZ> <ASIN:B0BL1HS3QD> In this chapter we make use of all the ideas introduced in earlier chapters to create a raw interface with the low-cost DHT11/22 temperature and humidity sensor. It is an exercise in implementing a custom protocol directly in MicroPython using bit banging and then using the PIO. Given the documentation advises against using it, you might be wondering why we start with bit banging? The answer is that bit banging is easier to debug and it is usually a good idea to implement it first if possible, if only to prove that the device in question works and you know how it works. The DHT22The DHT22 is a more accurate version of the DHT11 and it is used in this project. The software will work with both versions and also with the AM2302, which is equivalent to the DHT22. Model AM2302/DHT22
The device will work at 3.3V and it makes use of a 1‑wire open collector-style bus, which makes it very easy to make the physical connection to the Pico.
and the standard way of connecting the device is:
The serial protocol is also fairly simple:
A 1 corresponds to 70µs and a 0 corresponds to 26 to 28µs. This is within the range of pulse measurements that can be achieved using standard library functions. There is also a 50µs low period between each data bit and this can be used to do some limited processing. The time between falling edge transitions is therefore 120µs for a 1 and 76µs for a 0. When trying to work out how to decode a new protocol it often helps to try to answer the question, “how can I tell the difference between a 0 and a 1?” If you have a logic analyzer it can help to look at the waveform and see how you work it out manually. In this case, despite the complex-looking timing diagram, the difference comes down to a short versus a long pulse! |
||||
Last Updated ( Monday, 17 May 2021 ) |