| Crates.io | dht20-rs |
| lib.rs | dht20-rs |
| version | 0.2.0 |
| created_at | 2025-03-18 14:22:33.644465+00 |
| updated_at | 2025-03-18 14:41:35.209374+00 |
| description | A `no_std` Rust driver for the DHT20 temperature and humidity sensor. |
| homepage | |
| repository | https://github.com/hoodnoah/dht20 |
| max_upload_size | |
| id | 1596617 |
| size | 22,271 |
A Rust library for interfacing with the DHT20 temperature and humidity sensor. This library provides an easy-to-use API for reading temperature and humidity data from the sensor.
Add dht20 to your Cargo.toml:
[dependencies]
dht20-rs = {version = "0.1.0"}
Then, include it in your project:
use dht20_rs;
Here's a basic example of how to use the library:
#![no_std]
#![no_main]
// use a delay implementation which is compatible with the HAL `DelayNs` trait (HAL 1.0)
use esp_hal::{delay::Delay, main};
// simple `println!` macro to print messages to the serial console
use esp_println::println;
use dht20_rs::Dht20;
#[main]
fn main() -> ! {
// Set up I2C
println!("Setting up I2C...");
let i2c = // Set up your I2C peripheral here
println!("Done.");
// Set up the DHT20 sensor
println!("Setting up DHT20 sensor...");
let mut delay = Delay::new(); // required dependency injection; sensor requires time delays
let mut dht20 = Dht20::new(i2c); // pass your I2C peripheral
// initialize the sensor
if let Err(e) = dht20.init(&mut delay) {
println!("Failed to initialize the DHT20 sensor: {:?}", e);
loop {}
}
println!("Done.");
loop {
// Read temperature and humidity
match dht20.take_reading(&mut delay) {
Ok(reading) => {
println!("Temperature: {}°F", reading.temperature_fahrenheit()); // also supports `temperature_celsius()`
println!("Humidity: {}%", reading.humidity());
}
Err(e) => {
println!("Failed to read from the DHT20 sensor: {:?}", e);
}
}
// wait between readings
delay.delay_millis(10000);
}
}
For detailed documentation, visit docs.rs/dht20.
This project is licensed under the MIT License. See the LICENSE file for details.
Contributions are welcome! Please open an issue or submit a pull request on GitHub.