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-ePaper-1.54 development board, the Arduino IDE requires the installation of arduino-esp32 v3.3.0 or higher.

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-ePaper-1.54 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.3.11/v9.5.0"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 from the RTC chip-
03_I2C_STHC3Get data from SHTC3 temperature & humidity sensor-
04_SD_CardLoad and display TF card information-
05_WIFI_APSet to AP mode to obtain the IP address of the access device-
06_WIFI_STASet to STA mode to connect to Wi-Fi and obtain an IP address-
07_BATT_PWR_TestWhen powered by a lithium battery alone, control power via the PWR buttonLVGL V9.5.0
08_Audio_TestPlay the sound recorded by the microphone through the speakerLVGL V9.5.0
09_LVGL_V8_TestLVGLV8 demoLVGL V8.3.11
10_LVGL_V9_TestLVGLV9 demoLVGL V9.5.0

01_ADC_Test

Demo Description

  • The analog voltage connected through the GPIO is converted to digital by the ADC, and then the actual lithium battery voltage is calculated and printed to the terminal.

Hardware Connection

  • Connect the board to the computer using a USB cable

Code Analysis

BoardAdc_Init(); // Initialize ADC
xTaskCreatePinnedToCore(Adc_LoopTask, "Adc_LoopTask", 4 * 1024, NULL, 4, NULL, 0); // Create ADC test task

Operation Result

  • After the program is compiled and uploaded, open the serial monitor to see the printed voltage and battery capacity, as shown below:

02_I2C_PCF85063

Demo Description

  • Through the I2C protocol, initialize the PCF85063 chip, set the time, and then periodically read the time and print it to the terminal

Hardware Connection

  • Connect the board to the computer using a USB cable

Code Analysis

pcf85063a_init(&pcf85063, i2c_bus->Get_I2cBusHandle(), PCF85063A_ADDRESS); // Initialize RTC chip
pcf85063a_set_time_date(&pcf85063, datatime); // Set time
pcf85063a_get_time_date(&pcf85063, &current_time); // Get 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:

03_I2C_STHC3

Demo Description

  • Uses the I2C protocol to initialize and configure the SHTC3 chip, then reads and prints temperature and humidity information to the terminal every second.

Hardware Connection

  • Connect the board to the computer using a USB cable

Code Analysis

Shtc3_Init(i2c_bus); // Initialize temperature and humidity sensor
Shtc3_ReadTempHumi(&t,&h); // Get temperature and humidity from the sensor

Operation Result

  • Open the serial port monitor, you can see the printed temperature and humidity data, as shown in the figure below:

04_SD_Card

Demo Description

  • Drive the TF card via SDSPI mode. After successful mounting, print the TF card information to the terminal.

Hardware Connection

  • Install a FatFs-formatted into the board before powering on

Code Analysis

sdcardinitflag = Sdcard_Init(); // Initialize TF card
Sdcard_WriteFile("/sdcard/sdcard.txt",sdcard_test1); // Write data to TF card
Sdcard_ReadFile("/sdcard/sdcard.txt",sdcard_test2,NULL); // Read data from TF card

Operation Result

  • Open the serial monitor to see the TF card information and read/write test output, as shown:

05_WIFI_AP

Demo Description

  • This demo can set the development board as a hotspot, allowing phones or other devices in STA mode to connect to the development board.

Hardware Connection

  • Connect the board to the computer using a USB cable

Code Analysis

  • In the 05_WIFI_AP.ino file, locate ssid and password. Phones or other STA mode devices can then connect to the board using this SSID and password.

    const char *ssid = "ESP32_AP";
    const char *password = "12345678";

Operation Result

  • After flashing the program, open the Serial Terminal. If a device successfully connects to the hotspot, the MAC address of that device will be output, as shown:

06_WIFI_STA

Demo Description

  • This example configures the development board as a STA device to connect to a router, thereby accessing the system network.

Hardware Connection

  • Connect the board to the computer using a USB cable

Code Analysis

  • In the 06_WIFI_STA.ino file, locate ssid and password, and modify them to match the SSID and Password of an available router in your current environment.

    const char *ssid = "you_ssid";
    const char *password = "you_password";

Operation Result

  • After flashing the program, open the Serial Terminal. If the device successfully connects to the hotspot, the obtained IP address will be output, as shown in the figure:

