Crates.io | dht11_gpio |
lib.rs | dht11_gpio |
version | 0.1.0 |
source | src |
created_at | 2024-01-09 11:15:32.436522 |
updated_at | 2024-01-09 11:15:32.436522 |
description | Simple implimentation for interfacing with the dht11 sensor to retrieve temperature and humidity |
homepage | |
repository | https://github.com/t4skmanag3r/dht11_gpio |
max_upload_size | |
id | 1093803 |
size | 14,454 |
Rust library for interfacing with the DHT11 temperature & humidity sensor.
Credit goes to this guide on how to properly wire up the sensor - circuitbasics.com
Connect the Vcc (+)
to the 3.3v Power
, NOT the 5v Power pin, because the signal would exceed the standard 3.3v of power the input pins take by using a pull-up resistor.
Add the crate to your project:
cargo add dht11_gpio
or add to Cargo.toml manually:
[dependencies]
dht11_gpio = "0.1.0"
check for the latest version on crates.io/dht11_gpio
use dht11_gpio::{DHT11Controller, Sensor};
fn main() {
const DHT11_PIN: u8 = 4;
let mut sensor = DHT11Controller::new(DHT11_PIN).unwrap();
let result = sensor.read_sensor_data();
match result {
Ok(data) => {
println!("temperature: {} °C", data.temperature);
println!("humidity: {} %", data.humidity);
}
Err(err) => {
println!("error: {}", err);
}
}
}
note: the sensor returns the temperature in celcius
The DHT11Controller::read_sensor_data()
method can fail to retrieve the correct sensor data if:
Bit Count Mismatch (DHT11Error::MissingData
):
Invalid Checksum (DHT11Error::InvalidChecksum
):
The implimentation of read_sensor_data()
is not perfect as it is implemented with a fixed 200ms timeout for receiving the data from the sensor, this sometimes leads to getting the MissingData
error.