Skip to content

Commit 73d0a11

Browse files
authored
Merge pull request #1 from rust-dd/feature/dht20
Feature/dht20
2 parents 37b3dca + f14880c commit 73d0a11

15 files changed

+213
-55
lines changed

README.md

Lines changed: 52 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,35 @@ Welcome to `embedded-dht-rs`, a Rust library designed to make working with DHT s
88

99
This library only depends on `embedded_hal`, making it versatile and compatible with virtually any microcontroller.
1010

11-
### Features:
12-
13-
- **DHT11 and DHT22 sensor support**: Both sensors are fully implemented and ready to use.
11+
**Support for DHT11, DHT20, and DHT22 Sensors**: All three sensors are fully implemented and ready for use.
1412

1513
We’ve tested it with the ESP32-WROOM, and you can find a detailed example below to help you get started.
1614

1715
## Getting Started
1816

19-
### Example
17+
### Tutorials
18+
19+
Here are some general tutorials that provide brief introductions to embedded programming:
20+
21+
- **Part 1 (Introduction)** - [Introduction to Embedded Systems with Rust: A Beginner's Guide Using ESP32](https://rust-dd.com/post/introduction-to-embedded-systems-with-rust-a-beginner-s-guide-using-esp32)
22+
- **Part 2 (LED + Button)** - [Building a Simple LED and Button Interface with Rust on ESP32](https://rust-dd.com/post/building-a-simple-led-and-button-interface-with-rust-on-esp32)
23+
24+
25+
### Example - ESP32
26+
27+
![running](/docs/example_esp32_wired.jpg)
2028

2129
```rust
2230
#![no_std]
2331
#![no_main]
2432

25-
use embedded_dht_rs::{dht11::Dht11, dht22::Dht22};
33+
use embedded_dht_rs::{dht11::Dht11, dht20::Dht20, dht22::Dht22};
2634
use esp_backtrace as _;
2735
use esp_hal::{
28-
clock::ClockControl,
29-
delay::Delay,
30-
gpio::{Io, Level, OutputOpenDrain, Pull},
31-
peripherals::Peripherals,
32-
prelude::*,
33-
system::SystemControl,
36+
clock::ClockControl, delay::Delay, gpio::{Io, Level, OutputOpenDrain, Pull}, i2c::I2C, peripherals::Peripherals, prelude::*, system::SystemControl
3437
};
38+
use fugit::HertzU32;
39+
3540
#[entry]
3641
fn main() -> ! {
3742
let peripherals = Peripherals::take();
@@ -43,11 +48,20 @@ fn main() -> ! {
4348

4449
let delay = Delay::new(&clocks);
4550

46-
let gpio4 = OutputOpenDrain::new(io.pins.gpio4, Level::High, Pull::None);
47-
let gpio5 = OutputOpenDrain::new(io.pins.gpio5, Level::High, Pull::None);
48-
49-
let mut dht11 = Dht11::new(gpio4, delay);
50-
let mut dht22 = Dht22::new(gpio5, delay);
51+
let od_for_dht11 = OutputOpenDrain::new(io.pins.gpio4, Level::High, Pull::None);
52+
let od_for_dht22 = OutputOpenDrain::new(io.pins.gpio5, Level::High, Pull::None);
53+
let i2c_for_dht20 = I2C::new(
54+
peripherals.I2C0,
55+
io.pins.gpio21,
56+
io.pins.gpio22,
57+
HertzU32::kHz(400),
58+
&clocks,
59+
None,
60+
);
61+
62+
let mut dht11 = Dht11::new(od_for_dht11, delay);
63+
let mut dht22 = Dht22::new(od_for_dht22, delay);
64+
let mut dht20 = Dht20::new(i2c_for_dht20, delay);
5165

5266
loop {
5367
delay.delay(5000.millis());
@@ -70,6 +84,15 @@ fn main() -> ! {
7084
Err(error) => log::error!("An error occurred while trying to read sensor: {:?}", error),
7185
}
7286

87+
match dht20.read() {
88+
Ok(sensor_reading) => log::info!(
89+
"DHT 20 Sensor - Temperature: {} °C, humidity: {} %",
90+
sensor_reading.temperature,
91+
sensor_reading.humidity
92+
),
93+
Err(error) => log::error!("An error occurred while trying to read sensor: {:?}", error),
94+
}
95+
7396
log::info!("-----");
7497
}
7598
}
@@ -82,50 +105,25 @@ fn main() -> ! {
82105

83106
We have gathered all the information you need to understand in order to implement a library like this. Additionally, we’ve included a few comments in the code for those curious about the details, based on the following specification.
84107

108+
The DHT20 differs from the DHT11 and DHT22 because it uses the I2C communication protocol, while both the DHT11 and DHT22 rely on a single-wire signal for data transmission.
85109

86-
![steps](/docs/steps.png)
87-
88-
### Step 1
89-
90-
After powering on the DHT11/DHT22 (once powered, allow 1 second to pass during which the sensor stabilizes; during this time, no commands should be sent), it measures the temperature and humidity of the surrounding environment and stores the data. Meanwhile, the DATA line of the DHT11/DHT22 is kept high by a pull-up resistor. The DATA pin of the DHT11/DHT22 is in input mode, ready to detect any external signals.
91-
92-
### Step 2
93-
94-
The microprocessor's I/O pin is set to output mode and pulled low, holding this state for at least 18 milliseconds. Then, the microprocessor's I/O is switched to input mode. Due to the pull-up resistor, the microprocessor’s I/O line and the DHT11/DHT22 DATA line will remain high, waiting for the DHT11/DHT22 to respond with a signal, as illustrated below:
95-
96-
![step2](/docs/step2.png)
97-
98-
99-
### Step 3
100-
101-
The DHT11/DHT22’s DATA pin detects an external signal and goes low, indicating that it is waiting for the external signal to complete. Once the signal ends, the DHT11/DHT22’s DATA pin switches to output mode, producing a low signal for 80 microseconds as a response. This is followed by an 80-microsecond high signal, notifying the microprocessor that the sensor is ready to transmit data. At this point, the microprocessor's I/O pin, still in input mode, detects the low signal from the DHT11/DHT22 (indicating the response) and then waits for the 80-microsecond high signal to start receiving data. The sequence of signal transmission is illustrated below:
102-
103-
![step3](/docs/step3.png)
104-
105-
### Step 4
106-
107-
The DHT11/DHT22 outputs 40 bits of data through the DATA pin, and the microprocessor receives these 40 data bits. The format for a data bit "0" consists of a low level lasting 50 microseconds, followed by a high level lasting 26-28 microseconds, depending on changes in the I/O level. For a data bit "1," the format includes a low level of 50 microseconds followed by a high level lasting up to 70 microseconds. The signal formats for data bits "0" and "1" are shown below.
108-
109-
![step4](/docs/step4.png)
110-
111-
### End signal
112-
113-
After outputting a low signal for 50 microseconds, the DHT11/DHT22 completes sending the 40 bits of data and switches the DATA pin back to input mode, which, along with the pull-up resistor, returns to a high state. Meanwhile, the DHT11/DHT22 internally re-measures the environmental temperature and humidity, records the new data, and waits for the next external signal.
114110

111+
- [DHT11 and DHT22 Documentation](docs/dht11_22.md)
112+
- [DHT20 Documentation](docs/dht20.md)
115113

116114

117-
## Comparison of DHT11 and DHT22 40-Bit Data Formats
118115

119-
| Feature | DHT11 | DHT22 |
120-
|-----------------------|-----------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------|
121-
| **Data Structure** | - **Byte 1:** Humidity Integer Part<br>- **Byte 2:** Humidity Decimal Part (always 0)<br>- **Byte 3:** Temperature Integer Part<br>- **Byte 4:** Temperature Decimal Part (always 0)<br>- **Byte 5:** Checksum | - **Byte 1:** Humidity High Byte<br>- **Byte 2:** Humidity Low Byte<br>- **Byte 3:** Temperature High Byte<br>- **Byte 4:** Temperature Low Byte<br>- **Byte 5:** Checksum |
122-
| **Precision** | Integer values only | Includes decimal values for higher precision |
123-
| **Example Temperature** | 25°C | 25.6°C |
124-
| **Example Humidity** | 60% | 60.5% |
125-
| **Example Data Bytes** | `60, 0, 25, 0, 85` | `2, 93, 1, 0, 96` |
126-
| **Measurement Range** | - Temperature: 0–50°C<br>- Humidity: 20–90% | - Temperature: -40–80°C<br>- Humidity: 0–100% |
116+
## Comparison of DHT11, DHT20, and DHT22 40-Bit Data Formats
127117

118+
| Feature | DHT11 | DHT20 | DHT22 |
119+
|-----------------------|----------------------------------------------------|--------------------------------------------------------|---------------------------------------------------------|
120+
| **Data Structure** | - Byte 1: Humidity Int<br>- Byte 2: Humidity Dec (0)<br>- Byte 3: Temp Int<br>- Byte 4: Temp Dec (0)<br>- Byte 5: Checksum | - Byte 1: Humidity High<br>- Byte 2: Humidity Low<br>- Byte 3: Temp High<br>- Byte 4: Temp Low<br>- Byte 5: CRC | - Byte 1: Humidity High<br>- Byte 2: Humidity Low<br>- Byte 3: Temp High<br>- Byte 4: Temp Low<br>- Byte 5: Checksum |
121+
| **Precision** | Integer only | Higher precision with decimals | Higher precision with decimals |
122+
| **Example Temp** | 25°C | 25.6°C | 25.6°C |
123+
| **Example Humidity** | 60% | 60.5% | 60.5% |
124+
| **Example Data Bytes** | `60, 0, 25, 0, 85` | `2, 93, 1, 0, CRC` | `2, 93, 1, 0, 96` |
125+
| **Range** | Temp: 0–50°C<br>Hum: 20–90% | Temp: -40–80°C<br>Hum: 10–90% | Temp: -40–80°C<br>Hum: 0–100% |
128126

129-
## Example Schematic
127+
## Example Schematic
130128

131129
![step3](/docs/example_esp32_dht11.png)

docs/dht11_22.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# DHT11/DHT22
2+
3+
![steps](/docs/dht11_22_steps.png)
4+
5+
## Step 1
6+
7+
After powering on the DHT11/DHT22 (once powered, allow 1 second to pass during which the sensor stabilizes; during this time, no commands should be sent), it measures the temperature and humidity of the surrounding environment and stores the data. Meanwhile, the DATA line of the DHT11/DHT22 is kept high by a pull-up resistor. The DATA pin of the DHT11/DHT22 is in input mode, ready to detect any external signals.
8+
9+
## Step 2
10+
11+
The microprocessor's I/O pin is set to output mode and pulled low, holding this state for at least 18 milliseconds. Then, the microprocessor's I/O is switched to input mode. Due to the pull-up resistor, the microprocessor’s I/O line and the DHT11/DHT22 DATA line will remain high, waiting for the DHT11/DHT22 to respond with a signal, as illustrated below:
12+
13+
![step2](/docs/dht11_22_step2.png)
14+
15+
16+
## Step 3
17+
18+
The DHT11/DHT22’s DATA pin detects an external signal and goes low, indicating that it is waiting for the external signal to complete. Once the signal ends, the DHT11/DHT22’s DATA pin switches to output mode, producing a low signal for 80 microseconds as a response. This is followed by an 80-microsecond high signal, notifying the microprocessor that the sensor is ready to transmit data. At this point, the microprocessor's I/O pin, still in input mode, detects the low signal from the DHT11/DHT22 (indicating the response) and then waits for the 80-microsecond high signal to start receiving data. The sequence of signal transmission is illustrated below:
19+
20+
![step3](/docs/dht11_22_step3.png)
21+
22+
## Step 4
23+
24+
The DHT11/DHT22 outputs 40 bits of data through the DATA pin, and the microprocessor receives these 40 data bits. The format for a data bit "0" consists of a low level lasting 50 microseconds, followed by a high level lasting 26-28 microseconds, depending on changes in the I/O level. For a data bit "1," the format includes a low level of 50 microseconds followed by a high level lasting up to 70 microseconds. The signal formats for data bits "0" and "1" are shown below.
25+
26+
![step4](/docs/dht11_22_step4.png)
27+
28+
## End signal
29+
30+
After outputting a low signal for 50 microseconds, the DHT11/DHT22 completes sending the 40 bits of data and switches the DATA pin back to input mode, which, along with the pull-up resistor, returns to a high state. Meanwhile, the DHT11/DHT22 internally re-measures the environmental temperature and humidity, records the new data, and waits for the next external signal.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

docs/dht20.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# DHT20
2+
3+
![steps](/docs/dht20_steps.png)
4+
5+
- SDA = Serial Data Line
6+
- SCL = Serial Clock Line
7+
8+
## Start the sensor
9+
10+
The initial step is to supply power to the sensor using the chosen VDD voltage, which can range from 2.2V to 5.5V. Once powered on, the sensor requires less than 100ms to stabilize (with SCL held high during this period) before entering the idle state, after which it is ready to accept commands from the host (MCU).
11+
12+
## Step 1
13+
14+
After powering on, wait at least 100ms. Before reading the temperature and humidity values, retrieve a status byte by sending 0x71. If the result of the status byte and 0x18 is not equal to 0x18, initialize the 0x1B, 0x1C, and 0x1E registers.
15+
16+
## Step 2
17+
Wait for 10ms before sending the 0xAC command to trigger the measurement. The command consists of two bytes: the first byte is 0x33 and the second byte is 0x00.
18+
19+
![step4](/docs/dht20_step2.png)
20+
21+
## Step 3
22+
Wait for 80ms for the measurement to complete. If Bit [7] of the status word is 0, the measurement is done, and you can proceed to read six bytes continuously; if not, continue waiting.
23+
24+
## Step 4
25+
After receiving the six bytes, the following byte is the CRC check data, which can be read if needed. If the receiver requires CRC validation, an ACK is sent after the sixth byte is received; otherwise, send a NACK to terminate. The initial CRC value is 0xFF, and the CRC8 check uses the polynomial: CRC [7:0] = 1 + X^4 + X^5 + X^8.
26+
27+
![step4](/docs/dht20_step4.png)
28+
29+
## Step 5
30+
Compute the temperature and humidity values.

docs/dht20_step2.png

92.6 KB
Loading

docs/dht20_step4.png

259 KB
Loading

docs/dht20_steps.png

84.9 KB
Loading

0 commit comments

Comments
 (0)