This project demonstrates how to interface a DHT22 sensor with an STM32 microcontroller using the single-wire protocol. It features real-time data output over UART and includes complete Proteus simulation support.
The DHT22 (AM2302) is a digital sensor capable of measuring both temperature and humidity with higher accuracy and a wider range than the DHT11. It communicates over a single-wire protocol, making it efficient for embedded applications. With its precise measurements, the DHT22 is commonly used in weather monitoring, HVAC systems, and IoT devices.
STM32CubeMX Setup- MCU Selection: STM32F103C6 (16 MHz clock)
- GPIO Configuration:
-
PA0 as Open-Drain Output (DATA line)
- TIM1 Configuration:
-
Prescaler = 15
-
Counter Period = 65535 (1 µs resolution)
- UART1 Configuration:
-
115200 baud
-
8-bit data
-
no parity
- Generate initialization code and open the project in STM32CubeIDE.
Key Functions:
- void DHT22_Init(); → Configures GPIO and TIM1 for µs delays
- void DHT22_Read(float *temp, float *humidity); → Reads 40-bit sensor data
- Checksum Validation:
-
if (checksum == (RH_H + RH_L + T_H + T_L));
→ Data integrity verified
- UART Output:
Components:
- STM32F103C6
- DHT22 Sensor
- Virtual Terminal
Connections:
- DATA (PA0) → DHT22 DATA
- UART1-TX (PA9) → Virtual Terminal RX
Simulation:
- Load the compiled.hex file into STM32
- Observe real-time temperature and humidity values in the virtual terminal
If you have any questions or suggestions don't hesitate to leave a comment below
Comments