07_BATT_PWR_Test

Demo Description

  • Demonstrates how to control the system power via the PWR button when powered by the lithium battery.

Hardware Connection

  • Connect the board to the computer using a USB cable

Code Analysis

i2c_bus = I2cMasterBus::requestInstance(ESP32_I2C_SCL_PIN, ESP32_I2C_SDA_PIN, ESP32_I2C_DEV_NUM); // I2C initialization
assert(i2c_bus);
ESP_ERROR_CHECK(esp_io_expander_new_i2c_tca9554(i2c_bus->Get_I2cBusHandle(), ESP_IO_EXPANDER_I2C_TCA9554_ADDRESS_000, &io_expander)); // I/O expander initialization
ESP_ERROR_CHECK(esp_io_expander_set_dir(io_expander, IO_EXPANDER_PIN_NUM_0 | IO_EXPANDER_PIN_NUM_1 | IO_EXPANDER_PIN_NUM_5, IO_EXPANDER_OUTPUT));
ESP_ERROR_CHECK(esp_io_expander_set_level(io_expander, IO_EXPANDER_PIN_NUM_0 | IO_EXPANDER_PIN_NUM_1 | IO_EXPANDER_PIN_NUM_5, 1));
InitializeButtons(); // Button event initialization
PortLvgl_Start_Init();// LVGL initialization
xTaskCreatePinnedToCore(Button_LoopTask, "Button_LoopTask", 4 * 1024, NULL, 4, NULL, 0); // Implement the lithium battery power switch via software control

Operation Result

  • After the program is flashed, disconnect the USB power supply and connect the lithium battery. Power on by pressing and holding the PWR button, as shown in the figure:

    tip
    1. Press and hold the PWR button, then release to power on. After startup, the screen will display "ON".
    2. Press and hold the PWR button again. Wait for the screen to display "OFF", indicating successful power shutdown, then release the button.

08_Audio_Test

Demo Description

  • Demonstrates how to get data from the microphone and then play it through the speaker

Hardware Connection

  • Connect the board to the computer using a USB cable

Code Analysis

PortLvgl_Start_Init(); // Initialize the screen
InitializeButtons(); // Initialize button events
Lvgl_PortInit(Lvgl_flush_cb); // Initialize LVGL interface
Codec_StartInit(); // Initialize audio codec IC
ESP_ERROR_CHECK(esp_io_expander_set_dir(io_expander, IO_EXPANDER_PIN_NUM_0 | IO_EXPANDER_PIN_NUM_1 | IO_EXPANDER_PIN_NUM_3, IO_EXPANDER_OUTPUT));
ESP_ERROR_CHECK(esp_io_expander_set_level(io_expander, IO_EXPANDER_PIN_NUM_0 | IO_EXPANDER_PIN_NUM_1 | IO_EXPANDER_PIN_NUM_3, 1)); // EXIO3 enables the amplifier

Operation Result

  • After the program is flashed, as shown in the figure:

    tip
    1. Press the BOOT button once to start recording. Speak into the MIC, and the recording will automatically play back after 2 seconds.
    2. Double-click the PWR button to play a piece of music
    3. Click the PWR button to interrupt music playback

09_LVGL_V8_Test

Demo Description

  • Helps users quickly implement UI design by porting LVGL V8.

Hardware Connection

  • Connect the board to the computer using a USB cable

Code Analysis

PortLvgl_Start_Init(); // Initialize the screen
Lvgl_PortInit(Lvgl_flush_cb); // Initialize LVGL V8
xTaskCreatePinnedToCore(Lvgl_LoopTask, "Lvgl_LoopTask", 4 * 1024, NULL, 4, NULL,0); // LVGL test task

Operation Result

  • After the program is flashed, the device operation result is as follows:

10_LVGL_V9_Test

Demo Description

  • Helps users quickly implement UI design by porting LVGL V9.

Hardware Connection

  • Connect the board to the computer using a USB cable

Code Analysis

PortLvgl_Start_Init(); // Initialize the screen
Lvgl_PortInit(Lvgl_flush_cb); // Initialize LVGL V9
xTaskCreatePinnedToCore(Lvgl_LoopTask, "Lvgl_LoopTask", 4 * 1024, NULL, 4, NULL,0); // LVGL test task

Operation Result

  • After the program is flashed, the device operation result is as follows: