Skip to main content

Working with Arduino

This chapter contains the following sections. Please read as needed:

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 Development Environment

1. Installing and Configuring Arduino IDE

info

For the ESP32-C6-Touch-AMOLED-2.16 development board, Arduino IDE requires arduino-esp32 version v3.3.0 or later.

Please refer to the tutorial Installing and Configuring Arduino IDE to download and install the Arduino IDE and add ESP32 support.

2. Installing Libraries

  • When installing Arduino libraries, there are typically two methods: Install Online and Install Offline. If the library installation requires Install Offline, you must use the provided library file.
  • For most libraries, users can easily search for and install them via the Arduino IDE's online Library Manager. However, some open-source or custom libraries are not synchronized to the Arduino Library Manager and therefore cannot be found through online search. In this case, users can only install these libraries manually via offline methods.
  • The sample program package for the ESP32-C6-Touch-AMOLED-2.16 development board can be downloaded from here. The Arduino\libraries directory within the package already contains all the library files required for this tutorial.
Library/File NameDescriptionVersionInstallation Method
LVGLGraphical libraryv8.* / v9.*"Install Offline"
SensorLibSensor control libraryv0.3.1"Install Online" or "Install Offline"
Version Compatibility Notes

There are strong dependencies between versions of LVGL and its driver libraries. For example, a driver written for LVGL v8 may not be compatible with LVGL v9. To ensure that the examples can be reproduced reliably, it is recommended to use the specific versions listed in the table above. Mixing different versions of libraries may lead to compilation failures or runtime errors.

3. Arduino Project Parameter Settings

Demo

The Arduino demos are located in the Arduino/examples directory of the demo package.

DemoBasic Program DescriptionDependency Library
01_ADC_TestGet the voltage value of the lithium battery-
02_I2C_PCF85063Print real-time time of RTC chipSensorLib
03_I2C_QMI8658Print the raw data from IMUSensorLib
04_SD_CardLoad and display the information of the TF card-
05_WIFI_APSet to AP mode to obtain the IP address of the access device-
06_WIFI_STASet to STA mode to connect to WiFi and obtain an IP address-
07_BATT_PWR_TestControl power via the PWR button when powered solely by the lithium battery-
08_Audio_TestPlay the sound recorded by the microphone through the speaker-
09_LVGL_V8_TestLVGL V8 demoLVGL V8.*
10_LVGL_V9_TestLVGL V9 demoLVGL V9.*

01_AXP2101_Test

Demo Description

  • Obtain power information by driving the AXP2101 and print it to the terminal.

Hardware Connection

  • Connect the board to the computer using a USB cable

Code Analysis

axp2101.setDC1Voltage(3300);      //Set DCDC1 voltage to 3.3V
axp2101.setALDO1Voltage(3300); //Set DCDC1 voltage to 3.3V
axp2101.setALDO2Voltage(3300); //Set DCDC1 voltage to 3.3V
axp2101.setALDO3Voltage(3300); //Set DCDC1 voltage to 3.3V
axp2101.setALDO4Voltage(3300); //Set DCDC1 voltage to 3.3V
axp2101.setPrechargeCurr(XPOWERS_AXP2101_PRECHARGE_50MA); //Set precharge current to 50mA
axp2101.setChargerConstantCurr(XPOWERS_AXP2101_CHG_CUR_500MA); //Set constant charging current to 500mA
axp2101.setChargerTerminationCurr(XPOWERS_AXP2101_CHG_ITERM_50MA); //Set termination charging current to 50mA
axp2101.getBattVoltage(); //Get lithium battery voltage

Operation Result

  • After compiling and downloading the program, open the Serial Monitor to see the printed power information, as shown in the figure below:

02_I2C_QMI8658

Demo Description

  • Obtain attitude information by driving the QMI8658 and print it to the terminal.

Hardware Connection

  • Connect the board to the computer using a USB cable

Code Analysis

qmi8658_set_accel_range(&qmi8658, QMI8658_ACCEL_RANGE_8G); // Set accelerometer range to ±8G (G = gravitational acceleration)
qmi8658_set_accel_odr(&qmi8658, QMI8658_ACCEL_ODR_1000HZ); // Set accelerometer output data rate to 1000Hz
qmi8658_set_gyro_range(&qmi8658, QMI8658_GYRO_RANGE_512DPS); // Set gyroscope range to ±512°/s
qmi8658_set_gyro_odr(&qmi8658, QMI8658_GYRO_ODR_1000HZ); // Set gyroscope output data rate to 1000Hz
qmi8658_set_accel_unit_mps2(&qmi8658, true); // Set accelerometer output unit to m/s²
qmi8658_set_gyro_unit_rads(&qmi8658, true); // Set gyroscope output unit to rad/s
qmi8658_set_display_precision(&qmi8658, 4); // Set output/print precision to 4 decimal places

Operation Result

  • After compiling and downloading the program, open the Serial Monitor to see the printed attitude information, as shown in the figure below:

03_I2C_QMI8658

Demo Description

  • Set and obtain time by driving the PCF85063, and print it to the terminal.

Hardware Connection

  • Connect the board to the computer using a USB cable

Code Analysis

pcf85063a_datetime_t datatime = {
.year = 2026,
.month = 1,
.day = 1,
.hour = 8,
.min = 0,
.sec = 0
};
pcf85063a_set_time_date(&pcf85063, datatime); /*Set start time 2026/1/1 08:00:00*/
pcf85063a_get_time_date(&pcf85063, &datatime); /*Get current time*/

Operation Result

  • After the program is compiled and downloaded, open the serial port monitoring to see the RTC time of the printout, as shown in the following figure:

04_SD_Card

Demo Description

  • Drive the TF card using SDSPI and print the TF card information to the terminal.

