Skip to content

Lesson 10: DHT20 Temperature and Humidity Acquisition

Applicable development board: CrowPanel Advanced 7 / 9 / 10.1inch ESP32-P4 HMI AI Display development board

Model compatibility: The 7-inch, 9-inch, and 10.1-inch models are fully interchangeable in terms of hardware interfaces and software code; only their physical dimensions differ. Please select the model based on your actual display size and use case. No code modifications are required for this lesson.

1. Course Introduction

This lesson reads the DHT20 temperature and humidity sensor over I2C and updates the results to both 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 task.
  • Display temperature and humidity on the screen and over the serial port once per second.

3. What You Need

  • Prepare the CrowPanel Advanced 7 / 9 / 10.1inch ESP32-P4 HMI AI Display development board (as many as required for this lesson) and a data-capable USB-C cable.
  • Prepare a 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-7inch-ESP32-P4-HMI-AI-Display-1024x600-IPS-Touch-Screen/tree/master/example

4. Software Operation Steps

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

Open the temperature and humidity project

Configure the options as described below.

  • 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 appeared COM port under "Tools → Port".

Standard upload configuration screenshot from Lesson 1

Follow the library import steps explained in detail in Lesson 1 to import the library files required by this project into the development environment, ensuring that the dependencies can be correctly located during compilation so that the program runs properly.

P4_Arduino_01_Images_15

5. Hardware Operation Steps

Now that the code is ready, we need to flash the ESP32-P4 to see the results in action.

First, connect the Advance-P4 device to your computer host via a USB cable.

USB connect development board

With the power off, connect the DHT20 to the I2C interface and confirm that SDA, SCL, 3.3V, and GND are matched correctly.

Development board I2C interface

Connect the DHT20 sensor

Before uploading the code, first select the upload configuration according to "4. Software Operation Steps."

Connect UART0 and compile and upload using the general configuration; keep the development board powered on.

Compile and upload the LED program

After the code upload is complete, the screen will display the current temperature and humidity information obtained from the DHT20 module.

image-20260727172127943

6. Key Code Explanation

6.1 Sensor Driver Interface

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

bsp_dht20.c handles the I2C commands, status bits, and CRC verification. The main program uses these three interfaces to perform initialization, calibration check, and data reading.

6.2 Periodic Reading and Thread Safety

if (dht20_read_data(&measurements) == ESP_OK) {
  lvgl_port_lock(-1);
  update_dht20_value(measurements.temperature,
                     measurements.humidity);
  lvgl_port_unlock();
  vTaskDelay(1000 / portTICK_PERIOD_MS);
}

After a successful reading, the LVGL lock is acquired first before updating the label, and the temperature and humidity are printed using MAIN_INFO(); finally, the sensor is sampled again at an interval of approximately 1 second.

7. Observed Behavior

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

(For how to open the serial monitor, please refer to Lesson 1, where we provide a detailed explanation.)

Temperature and humidity display and serial output

8. Frequently Asked Questions and Troubleshooting

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