5.2 KiB
DHT11 Temperature & Humidity Sensor for ESP32-C3
ESP-IDF project that reads temperature and humidity data from a DHT11 sensor and prints it to the serial console.
Overview
This project reads data from a DHT11 digital temperature and humidity sensor using the ESP32-C3 microcontroller. It implements the DHT11 communication protocol in software, reads sensor values every 5 seconds, validates the checksum, and displays:
- Current date and time (derived from compile time)
- Chip information (model, cores, features, flash size, silicon revision)
- Humidity percentage with 1 decimal place
- Temperature in Celsius with 1 decimal place
- Checksum validation status
Hardware Requirements
| Component | Details |
|---|---|
| Microcontroller | ESP32-C3 |
| Sensor | DHT11 (AM2302) |
| Connection | Single wire + pull-up resistor |
| Data Pin | GPIO 0 (configurable in main/main.c) |
| Resistor | 4.7kΩ - 10kΩ pull-up between VCC and DATA |
Wiring Diagram
DHT11 ESP32-C3
VCC ------ 3V3
DATA ----- GPIO0
GND ------ GND
Add a 4.7kΩ pull-up resistor between VCC and DATA pin.
Supported Targets
| ESP32 | ESP32-C2 | ESP32-C3 | ESP32-S2 | ESP32-S3 |
|---|---|---|---|---|
| ❌ | ❌ | ✅ | ❌ | ❌ |
Project Structure
dht11_reader/
├── CMakeLists.txt # Project configuration
├── sdkconfig # Build configuration
├── main/
│ ├── CMakeLists.txt # Main component configuration
│ └── main.c # Application source code
└── README.md # This file
Setup & Build
Prerequisites
- ESP-IDF v6.0
- RISC-V toolchain (riscv32-esp-elf)
- Python 3.13+ with virtual environment
Build Instructions
# Activate ESP-IDF environment
source "$HOME/.espressif/tools/activate_idf_v6.0.sh"
# Navigate to project
cd temperature_measure
# Build the project
idf.py build
# Flash to device (replace PORT with your serial port)
idf.py -p /dev/ttyUSB0 flash monitor
Configuration
The DHT11 data pin is defined in main/main.c:
#define DHT11_GPIO 0
To use a different GPIO, change this define and rebuild.
Reading Interval
The sensor is read every 5 seconds:
vTaskDelay(pdMS_TO_TICKS(5000));
Note: DHT11 requires at least 1 second between readings. Adjust as needed.
How It Works
DHT11 Communication Protocol
-
Start Signal: MCU pulls DATA line low for 20ms, then high
-
Sensor Response: DHT11 pulls line low for ~80μs, then high for ~80μs
-
Data Transmission: 40 bits of data (5 bytes) in the following format:
- Byte 0: Humidity integer part
- Byte 1: Humidity decimal part
- Byte 2: Temperature integer part
- Byte 3: Temperature decimal part
- Byte 4: Checksum (sum of bytes 0-3)
-
Bit Encoding: Each bit starts with a ~50μs low pulse:
0: ~26-28μs high pulse1: ~70μs high pulse
Checksum Validation
The checksum byte should equal the sum of the first 4 data bytes. If validation fails, the reading is retried after 1 second.
Output Example
Time: 2024-04-24 22:30:00
This is esp32c3 chip with 1 CPU core(s), WiFi/BLE, silicon revision v0.4, 4MB embedded flash
Minimum free heap size: 283488 bytes
Humidity: 45.2%
Temperature: 23.5°C
Checksum valid: Yes
Troubleshooting
Common Issues
| Issue | Solution |
|---|---|
| No sensor readings / checksum fails | Check wiring: DATA to GPIO0, pull-up resistor present |
| Build fails with missing component | Run idf.py fullclean then rebuild |
driver/gpio.h not found |
Ensure esp_driver_gpio is in PRIV_REQUIRES |
Linker errors with .riscv.attributes |
Your toolchain may be too new; use ESP-IDF 6.0 compatible version |
Debug Mode
To enable debug output:
idf.py monitor
Hardware Connection Check
Verify your wiring with a multimeter:
- GPIO0 should read HIGH when idle (via pull-up)
- 3V3 is supplying power
- GND is properly connected
Technical Details
- Sensor Range: 0-50°C temperature, 20-90% humidity
- Accuracy: ±1°C temperature, ±1% humidity
- Sampling Rate: 1Hz maximum (1 reading per second)
- Resolution: 1° C, 1% humidity
License
This project is provided under the CC0-1.0 license (public domain). See the SPDX identifier in the source file.