Skip to content

Commit 3176cab

Browse files
committed
feat: calculate and check crc; update docs
1 parent 7e0dd74 commit 3176cab

File tree

8 files changed

+139
-115
lines changed

8 files changed

+139
-115
lines changed

README.md

Lines changed: 2 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -91,70 +91,9 @@ We have gathered all the information you need to understand in order to implemen
9191
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.
9292

9393

94-
### DHT11/DHT22
94+
- [DHT11 and DHT22 Documentation](docs/dht11_22.md)
95+
- [DHT20 Documentation](docs/dht20.md)
9596

96-
![steps](/docs/steps.png)
97-
98-
#### Step 1
99-
100-
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.
101-
102-
#### Step 2
103-
104-
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:
105-
106-
![step2](/docs/step2.png)
107-
108-
109-
#### Step 3
110-
111-
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:
112-
113-
![step3](/docs/step3.png)
114-
115-
#### Step 4
116-
117-
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.
118-
119-
![step4](/docs/step4.png)
120-
121-
#### End signal
122-
123-
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.
124-
125-
126-
### DHT20
127-
128-
![steps](/docs/dht20_steps.png)
129-
130-
- SDA = Serial Data Line
131-
- SCL = Serial Clock Line
132-
133-
#### Start the sensor
134-
135-
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).
136-
137-
138-
#### Step 1
139-
140-
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.
141-
142-
143-
#### Step 2
144-
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.
145-
146-
![step4](/docs/dht20_step2.png)
147-
148-
#### Step 3
149-
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.
150-
151-
#### Step 4
152-
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.
153-
154-
![step4](/docs/dht20_step4.png)
155-
156-
#### Step 5
157-
Compute the temperature and humidity values.
15897

15998

16099
## Comparison of DHT11, DHT20, and DHT22 40-Bit Data Formats

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.

src/dht20.rs

Lines changed: 77 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -7,67 +7,92 @@ pub struct Dht20<I: I2c, D: DelayNs> {
77
pub delay: D,
88
}
99

