A PIO driver for DHT sensor
Go to file
Jonathan BAUDIN b0ecfd37a3 fix typo
2023-09-15 20:33:46 +02:00
.cargo First commit 2023-09-08 23:09:15 +02:00
src Refactor for Support DHT11 2023-09-09 22:22:30 +02:00
.gitignore First commit 2023-09-08 23:09:15 +02:00
Cargo.toml Refactor for Support DHT11 2023-09-09 22:22:30 +02:00
LICENSE Create LICENSE 2023-09-09 14:23:18 +02:00
readme.md fix typo 2023-09-15 20:33:46 +02:00
readme_fr.md Add usage section 2023-09-09 22:36:15 +02:00

DHT PIO Rust Library

crates.io MIT GitHub

french version

Why?

The DHT (22 or 11) uses a 1-Wire protocol, which is not compatible with the Dallas Semicondutors protocol of the same name. The Raspberry Pico (like other microcontrollers) has no dedicated peripherals for this protocol.

Numerous crates exist for using DHT via a digital pin, but after testing several of them, they don't work reliably. The main problem is the implementation of the embedded_hal by rp2040_hal. Manipulating the state and direction of a pin takes too long (I measured between 2µs and 6µs depending on the action requested). This is due, among other things, to the impossibility of placing a pin in open drain, which requires "simulating" this feature

The PIO ❤️

The RP2040 chip (used for the Pico) has a rather atypical peripheral called PIO (Programmable Input/Output), Chapter 3 of the DataSheet. In simple terms, the idea is to be able to run a small program (max. 32 instructions), which executes independently. It can manipulate GPIOs and share information with the main program.

The PIO is programmed using an assembler called pioasm, with just a few very basic instructions. What's interesting is that each instruction takes (usually) 1 cycle to execute. What's more, it's possible to divide the clock at which the program executes. In our case, we divide the main clock of 125 MHz by 125, giving us one instruction per microsecond.

Usage

First, create and retrieve the PIO objects

let (dht_pio, dht_sm, _, _, _) = pac.PIO0.split(&mut pac.RESETS);

To create a new object:

  • DHT22
    let mut dht = Dht22::new(dht_pio, dht_sm, pins.gpio0.into_function());
    ```
    
  • DHT11
    let mut dht = Dht11::new(dht_pio, dht_sm, pins.gpio0.into_function());
    ```
    
    

Read data:

let dht_data = dht.read(&mut delay);

NB: read retrun a Result<DhtResult, DhtError>.

Support

Board

For the moment, the crates have only been tested on a Raspberry Pico.

DHT

DHT22
DHT11

TODO

  • Finish Readme
  • Add CRC read
  • Check CRC
  • DHT11 support
  • Test DHT11
  • Document code