Lesson02_GPIO_LED: Lighting Up the Onboard LED on ESP32-P4¶
1. Course Overview¶
In this lesson, we use ESP-IDF to light up the onboard LED on the ESP32-P4 development board by driving it through GPIO. After the program is flashed, the board resets on power-up and the LED blinks continuously at a rhythm of one second on and one second off. Learners only need to connect the USB data cable, set the target chip, compile, and flash to observe the periodic blinking of the LED.
This lesson is an introductory course on peripheral drivers. Building on the environment setup foundation from Lesson01, it introduces GPIO output configuration and FreeRTOS task creation for the first time. Through this experiment, learners will complete an introductory verification of GPIO initialization, level control, and task scheduling, laying the groundwork for driving other peripherals such as screen backlights and sensors in later lessons.
2. Learning Objectives¶
- Be able to open the Lesson02 project in ESP-IDF and set the target chip to
esp32p4. - Be able to explain the meaning of each parameter when
gpio_configconfigures GPIO48 as an output. - Be able to explain the role of the stack size and priority parameters when
xTaskCreatecreates a task. - Be able to complete compilation and flashing, and observe whether the LED blinks at a 1-second cycle.
- Be able to determine whether the GPIO configuration and task are running correctly based on whether the LED blinks.
3. What You Need to Prepare¶
- Hardware: One CrowPanel Advanced 7 / 9 / 10.1-inch ESP32-P4 HMI AI Display development board (the onboard LED is connected to GPIO48) and one USB Type-C data cable that supports data transfer.
- Compatibility note: The 7 / 9 / 10.1-inch development boards share the same hardware and software code, differing only in board size; please choose the appropriate model based on the display size and your use case.
- Software: VS Code, ESP-IDF Extension (ESP-IDF v5.4 or later). This lesson uses ESP-IDF v5.4.2.
- Project dependencies: Keep the
main/main.cfile and theperipheral/bsp_extracomponent under theLesson02-Turn_on_the LEDdirectory; no additional components need to be installed. - Configuration: Target chip
esp32p4.
Code download link:
4. Software Operation Steps¶
-
Open the ESP-IDF Extension panel in VS Code, click Open ESP-IDF Project, and select the
Lesson02-Turn_on_the LEDfolder. After opening, the Explorer should display project files such asmain,peripheral, andCMakeLists.txt.
-
First, select the code runtime environment ESP-IDF v5.4.2, set the flash method to UART, and then select the serial port that corresponds to the actual development board. Next, click Set Espressif Device Target in the ESP-IDF Extension panel and select
esp32p4.
After the settings are complete, the status bar should display ESP-IDF v5.4.2, UART, the required COM port, and ESP32-P4.
- Click SDK Configuration Editor in the VS Code bottom status bar or the ESP-IDF extension panel, and wait for the configuration page to fully load before modifying parameters. If the page is still loading, do not execute Build immediately.
- Enter
flashin the search box and ensure Flash SPI mode:QIO; Flash Sampling Mode:STR Mode; Flash SPI speed:80 MHz; Flash size:16 MB. These parameters should match the onboard Flash of the Advance-P4.
-
After verifying the configuration is correct, click Save in the top-right corner; confirm the changes have been saved, then run Build to compile.
-
Click Full Clean to clear the cache left over from the previous build. Running this after the first build, after switching project configuration, or after modifying SDK parameters prevents old configurations from affecting the new build result.
- Before compiling, re-check the GPIO48 initialization and blinking task code, then click Build. On success, the end of the output window shows
Project build completewith no errors.
-
Confirm the development board is connected to the computer via USB, then click Select Port to Use to choose the corresponding serial port.
-
Click Flash to flash the firmware. After flashing completes, the message
Hard resetting...is displayed.
-
After flashing, you can click Monitor to view the serial log and confirm whether the task was created correctly. Press
Ctrl + ]to exit the monitor. -
Finally, you can use the one-click operation button in the ESP-IDF status bar to run compilation, flashing, and opening the serial monitor in sequence. Use this only after the project configuration, serial port, and code have all been confirmed correct; if you need to locate a problem, still follow the steps above one by one.
5. Hardware Operation Steps¶
- Use a USB Type-C data cable that supports data transfer to connect the ESP32-P4 development board to the computer. After connecting, the board's power indicator should light up.
- After flashing completes and the board resets, observe whether the LED corresponding to GPIO48 lights up.
- Continuously observe whether the LED blinks periodically at a rhythm of one second on and one second off; it should not stay constantly on, constantly off, or blink irregularly.
6. Key Code Explanation¶
const gpio_config_t gpio_cofig = {
.pin_bit_mask = (1ULL << 48),
.mode = GPIO_MODE_OUTPUT,
.pull_up_en = false,
.pull_down_en = false,
.intr_type = GPIO_INTR_DISABLE,
};
err = gpio_config(&gpio_cofig);
This is the initialization configuration for GPIO48. pin_bit_mask uses a bitmask to select GPIO48; 1ULL << 48 must use a 64-bit left shift, otherwise it will overflow. mode is set to output mode; the pull-up and pull-down resistors are disabled because the LED is driven directly by the pin; the interrupt is also disabled. If pin_bit_mask is written incorrectly or omitted, gpio_config will not configure that pin and the LED will never light up. If mode is changed to GPIO_MODE_INPUT, the pin cannot output a level and the LED likewise will not light up.
gpio_extra_set_level(1);
vTaskDelay(1000 / portTICK_PERIOD_MS);
gpio_extra_set_level(0);
vTaskDelay(1000 / portTICK_PERIOD_MS);
This is the core blinking logic: first set the level high to light up the LED, then delay for 1 second; then set the level low to turn off the LED, and delay for another 1 second. Both vTaskDelay calls are 1000 ms, so together one full cycle is 2 seconds. If one of the delays is removed, the other state will be skipped almost instantly, and the LED may appear to stay constantly on or off; if 1000 is changed to 200, the blinking will noticeably speed up.
The blinking task is created in app_main. A stack size of 2048 bytes should be sufficient to hold the local variables the task needs while running; priority 5 is a medium priority. If the stack size is set too small (e.g., 512 bytes), the task may crash due to stack overflow, the LED will stop blinking, and the system may even trigger a reboot.
7. Experimental Observations¶
After the program is flashed and the board resets, the serial monitor displays the ESP-IDF startup information. Then led_blink_task is created and begins running, and the GPIO48 output level toggles once per second.
The LED should not stay constantly on, constantly off, or blink irregularly. If the LED stays on, it may be because a vTaskDelay was deleted or gpio_extra_set_level(0) was not executed; if the LED never lights up, you should first check whether pin_bit_mask and mode in gpio_config are correct, and whether the LED is actually connected to GPIO48.











