Skip to main content

Working with Arduino

This chapter includes the following sections, please read as needed:

Before You Start

The Arduino examples for the ESP32-P4-WIFI6-DB are based on Arduino-ESP32 3.3.11. The onboard ESP32-P4 is the compile target for Arduino projects. The ESP32-C5 wireless co-processor connects to the ESP32-P4 via ESP-Hosted/SDIO and is not a compile target for Arduino projects.

Before running the display, camera, audio, or TF card examples, connect the corresponding onboard or external hardware first, and install the required libraries and configuration files as described in the example prerequisites.

Arduino Getting Started​

New to Arduino ESP32 development and looking for a quick start? We have prepared a comprehensive Getting Started Tutorial for you.

Note: This tutorial uses the ESP32-S3-Zero as a reference example, and all hardware code is based on its pinout. Before you start, we recommend checking the pinout of your development board to ensure the pin configuration is correct.

Setting Up the Arduino Development Environment​

1. Install Arduino IDE​

Please refer to the Install and Configure Arduino IDE Tutorial to install the Arduino IDE and add ESP32 board support.

2. Install Arduino-ESP32 and Board Definitions​

  1. Install Arduino-ESP32 3.3.11. The example code and board definitions are organized around this version. Do not mix the board files in this directory with other versions.
  2. Copy examples/Arduino/esp32/variants/waveshare_esp32_p4_wifi6_db from the example package to the variants directory in your Arduino-ESP32 installation.
  3. examples/Arduino/esp32/boards.txt is a complete boards.txt snapshot based on Arduino-ESP32 3.3.11. If your Arduino-ESP32 installation has no other local board modifications, you can back up the original file and replace it. If you have already added other boards, merge only the entries beginning with waveshare_esp32_p4_wifi6_db..
  4. Restart Arduino IDE and select Waveshare ESP32-P4-WIFI6-DB from the board list.

The Arduino examples are located in the examples/Arduino directory of the ESP32-P4-WIFI6-DB example repository.

3. Arduino IDE Tool Options​

After connecting the board, select the board and port from the "Tools" menu in Arduino IDE. The default parameters in the board definition are as follows:

OptionSetting
BoardWaveshare ESP32-P4-WIFI6-DB
Compile Targetesp32p4
Flash Size32MB
Partition Scheme32M Flash (13MB APP/6.75MB SPIFFS)
PSRAMThe board entry automatically defines BOARD_HAS_PSRAM
Upload ModeUART0
Serial Monitor Baud Rate115200

The development board uses the onboard CH343P USB-to-serial chip for program flashing and serial log output. Keep the upload mode at the default UART0. When changing the partition scheme or upload speed, confirm the corresponding example's memory requirements and actual connection. The serial output for all examples uses 115200.

Arduino Examples​

The example directory is examples/Arduino and currently includes the following examples:

Example DirectoryFunctionMain Dependencies or Hardware
board_checkOutputs chip, Flash, PSRAM, and heap memory informationNo external peripherals required
gpioInteractive GPIO testing via serial commandsArduino-ESP32 Console
i2cScans for 7‑bit addresses on the I2C1 busWire1, I2C1
i2sAudio loopback from a single microphone to a single speaker using ES8311ES8311, onboard mic and speaker
sdmmcTests TF file read/write in 4‑bit modeOnboard TF card
mipi_dsiDisplays MIPI-DSI color bars and tests backlightLCD panel, backlight controller
mipi_csiDisplays OV5647 camera feed on the MIPI-DSI LCDOV5647, LCD, ESP_Video

board_check: Board Information Check​

Open board_check/board_check.ino, compile and upload, then set the serial monitor to 115200. The example will output:

  • Chip model and chip revision;
  • CPU core count and frequency;
  • Flash size;
  • PSRAM size;
  • Remaining heap memory.

This example does not access the LCD, camera, audio, SDMMC, or ESP-Hosted, making it suitable for first confirming that the Arduino board definition is working correctly.

gpio: GPIO Console​

Open gpio/gpio.ino and upload it. Set the serial monitor line ending to Newline. Enter help to see the command list.

gpio read <pin>
gpio write <pin> <0|1>
gpio mode <pin> <in|out|in_pu|in_pd>

For example:

gpio mode 2 out
gpio write 2 0
gpio write 2 1
gpio mode 4 in_pu
gpio read 4

This board variant does not define an onboard LED_BUILTIN. When testing GPIO, avoid using pins that are already occupied by the MIPI display, touch controller, camera, SDMMC, ESP-Hosted/SDIO, or audio codec.

i2c: I2C Address Scan​

Open i2c/i2c.ino and upload it. The example uses Arduino Wire1 to scan the I2C1 bus. The default parameters are:

ItemParameter
SDAGPIO7
SCLGPIO8
Frequency400 kHz
Address Format7‑bit I2C address

This bus is shared by devices such as the ES8311, LCD backlight controller, GT911, and camera SCCB. Do not run other examples that initialize I2C1 at the same time as this scan. If no device responds, the program will not list it as a device; this does not indicate an issue with the scanning program.

