Working with Arduino
This chapter includes the following sections, please read as needed:
- Arduino Getting Started
- Setting Up the Arduino Development Environment
- Arduino Examples
- Board Resources and Shared Interfaces
The Arduino examples for the ESP32-P4-NANO-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, please 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.
- Section 0: Getting to Know ESP32
- Section 1: Installing and Configuring Arduino IDE
- Section 2: Arduino Basics
- Section 3: Digital Output/Input
- Section 4: Analog Input
- Section 5: Pulse Width Modulation (PWM)
- Section 6: Serial Communication (UART)
- Section 7: I2C Communication
- Section 8: SPI Communication
- Section 9: Wi-Fi Basics
- Section 10: Web Server
- Section 11: Bluetooth
- Section 12: LVGL GUI Development
- Section 13: Comprehensive Project
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 Arduino IDE Setup Guide to install Arduino IDE and add ESP32 board support.
2. Install Arduino-ESP32 and Board Definitions
- 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. - Copy the
examples/Arduino/esp32/variants/waveshare_esp32_p4_nano_wifi6_dbdirectory from the example package to thevariantsdirectory in your Arduino-ESP32 installation. examples/Arduino/esp32/boards.txtis a snapshot ofboards.txtfrom Arduino-ESP323.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 forwaveshare_esp32_p4_nano_wifi6_db.- Restart Arduino IDE and select
Waveshare ESP32-P4-NANO-WIFI6-DBfrom the board list.
The examples are located in the examples/Arduino directory of the example package. The repository link for the ESP32-P4-NANO-WIFI6-DB examples is temporarily kept as ESP32-P4-NANO-WIFI6-DB. For specific examples, please refer to the current directory structure in the example package.
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:
| Option | Setting |
|---|---|
| Board | Waveshare ESP32-P4-NANO-WIFI6-DB |
| Flash Size | 16MB |
| Partition Scheme | 16M Flash (6.25MB APP/3.43MB SPIFFS) |
| PSRAM | Enabled by default in the board definition |
| USB Mode | USB-OTG (TinyUSB) |
| Upload Mode | UART0 / Hardware CDC |
| Serial Monitor Baud Rate | 115200 |
If using USB-OTG CDC for upload, switch the "Upload Mode" to USB-OTG CDC (TinyUSB) and select the device according to the port prompt in Arduino IDE. The serial output for all examples uses 115200.
Arduino Examples
The example directory is examples/Arduino and currently includes the following examples:
| Example Directory | Function | Main Dependencies or Hardware |
|---|---|---|
board_check | Outputs chip, Flash, PSRAM, and heap memory information | No external peripherals required |
gpio | Interactive GPIO testing via serial commands | Arduino-ESP32 Console |
i2c | Scans for 7-bit addresses on the I2C1 bus | Wire1, I2C1 |
i2s | Audio loopback from a single microphone to a single speaker using ES8311 | ES8311, onboard mic and speaker |
sdmmc | Tests TF file read/write in 4-bit mode | Onboard TF card |
mipi_dsi | Displays MIPI-DSI color bars and tests backlight | LCD panel, backlight controller |
mipi_csi | Displays OV5647 camera feed on the MIPI-DSI LCD | OV5647, 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:
| Item | Parameter |
|---|---|
| SDA | GPIO7 |
| SCL | GPIO8 |
| Frequency | 400 kHz |
| Address Format | 7-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.
| Signal | GPIO |
|---|---|
| MCLK | GPIO13 |
| BCLK | GPIO12 |
| LRCK | GPIO10 |
| DOUT | GPIO9 |
| DIN | GPIO11 |
| Amplifier Enable | GPIO53, 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.
| Signal | GPIO |
|---|---|
| CLK | GPIO43 |
| CMD | GPIO44 |
| D0-D3 | GPIO39, GPIO40, GPIO41, GPIO42 |
| Card Power Control | GPIO45, 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:
| Library | Version |
|---|---|
ESP32_Display_Panel | 1.0.4 |
ESP32_IO_Expander | 1.1.1 |
esp-lib-utils | 0.2.3 |
In mipi_dsi/esp_panel_drivers_conf.h, select only one local LCD driver:
ESP_PANEL_DRIVERS_LCD_ENABLE_JD9365_LOCALESP_PANEL_DRIVERS_LCD_ENABLE_HX8394_LOCALESP_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.11and its corresponding ESP32-P4 precompiled libraries. - Confirm that
ESP_Videohas the MIPI-CSI video device enabled:CONFIG_ESP_VIDEO_ENABLE_MIPI_CSI_VIDEO_DEVICE=y. - In
lcd_panel.h, setLCD_PANEL_TYPEto 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 initialize I2C1 in different ways. 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:
| Interface | Pin |
|---|---|
SDA / SDA1 | GPIO7 |
SCL / SCL1 | GPIO8 |
SS | GPIO26 |
MOSI | GPIO32 |
MISO | GPIO33 |
SCK | GPIO36 |
UART TX / RX | GPIO37 / 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:
i2candi2suse ArduinoWire1.mipi_dsiuses the legacy API fromdriver/i2c.hto control the LCD backlight.mipi_csiusesWire1to create a new-stylei2c_masterbus, 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.