ESP32
Read DHT11 temperature and humidity on ESP32-C3
Read a DHT11 temperature and humidity sensor with Rust on ESP32-C3, then build, flash, and monitor it with cargo run.
Contents
Overview and result#
Skip the usual
Hello WorldLED example and read aDHT11temperature and humidity sensor directly.
Experiment details#
| Item | Details |
|---|---|
| Target chip | ESP32-C3 |
| Sensor | DHT11 temperature and humidity module |
| Data pin | GPIO0 |
| Prerequisites | Base Rust environment + ESP32 Rust environment |
| Expected result | The terminal continuously prints temperature and humidity data |

The final output looks like this.

The
DHT11readings appear directly in the terminal without an extra serial monitor.
Generate the template project#
The process is the same as the previous guide, so follow Create an ESP32 starter project with esp-generate.
Open the project#
- Add the required
cratefrom the project root:
cargo add esp32-dht11-rs
Add the following code to main.rs.
- Import the required
crateat the top:
use esp32_dht11_rs::{DHT11};
use esp_hal::delay::Delay;
- Then initialize the sensor in
main:
let delay = Delay::new();
let mut dht11 = DHT11::new(peripherals.GPIO0, delay);
let mut dht11_read_counter = 1;
- Copy this block into the
loop:
match dht11.read() {
Ok(m) => println!("DHT11 TEMP is: {}℃ DHT11 HUMI is:{}% measuring for [NO.{:?}] time", m.temperature, m.humidity, dht11_read_counter),
Err(error) => println!("error occurred: {:?}", error),
}
Timer::after_millis(2000).await;
dht11_read_counter += 1;
- With the board powered off, connect the hardware:
# esp32c3 # dht11
gpio0 -> dat
vcc -> vcc
gnd -> gnd
- Connect the board to the computer and run:
Install the
CH34xdriver separately if your board requires it.
cargo run --release
The terminal will show the compiling step.

Flashing starts automatically; you normally do not need to press any board button, including BOOT.

After flashing, readings should print in the terminal. Holding the DHT11 module will quickly raise humidity and slowly raise temperature, confirming that the sensor and program work.

Troubleshooting#
- If the terminal is empty, check that
GPIO0,VCC, andGNDare wired correctly and that the sensor voltage matches the board. - If
cargo run --releasecannot flash, verify thatespflashis installed and that no other program owns the board's serial port. - If compilation cannot find
esp32_dht11_rs, return to the project root and runcargo add esp32-dht11-rsagain.
Discussion
Comments and discussion
Sign in with GitHub to leave a comment, ask a question, or add context.