i2s: ES8311 Audio Loopback​

Open i2s/i2s.ino and upload it. This runs a mono audio loopback using the onboard microphone and speaker. The example runs at 48 kHz, 16-bit, I2S standard mono mode using the left-channel slot.

SignalGPIO
MCLKGPIO13
BCLKGPIO12
LRCKGPIO10
DOUTGPIO9
DINGPIO11
Amplifier EnableGPIO53, high to enable

The ES8311 Arduino Wire1 control address is the 7‑bit address 0x18. If the serial output does not show ES8311 ready, or if there is no sound in the loopback, check the I2C address, MCLK, amplifier enable, and the microphone and speaker connections. Acoustic feedback (howling) may occur if the microphone and speaker are too close together.

sdmmc: TF File Read/Write​

Insert a TF card, then open sdmmc/sdmmc.ino and upload it. The example mounts the card using the 4-bit SDMMC interface, reads the capacity, and performs write and read tests on the /arduino_test.txt file in the root directory.

SignalGPIO
CLKGPIO43
CMDGPIO44
D0-D3GPIO39, GPIO40, GPIO41, GPIO42
Card Power ControlGPIO45, low to enable

If mounting fails, check that the TF card is inserted, the format is recognized, and the SDMMC pins are not occupied by other examples.

mipi_dsi: MIPI-DSI LCD​

mipi_dsi uses a project-local LCD driver to initialize the MIPI-DSI panel, cyclically displays horizontal and vertical color bars, and verifies the LCD backlight through the I2C backlight controller.

Before running, install or confirm the following libraries and versions:

LibraryVersion
ESP32_Display_Panel1.0.4
ESP32_IO_Expander1.1.1
esp-lib-utils0.2.3

In mipi_dsi/esp_panel_drivers_conf.h, select only one local LCD driver:

  • ESP_PANEL_DRIVERS_LCD_ENABLE_JD9365_LOCAL
  • ESP_PANEL_DRIVERS_LCD_ENABLE_HX8394_LOCAL
  • ESP_PANEL_DRIVERS_LCD_ENABLE_ILI9881C_LOCAL

The current default configuration is for the 10.1inch JD9365, with 800 × 1280 resolution, RGB565, and dual DSI lanes. When using a different LCD panel, you must select the controller and configuration that match the actual panel.

mipi_csi: Camera Display​

mipi_csi initializes the OV5647 MIPI-CSI camera and the MIPI-DSI LCD, and displays the camera's live feed on the LCD. The LCD interface supports JD9365, HX8394, and ILI9881C. mipi_csi/lcd_panel.h currently defaults to the 10.1inch JD9365 configuration.

Before running, ensure the following prerequisites are met:

  • Use Arduino-ESP32 3.3.11 and its corresponding ESP32-P4 precompiled libraries.
  • Confirm that ESP_Video has the MIPI-CSI video device enabled: CONFIG_ESP_VIDEO_ENABLE_MIPI_CSI_VIDEO_DEVICE=y.
  • In lcd_panel.h, set LCD_PANEL_TYPE to match the actual connected LCD panel type.
  • Connect the OV5647 camera and the actual LCD panel, and verify that camera power, MIPI cables, and the shared I2C/SCCB connections are correct.

mipi_csi, mipi_dsi, i2s, and i2c do not all initialize I2C1 in the same way: i2s and i2c use Arduino Wire1, mipi_dsi uses the legacy I2C API, and mipi_csi uses the newer i2c_master API. Do not run these examples simultaneously in the same Arduino project, or re-initialize the same bus repeatedly.

Board Resources and Shared Interfaces​

Pin Aliases​

The Arduino board variant provides the following common pin aliases:

InterfacePin
SDA / SDA1GPIO7
SCL / SCL1GPIO8
SSGPIO26
MOSIGPIO32
MISOGPIO33
SCKGPIO36
UART TX / RXGPIO37 / GPIO38

GPIO7 and GPIO8 are the onboard I2C1 shared bus. The LCD backlight, GT911, ES8311, and camera SCCB all use the relevant board-level bus resources. Before changing pins, check both the actual hardware connections and the initialization code in the corresponding examples.

I2C and Onboard Peripherals​

The board uses GPIO7 (SDA) and GPIO8 (SCL) to connect multiple I2C devices, with a default frequency of 400 kHz. Different examples use different I2C APIs:

  • i2c and i2s use Arduino Wire1.
  • mipi_dsi uses the legacy API from driver/i2c.h to control the LCD backlight.
  • mipi_csi uses Wire1 to create a new‑style i2c_master bus, and passes the bus handle to the camera SCCB and LCD backlight.

Do not mix the initialization methods from these examples within the same program. The build results and hardware state for complex examples must be verified individually on the target board; the current configuration cannot be applied directly after changes to the Arduino-ESP32 version, LCD panel model, or hardware connections.