Hardware Connection

  • Connect the board to the computer using a USB cable

Code Analysis

esp_vfs_fat_sdspi_mount(SdName_, &host, &slot_config, &mount_config, &sdCardHead);  //Mount sdcard to fatfs filesystem
int SDPort_WriteFile(const char *path, const void *data, size_t data_len); //Write data to file
int SDPort_ReadFile(const char *path, uint8_t *buffer, size_t *outLen); //Read data from file
int SDPort_ReadOffset(const char *path, void *buffer, size_t len, size_t offset); //Read data from file with offset
int SDPort_WriteOffset(const char *path, const void *data, size_t len, bool append); //Write data to file with offset

Operation Result

  • After compiling and downloading the program, open the Serial Monitor to see the printed TF card information, as shown in the figure below:

05_WIFI_STA

Demo Description

  • Set the development board to Wi-Fi STA mode, connect to an AP router, and print the obtained IP address to the terminal.

Hardware Connection

  • Connect the board to the computer using a USB cable

Code Analysis

nvs_flash_init();                                    // Initialize NVS (non-volatile storage) for saving Wi-Fi configuration data
esp_netif_init(); // Initialize TCP/IP stack, required for network communication
esp_event_loop_create_default(); // Create default event loop for system event distribution (e.g., Wi-Fi events)
esp_netif_create_default_wifi_sta(); // Create default Wi-Fi STA (Station) interface for connecting to a router
esp_netif_create_default_wifi_ap(); // Create default Wi-Fi AP (Access Point) interface for providing a hotspot
strcpy((char*)sta_config.sta.ssid, "ESP32_STA"); // Set the router's SSID
strcpy((char*)sta_config.sta.password, "12345678"); // Set the router's password
esp_wifi_set_mode(WIFI_MODE_STA); // Set ESP32 Wi-Fi mode

Operation Result

  • After compiling and downloading the program, open the Serial Monitor to see the printed IP address, as shown in the figure below:

06_WIFI_AP

Demo Description

  • Set the development board to Wi-Fi AP mode. Other STA devices can connect to this AP, and upon successful connection, the device's MAC address will be printed to the terminal.

Hardware Connection

  • Connect the board to the computer using a USB cable

Code Analysis

nvs_flash_init();                                      // Initialize NVS (non-volatile storage) for saving Wi-Fi configuration data
esp_netif_init(); // Initialize TCP/IP stack, required for network communication
esp_event_loop_create_default(); // Create default event loop for system event distribution (e.g., Wi-Fi events)
esp_netif_create_default_wifi_sta(); // Create default Wi-Fi STA (Station) interface for connecting to a router
esp_netif_create_default_wifi_ap(); // Create default Wi-Fi AP (Access Point) interface for providing a hotspot
strcpy((char*)ap_config.ap.ssid, "ESP32_AP"); // Set the AP's SSID to "ESP32_AP"
strcpy((char*)ap_config.ap.password, "12345678"); // et the AP's password to "12345678" (at least 8 characters)
ap_config.ap.max_connection = 4; // Set a maximum of 4 devices to connect simultaneously
ap_config.ap.authmode = WIFI_AUTH_WPA_WPA2_PSK; // Set authentication mode to WPA/WPA2 PSK (password authentication)
esp_wifi_set_mode(WIFI_MODE_AP); // Set Wi-Fi mode to AP mode (hotspot only)
esp_wifi_set_config(WIFI_IF_AP, &ap_config); // Apply the AP configuration to enable the Wi-Fi hotspot

Operation Result

  • After compiling and downloading the program, open the Serial Monitor to see the printed MAC address, as shown in the figure below:

07_Audio_Test

Demo Description

  • Implement recording and playback by driving the ES8311 and ES7210 Codec chips.

Hardware Connection

  • Connect the board to the computer using a USB cable

Code Analysis

esp_err_t play_pcm_from_spiffs(uint8_t *play_buf, const char *file_path,bool *Music_flag); // Read audio data from the spiffs filesystem and play it
void fac_play_read_storage(uint8_t *play_buf); // Read binary recording data from the filesystem and play it
void fac_rec_write_storage(uint8_t *rec_buf,size_t max_size); // Save recorded data to spiffs

Operation Result

  • After compiling and downloading the program, the display on the development board will appear as shown below:

    tip
    1. Click Recording to record; recording lasts 3 seconds
    2. Click Play Recording to play the recording
    3. Click Play Music to play music
    4. Click Stop Music to pause music

08_LVGL_V8_Test

Demo Description

  • By porting LVGL V8, this helps users quickly get started with UI design.

Hardware Connection

  • Connect the board to the computer using a USB cable

Code Analysis

#define Brightness_Test_EN  1 // Set to 0 to disable backlight test, default is 1 
Custom_PmicPortInit(&user_i2cbus,0x34); // Initialize power IC
user_display = new DisplayPort(user_i2cbus,480,480); // Initialize the screen
user_display->DisplayPort_TouchInit(); // Initialize touch
Lvgl_PortInit(*user_display); // Initialize LVGL interface

Operation Result

  • After compiling and downloading the program, the display on the development board will appear as shown below:

09_LVGL_V9_Test

Demo Description

  • By porting LVGL V9, this helps users quickly get started with UI design.

Hardware Connection

  • Connect the board to the computer using a USB cable

Code Analysis

#define Brightness_Test_EN  1 // Set to 0 to disable backlight test, default is 1 
Custom_PmicPortInit(&user_i2cbus,0x34); // Initialize power IC
user_display = new DisplayPort(user_i2cbus,480,480); // Initialize the screen
user_display->DisplayPort_TouchInit(); // Initialize touch
Lvgl_PortInit(*user_display); // Initialize LVGL interface

Operation Result

  • After compiling and downloading the program, the display on the development board will appear as shown below: