Skip to content

Lesson 10: DHT20 Temperature and Humidity Acquisition

1. Course Introduction

Read the DHT20 temperature and humidity sensor via I2C and update the results simultaneously to the LVGL label and the serial log.

2. Learning Objectives

After completing this lesson, you should be able to:

  • Identify the I2C interface and the DHT20 module wiring.
  • Understand the sensor driver, calibration check, and periodic tasks.
  • Display temperature and humidity on the screen and via serial once per second.

3. What You Need to Prepare

  • Prepare the CrowPanel Advanced 5-inch ESP32-P4 HMI AI Display development board and a data-capable USB-C cable as required for this lesson.
  • Prepare the DHT20 temperature and humidity module; the project includes bsp_i2c.c/.h and bsp_dht20.c/.h.
  • With the power off, connect SDA, SCL, 3.3V, and GND.

Code reference link: https://github.com/Elecrow-RD/-CrowPanel-Advanced-5inch-ESP32-P4-HMI-AI-Display-800x480-IPS-Touch-Screen/tree/master/example/V1.0

4. Software Operation Steps

Double-click to open the Lesson 10 code. (.ino file)

Open the temperature and humidity project

Configure the following options accordingly.

  • Board: ESP32P4 Dev Module
  • Core Debug Level: Info
  • Flash Frequency / Mode / Size: 80MHz / QIO / 16MB (128Mb)
  • Partition Scheme: 16M Flash (3MB APP/9.9MB FATFS)
  • PSRAM: Enabled
  • USB Mode: Hardware CDC and JTAG
  • Port: After connecting the USB data cable to the board's UART0, select the newly appearing COM port under "Tools → Port".

Screenshot of the standard upload configuration from Lesson 1

Following the library import steps explained in detail in Lesson 1, import the library files required by this project into the development environment to ensure the code can correctly locate the relevant dependencies during compilation, thereby guaranteeing the program runs normally.

P4_Arduino_01_Images_15

5. Hardware Operation Steps

With the power off, check the LCD ribbon cable, then connect a data-capable USB-C cable to the board's UART0 port and the computer.

Connect the board to the computer via UART0

With the power off, connect the DHT20 to the board's I2C interface, verifying VCC, GND, SDA, and SCL one by one; do not reverse the power supply connections.

Connect the DHT20 temperature and humidity sensor

In the Arduino IDE, select the correct board and COM port, then click Upload. After the upload completes, open the serial monitor at 115200 baud.

For details on how to open the serial monitor and set the baud rate, please refer to Lesson 1.

The screen should display temperature and humidity, with values refreshing approximately once per second; the serial monitor should output the same measurement results.

Observe the temperature and humidity values refreshing

You may gently cover the sensor with a finger to observe whether the temperature and humidity change gradually. Do not cover it for too long, and do not touch the sensor with sharp or wet objects.

6. Key Code Explanation

6.1 Sensor Driver Interface

dht20_begin();
dht20_is_calibrated();
dht20_read_data(&measurements);

bsp_dht20.c owns the DHT20 I2C transactions, status-bit handling, and CRC verification. The sketch calls dht20_begin() once, checks dht20_is_calibrated(), and obtains the latest values with dht20_read_data(&measurements).

6.2 Periodic Reading and Thread Safety

if (dht20_read_data(&measurements) != ESP_OK) {
            if (lvgl_port_lock(-1)) {
                lv_label_set_text(dht20_data, "dht20 read data error"); /*Read failure message displayed*/
                lvgl_port_unlock();
            }
            MAIN_ERROR("dht20 read data error");
        }

The periodic task checks the return value of dht20_read_data(). On failure it locks LVGL, writes the error text, unlocks, and logs the error. On success it formats temperature and humidity for the label while holding the LVGL lock, logs the measurements with MAIN_INFO(), then waits about one second before the next sample.

7. Experimental Results

The screen displays Temperature and Humidity, while the serial port prints the values synchronously, refreshing approximately once per second.

(For how to open the serial monitor, please refer to Lesson 1, where we explain it in detail)

Temperature and humidity display and serial output

8. Common Issues and Troubleshooting

  • If reading fails, check SDA/SCL, the power supply, and GND.
  • If the screen does not refresh, check the LVGL lock and label pointer; if the readings are fixed, check the sensor calibration.