Skip to content

Commit 42f3514

Browse files
committed
feat: check dht20 status and add callibration
1 parent 2ae8805 commit 42f3514

File tree

2 files changed

+53
-19
lines changed

2 files changed

+53
-19
lines changed

README.md

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ Welcome to `embedded-dht-rs`, a Rust library designed to make working with DHT s
44

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

7-
### Features:
8-
9-
- **DHT11 and DHT22 sensor support**: Both sensors are fully implemented and ready to use.
7+
**Support for DHT11, DHT20, and DHT22 Sensors**: All three sensors are fully implemented and ready for use.
108

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

@@ -78,50 +76,70 @@ fn main() -> ! {
7876

7977
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.
8078

79+
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.
80+
81+
82+
### DHT11/DHT22
8183

8284
![steps](/docs/steps.png)
8385

84-
### Step 1
86+
#### Step 1
8587

8688
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.
8789

88-
### Step 2
90+
#### Step 2
8991

9092
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:
9193

9294
![step2](/docs/step2.png)
9395

9496

95-
### Step 3
97+
#### Step 3
9698

9799
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:
98100

99101
![step3](/docs/step3.png)
100102

101-
### Step 4
103+
#### Step 4
102104

103105
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.
104106

105107
![step4](/docs/step4.png)
106108

107-
### End signal
109+
#### End signal
108110

109111
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.
110112

111113

114+
### DHT20
115+
116+
- SDA = Serial Data Line
117+
- SCL = Serial Clock Line
118+
119+
#### Start the sensor
120+
121+
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).
122+
123+
124+
#### Step 1
125+
126+
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.
127+
128+
129+
#### Step 2
112130

113-
## Comparison of DHT11 and DHT22 40-Bit Data Formats
114131

115-
| Feature | DHT11 | DHT22 |
116-
|-----------------------|-----------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------|
117-
| **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 |
118-
| **Precision** | Integer values only | Includes decimal values for higher precision |
119-
| **Example Temperature** | 25°C | 25.6°C |
120-
| **Example Humidity** | 60% | 60.5% |
121-
| **Example Data Bytes** | `60, 0, 25, 0, 85` | `2, 93, 1, 0, 96` |
122-
| **Measurement Range** | - Temperature: 0–50°C<br>- Humidity: 20–90% | - Temperature: -40–80°C<br>- Humidity: 0–100% |
132+
## Comparison of DHT11, DHT20, and DHT22 40-Bit Data Formats
123133

134+
| Feature | DHT11 | DHT20 | DHT22 |
135+
|-----------------------|----------------------------------------------------|--------------------------------------------------------|---------------------------------------------------------|
136+
| **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 |
137+
| **Precision** | Integer only | Higher precision with decimals | Higher precision with decimals |
138+
| **Example Temp** | 25°C | 25.6°C | 25.6°C |
139+
| **Example Humidity** | 60% | 60.5% | 60.5% |
140+
| **Example Data Bytes** | `60, 0, 25, 0, 85` | `2, 93, 1, 0, CRC` | `2, 93, 1, 0, 96` |
141+
| **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% |
124142

125-
## Example Schematic
143+
## Example Schematic
126144

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

src/dht20.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,23 @@ impl<P: I2c, D: DelayNs> Dht20<P, D> {
1616
}
1717

1818
pub fn read(&mut self) -> Result<SensorReading, SensorError> {
19-
19+
// Check status
20+
let status_request: &[u8] = &[0x71];
21+
let mut status_response: [u8; 1] = [0; 1];
22+
23+
let _ = self.pin.write_read(0x38, status_request, &mut status_response);
24+
if status_response[0] & 0x18 != 0x18 {
25+
// Callibration
26+
let _ = self.pin.write(0x38, &[0x1B, 0, 0]);
27+
let _ = self.pin.write(0x38, &[0x1C, 0, 0]);
28+
let _ = self.pin.write(0x38, &[0x1E, 0, 0]);
29+
}
30+
31+
32+
33+
34+
35+
2036
// TODO
2137
let humidity_percentage = 10.0;
2238
// TODO

0 commit comments

Comments
 (0)