Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A Rust library that provides full support for DHT11, DHT22, and DHT20 (AHT20) temperature and humidity sensors.

License

NotificationsYou must be signed in to change notification settings

rust-dd/embedded-dht-rs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

54 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

build workflowCrates.ioCrates.ioAPI

embedded-dht-rs

embedded-dht-rs is a Rust library designed to simplify interfacing with DHT sensors in embedded systems.

This library is#![no_std] and depends only onembedded_hal, making it versatile and compatible with virtually any microcontroller.

Support for DHT11, DHT20, and DHT22 Sensors: All three sensors are fully implemented and ready for use.

The library has been tested with the ESP32-WROOM, and a detailed example is provided below to help you get started.

Getting Started

Tutorials

Here are some general tutorials that provide brief introductions to embedded programming:

Install

To include thedht11 feature:

cargo add embedded-dht-rs --features"dht11"

To include all features (dht11,dht20, anddht22):

cargo add embedded-dht-rs --features"dht11,dht20,dht22"

Example - ESP32

You can find the full example in theexamples folder.

#![no_std]#![no_main]use embedded_dht_rs::{dht11::Dht11, dht20::Dht20, dht22::Dht22};use esp_backtraceas _;use esp_hal::{    clock::ClockControl, delay::Delay, gpio::{Io,Level,OutputOpenDrain,Pull}, i2c::I2C, peripherals::Peripherals, prelude::*, system::SystemControl};use fugit::HertzU32;#[entry]fnmain() -> !{let peripherals =Peripherals::take();let system =SystemControl::new(peripherals.SYSTEM);let clocks =ClockControl::boot_defaults(system.clock_control).freeze();let io =Io::new(peripherals.GPIO, peripherals.IO_MUX);    esp_println::logger::init_logger_from_env();let delay =Delay::new(&clocks);let od_for_dht11 =OutputOpenDrain::new(io.pins.gpio4,Level::High,Pull::None);let od_for_dht22 =OutputOpenDrain::new(io.pins.gpio5,Level::High,Pull::None);let i2c_for_dht20 =I2C::new(        peripherals.I2C0,         io.pins.gpio21,        io.pins.gpio22,HertzU32::kHz(400),&clocks);letmut dht11 =Dht11::new(od_for_dht11, delay);letmut dht22 =Dht22::new(od_for_dht22, delay);letmut dht20 =Dht20::new(i2c_for_dht20, delay);loop{        delay.delay(5000.millis());match dht11.read(){Ok(sensor_reading) => log::info!("DHT 11 Sensor - Temperature: {} °C, humidity: {} %",                sensor_reading.temperature,                sensor_reading.humidity),Err(error) => log::error!("An error occurred while trying to read sensor: {:?}", error),}match dht22.read(){Ok(sensor_reading) => log::info!("DHT 22 Sensor - Temperature: {} °C, humidity: {} %",                sensor_reading.temperature,                sensor_reading.humidity),Err(error) => log::error!("An error occurred while trying to read sensor: {:?}", error),}match dht20.read(){Ok(sensor_reading) => log::info!("DHT 20 Sensor - Temperature: {} °C, humidity: {} %",                sensor_reading.temperature,                sensor_reading.humidity),Err(error) => log::error!("An error occurred while trying to read sensor: {:?}", error),}        log::info!("-----");}}

running

Implementation Specification

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.

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.

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

FeatureDHT11DHT20DHT22
Data Structure- Byte 1: Humidity Int
- Byte 2: Humidity Dec (0)
- Byte 3: Temp Int
- Byte 4: Temp Dec (0)
- Byte 5: Checksum
- Byte 1: Humidity High
- Byte 2: Humidity Low
- Byte 3: Temp High
- Byte 4: Temp Low
- Byte 5: CRC
- Byte 1: Humidity High
- Byte 2: Humidity Low
- Byte 3: Temp High
- Byte 4: Temp Low
- Byte 5: Checksum
PrecisionInteger onlyHigher precision with decimalsHigher precision with decimals
Example Temp25°C25.6°C25.6°C
Example Humidity60%60.5%60.5%
Example Data Bytes60, 0, 25, 0, 852, 93, 1, 0, CRC2, 93, 1, 0, 96
RangeTemp: 0–50°C
Hum: 20–90%
Temp: -40–80°C
Hum: 10–90%
Temp: -40–80°C
Hum: 0–100%

Example Schematic

SchematicClick to zoomRunningClick to zoom

About

A Rust library that provides full support for DHT11, DHT22, and DHT20 (AHT20) temperature and humidity sensors.

Topics

Resources

License

Stars

Watchers

Forks

Contributors4

  •  
  •  
  •  
  •  

Languages


[8]ページ先頭

©2009-2025 Movatter.jp