-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from rust-dd/feature/dht20
Feature/dht20
- Loading branch information
Showing
15 changed files
with
213 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# DHT11/DHT22 | ||
|
||
![steps](/docs/dht11_22_steps.png) | ||
|
||
## Step 1 | ||
|
||
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. | ||
|
||
## Step 2 | ||
|
||
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: | ||
|
||
![step2](/docs/dht11_22_step2.png) | ||
|
||
|
||
## Step 3 | ||
|
||
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: | ||
|
||
![step3](/docs/dht11_22_step3.png) | ||
|
||
## Step 4 | ||
|
||
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. | ||
|
||
![step4](/docs/dht11_22_step4.png) | ||
|
||
## End signal | ||
|
||
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# DHT20 | ||
|
||
![steps](/docs/dht20_steps.png) | ||
|
||
- SDA = Serial Data Line | ||
- SCL = Serial Clock Line | ||
|
||
## Start the sensor | ||
|
||
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). | ||
|
||
## Step 1 | ||
|
||
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. | ||
|
||
## Step 2 | ||
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. | ||
|
||
![step4](/docs/dht20_step2.png) | ||
|
||
## Step 3 | ||
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. | ||
|
||
## Step 4 | ||
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. | ||
|
||
![step4](/docs/dht20_step4.png) | ||
|
||
## Step 5 | ||
Compute the temperature and humidity values. |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
use embedded_hal::{delay::DelayNs, i2c::I2c}; | ||
|
||
use crate::{SensorError, SensorReading}; | ||
|
||
pub struct Dht20<I: I2c, D: DelayNs> { | ||
pub i2c: I, | ||
pub delay: D, | ||
} | ||
|
||
impl<I: I2c, D: DelayNs> Dht20<I, D> { | ||
const SENSOR_ADDRESS: u8 = 0x38; | ||
|
||
pub fn new(i2c: I, delay: D) -> Self { | ||
Self { i2c, delay } | ||
} | ||
|
||
pub fn read(&mut self) -> Result<SensorReading<f32>, SensorError> { | ||
// Check status | ||
let mut status_response: [u8; 1] = [0; 1]; | ||
let _ = self | ||
.i2c | ||
.write_read(Self::SENSOR_ADDRESS, &[0x71], &mut status_response); | ||
|
||
// Callibration if needed | ||
if status_response[0] & 0x18 != 0x18 { | ||
let _ = self.i2c.write(Self::SENSOR_ADDRESS, &[0x1B, 0, 0]); | ||
let _ = self.i2c.write(Self::SENSOR_ADDRESS, &[0x1C, 0, 0]); | ||
let _ = self.i2c.write(Self::SENSOR_ADDRESS, &[0x1E, 0, 0]); | ||
} | ||
|
||
// Trigger the measurement | ||
self.delay.delay_ms(10); | ||
let _ = self.i2c.write(Self::SENSOR_ADDRESS, &[0xAC, 0x33, 0x00]); | ||
|
||
// Read the measurement status | ||
self.delay.delay_ms(80); | ||
loop { | ||
let mut measurement_status_response: [u8; 1] = [0; 1]; | ||
let _ = self | ||
.i2c | ||
.read(Self::SENSOR_ADDRESS, &mut measurement_status_response); | ||
let status_word = measurement_status_response[0]; | ||
if status_word & 0b1000_0000 == 0 { | ||
break; | ||
} | ||
self.delay.delay_ms(1); | ||
} | ||
|
||
// Read the measurement (1 status + 5 data + 1 crc) | ||
let mut measurement_response: [u8; 7] = [0; 7]; | ||
let _ = self | ||
.i2c | ||
.read(Self::SENSOR_ADDRESS, &mut measurement_response); | ||
|
||
// Humidity 20 bits (8 + 8 + 4) | ||
let mut raw_humidity = measurement_response[1] as u32; | ||
raw_humidity = (raw_humidity << 8) + measurement_response[2] as u32; | ||
raw_humidity = (raw_humidity << 4) + (measurement_response[3] >> 4) as u32; | ||
let humidity_percentage = (raw_humidity as f32 / ((1 << 20) as f32)) * 100.0; | ||
|
||
// Temperature 20 bits | ||
let mut raw_temperature = (measurement_response[3] & 0b1111) as u32; | ||
raw_temperature = (raw_temperature << 8) + measurement_response[4] as u32; | ||
raw_temperature = (raw_temperature << 8) + measurement_response[5] as u32; | ||
let temperatue_percentage = (raw_temperature as f32 / ((1 << 20) as f32)) * 200.0 - 50.0; | ||
|
||
// Compare the calculated CRC with the received CRC | ||
let data = &measurement_response[..6]; | ||
let received_crc = measurement_response[6]; | ||
let calculcated_crc = Self::calculate_crc(data); | ||
if received_crc != calculcated_crc { | ||
return Err(SensorError::ChecksumMismatch); | ||
} | ||
|
||
Ok(SensorReading { | ||
humidity: humidity_percentage, | ||
temperature: temperatue_percentage, | ||
}) | ||
} | ||
|
||
fn calculate_crc(data: &[u8]) -> u8 { | ||
let polynomial = 0x31u8; // x^8 + x^5 + x^4 + 1 | ||
let mut crc = 0xFFu8; | ||
|
||
for &byte in data { | ||
crc ^= byte; | ||
// CRC8 - process every bit | ||
for _ in 0..8 { | ||
if crc & 0x80 != 0 { | ||
crc = (crc << 1) ^ polynomial; | ||
} else { | ||
crc <<= 1; | ||
} | ||
} | ||
} | ||
|
||
crc | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
mod dht; | ||
pub mod dht11; | ||
pub mod dht20; | ||
pub mod dht22; | ||
|
||
/// Represents a reading from the sensor. | ||
|