10-
impl<I: I2c, D: DelayNs> Dht20<I, D> {
11-
const SENSOR_ADDRESS: u8 = 0x38;
10+
impl<I: I2c, D: DelayNs> Dht20<I, D> {
11+
const SENSOR_ADDRESS: u8 = 0x38;
1212

13-
pub fn new(i2c: I, delay: D) -> Self {
14-
Self {
15-
i2c,
16-
delay,
13+
pub fn new(i2c: I, delay: D) -> Self {
14+
Self {
15+
i2c,
16+
delay,
17+
}
1718
}
18-
}
1919

20-
pub fn read(&mut self) -> Result<SensorReading<f32>, SensorError> {
21-
// Check status
22-
let mut status_response: [u8; 1] = [0; 1];
23-
let _ = self.i2c.write_read(Self::SENSOR_ADDRESS, &[0x71], &mut status_response);
24-
25-
if status_response[0] & 0x18 != 0x18 {
26-
// Callibration
27-
let _ = self.i2c.write(Self::SENSOR_ADDRESS, &[0x1B, 0, 0]);
28-
let _ = self.i2c.write(Self::SENSOR_ADDRESS, &[0x1C, 0, 0]);
29-
let _ = self.i2c.write(Self::SENSOR_ADDRESS, &[0x1E, 0, 0]);
30-
}
31-
32-
// Trigger the measurement
33-
self.delay.delay_ms(10);
34-
let _ = self.i2c.write(Self::SENSOR_ADDRESS, &[0xAC, 0x33, 0x00]);
35-
36-
// Read the measurement status
37-
self.delay.delay_ms(80);
38-
loop {
39-
let mut measurement_status_response: [u8; 1] = [0; 1];
40-
let _ = self.i2c.read(Self::SENSOR_ADDRESS, &mut measurement_status_response);
41-
let status_word = measurement_status_response[0];
42-
if status_word & 0b1000_0000 == 0 {
43-
break;
20+
pub fn read(&mut self) -> Result<SensorReading<f32>, SensorError> {
21+
// Check status
22+
let mut status_response: [u8; 1] = [0; 1];
23+
let _ = self.i2c.write_read(Self::SENSOR_ADDRESS, &[0x71], &mut status_response);
24+
25+
// Callibration if needed
26+
if status_response[0] & 0x18 != 0x18 {
27+
let _ = self.i2c.write(Self::SENSOR_ADDRESS, &[0x1B, 0, 0]);
28+
let _ = self.i2c.write(Self::SENSOR_ADDRESS, &[0x1C, 0, 0]);
29+
let _ = self.i2c.write(Self::SENSOR_ADDRESS, &[0x1E, 0, 0]);
4430
}
45-
self.delay.delay_ms(1);
46-
}
31+
32+
// Trigger the measurement
33+
self.delay.delay_ms(10);
34+
let _ = self.i2c.write(Self::SENSOR_ADDRESS, &[0xAC, 0x33, 0x00]);
35+
36+
// Read the measurement status
37+
self.delay.delay_ms(80);
38+
loop {
39+
let mut measurement_status_response: [u8; 1] = [0; 1];
40+
let _ = self.i2c.read(Self::SENSOR_ADDRESS, &mut measurement_status_response);
41+
let status_word = measurement_status_response[0];
42+
if status_word & 0b1000_0000 == 0 {
43+
break;
44+
}
45+
self.delay.delay_ms(1);
46+
}
47+
48+
// Read the measurement (1 status + 5 data + 1 crc)
49+
let mut measurement_response: [u8; 7] = [0; 7];
50+
let _ = self.i2c.read(Self::SENSOR_ADDRESS, &mut measurement_response);
4751

48-
// Read the measurement (1 status + 5 data + 1 crc)
49-
let mut measurement_response: [u8; 7] = [0; 7];
50-
let _ = self.i2c.read(Self::SENSOR_ADDRESS, &mut measurement_response);
52+
// Humidity 20 bits (8 + 8 + 4)
53+
let mut raw_humidity = measurement_response[1] as u32;
54+
raw_humidity = (raw_humidity << 8) + measurement_response[2] as u32;
55+
raw_humidity = (raw_humidity << 4) + (measurement_response[3] >> 4) as u32;
56+
let humidity_percentage = (raw_humidity as f32/ ((1 << 20) as f32)) * 100.0;
5157

52-
// Humidity 20 bits (8 + 8 + 4)
53-
let mut raw_humidity = measurement_response[1] as u32;
54-
raw_humidity = (raw_humidity << 8) + measurement_response[2] as u32;
55-
raw_humidity = (raw_humidity << 4) + (measurement_response[3] >> 4) as u32;
56-
let humidity_percentage = (raw_humidity as f32/ ((1 << 20) as f32)) * 100.0;
5758

59+
// Temperature 20 bits
60+
let mut raw_temperature = (measurement_response[3] & 0b1111) as u32;
61+
raw_temperature = (raw_temperature << 8) + measurement_response[4] as u32;
62+
raw_temperature = (raw_temperature << 8) + measurement_response[5] as u32;
63+
let temperatue_percentage = (raw_temperature as f32 / ((1 << 20) as f32)) * 200.0 - 50.0;
5864

59-
// Temperature 20 bits
60-
let mut raw_temperature = (measurement_response[3] & 0b1111) as u32;
61-
raw_temperature = (raw_temperature << 8) + measurement_response[4] as u32;
62-
raw_temperature = (raw_temperature << 8) + measurement_response[5] as u32;
63-
let temperatue_percentage = (raw_temperature as f32 / ((1 << 20) as f32)) * 200.0 - 50.0;
65+
// Compare the calculated CRC with the received CRC
66+
let data = &measurement_response[..6];
67+
let received_crc = measurement_response[6];
68+
let calculcated_crc = Self::calculate_crc(data);
69+
if received_crc != calculcated_crc {
70+
return Err(SensorError::ChecksumMismatch);
71+
}
72+
73+
Ok(SensorReading {
74+
humidity: humidity_percentage as f32,
75+
temperature: temperatue_percentage as f32,
76+
})
77+
}
6478

65-
// CRC 8 bits
6679

80+
fn calculate_crc(data: &[u8]) -> u8 {
81+
let polynomial = 0x31u8; // x^8 + x^5 + x^4 + 1
82+
let mut crc = 0xFFu8;
83+
84+
for &byte in data {
85+
crc ^= byte;
86+
// CRC8 - process every bit
87+
for _ in 0..8 {
88+
if crc & 0x80 != 0 {
89+
crc = (crc << 1) ^ polynomial;
90+
} else {
91+
crc <<= 1;
92+
}
93+
}
94+
}
95+
return 10;
96+
}
6797

68-
Ok(SensorReading {
69-
humidity: humidity_percentage as f32,
70-
temperature: temperatue_percentage as f32,
71-
})
7298
}
73-
}

0 commit comments

Comments
 (0)