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

1. Installing and Configuring the Arduino IDE

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: online installation and offline installation. If the library installation requires offline installation, you must use the provided library files.
  • 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.
  • You can click this link to download the example package for the ESP32-S3-Touch-LCD-1.85B board from the Arduino directory. The Arduino\libraries directory within this package contains all the necessary library files required for this tutorial.
Library/File NameDescriptionVersionInstallation Method
ESP32-audioI2S-masterAudio playback libraryv3.4.5Install via Library Manager or manually
SensorLibPCF85063, QMI8658 sensor driver libraryv0.3.1Install via library manager or manually
es7210ES7210 driver-Manual installation
es8311ES8311 driver-Manual installation
kode_bq27220bq27220 battery gauge driver——Manual installation
lvglLVGL UIv8.4.0Manual installation
Version Compatibility Note

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.

Installation Steps:

  1. Navigate to the downloaded example package.

  2. Copy all folders (Arduino_DriveBus, GFX_Library_for_Arduino, etc.) in the Arduino\libraries directory to the Arduino library folder.

    info

    The path to the Arduino libraries folder is typically: c:\Users\<username>\Documents\Arduino\libraries.

    You can also locate it in the Arduino IDE by going to File > Preferences and checking the "Sketchbook location". The libraries folder is the libraries subfolder within this path.

  3. For other installation methods, please refer to: Arduino Library Management Tutorial.

Board Installation Instructions for ESP32-S3-Touch-LCD-1.85B

Board NameInstallation RequirementVersion Requirement
ESP32 by Espressif Systems"Offline installation" / "Online installation"≥3.2.0

Example

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

ExampleBasic Program DescriptionDependency Library
01_lvgl_demoDemonstrates basic LVGL graphics library functions, tests basic display performancelvgl
02_lvgl_BQ27220Tests the BQ27220 battery gauge sensorkode_bq27220
03_audio_out_no_tfAudio playback testes8311
04_SDMMC_TestTF card mounting and file read/write test——
05_audio_out_tfLVGL demonstrationLVGL, Arduino_DriveBus
06_esp_srES7210 driver example, voice detection——
07_I2C_qmi8658Gyroscope driver testSensorLib
08_I2C_pcf85063RTC real‑time clock driver testSensorLib

01_lvgl_demo

Example Description

  • This example demonstrates how to drive the display using Arduino and run multiple LVGL example programs.

Hardware Connection

  • Connect the development board to the computer.

Code Analysis

  • Initialize the screen, touch, and LVGL:

    I2C_Init();
    Backlight_Init();
    LCD_Init();
    Lvgl_Init();
  • Select the LVGL example to run:

    lv_demo_widgets();
    // lv_demo_benchmark();
    // lv_demo_keypad_encoder();
    // lv_demo_music();
    // lv_demo_stress();

Operation Result

02_lvgl_BQ27220

Example Description

  • This example demonstrates using the BQ27220 battery gauge sensor to read battery information and display it in LVGL.

Hardware Connection

  • Connect a 3.7V lithium battery.

Code Analysis

  • Create an I2C mutex to prevent conflicts between touch and BQ27220 readings in different tasks.

    wire_mutex = xSemaphoreCreateMutex();
  • Initialize BQ27220:

    if (!gauge.begin(Wire, 0x55, I2C_SDA_PIN, I2C_SCL_PIN, 400000)) {
    Serial.println("BQ27220 not found");
    while (1) delay(1000);
    }

Operation Result

03_audio_out_no_tf

Example Description

  • This example demonstrates playing music using the ES8311 audio DAC

Hardware Connection

  • Connect a speaker
  • Connect the development board to the computer.

Code Analysis

  • Initialize the I2C controller and external I/O expander:

    DEV_I2C_Init();
    IO_EXTENSION_Init();
    IO_EXTENSION_Output(IO_EXTENSION_IO_6, 1);
  • Initialize ES8311:

    es8311_codec_init();
  • Initialize I2S and enable the power amplifier (PA) pin:

    setupI2S();
    IO_EXTENSION_Output(IO_EXTENSION_IO_4, 1);
  • In the loop, continuously write data to the ES8311 via I2S:

    void loop() {
    i2s.write((uint8_t *)audio_data, AUDIO_SAMPLES * 2);
    }

Operation Result

  • The screen has no display.
  • After flashing the program, music will start playing automatically

04_SDMMC_Test

Example Description

  • This example demonstrates how to mount a TF card and test file reading and writing

Hardware Connection

  • Insert a TF card
  • Connect the development board to the computer.

Code Analysis

  • Set the SDIO interface pins and mount the file system:

    if(!SD_MMC.setPins(clk, cmd, d0)){
    Serial.println("Pin change failed!");
    return;
    }

    if (!SD_MMC.begin( "/sdcard", true)) {
    Serial.println("Card Mount Failed");
    return;
    }
  • File read/write test:

    listDir(SD_MMC, "/", 0);
    createDir(SD_MMC, "/mydir");
    listDir(SD_MMC, "/", 0);
    removeDir(SD_MMC, "/mydir");
    listDir(SD_MMC, "/", 2);
    writeFile(SD_MMC, "/hello.txt", "Hello ");
    appendFile(SD_MMC, "/hello.txt", "World!\n");
    readFile(SD_MMC, "/hello.txt");
    deleteFile(SD_MMC, "/foo.txt");
    renameFile(SD_MMC, "/hello.txt", "/foo.txt");
    readFile(SD_MMC, "/foo.txt");
    testFileIO(SD_MMC, "/test.txt");
    Serial.printf("Total space: %lluMB\n", SD_MMC.totalBytes() / (1024 * 1024));
    Serial.printf("Used space: %lluMB\n", SD_MMC.usedBytes() / (1024 * 1024));

Operation Result

05_audio_out_tf

Example Description

  • This example demonstrates playing MP3 audio from a TF card

Hardware Connection

  • Insert a TF card with an audio file named ff-16b-1c-44100hz.mp3 in the root directory
  • Connect a speaker
  • Connect the development board to the computer.

Code Analysis

  • Initialize I2S and the audio decoder library, and set the playback path:

    audio.setPinout(I2S_BCK_PIN, I2S_LRCK_PIN, I2S_DOUT_PIN,I2S_MCLK_PIN);
    audio.connecttoFS(SD_MMC, "ff-16b-1c-44100hz.mp3");

Operation Result

  • The screen has no display.
  • After flashing the program, MP3 music will start playing automatically.

06_esp_sr

Example Description

  • This example demonstrates using the ES7210 audio ADC chip for voice wake-up and speech recognition

Hardware Connection

  • Connect the development board to the computer.

Code Analysis

  • Initialize I2S and the ES7210 audio ADC:

    Wire.begin(I2C_PIN_SDA, I2C_PIN_SCL);
    es7210_init();
    i2s.setPins(I2S_PIN_BCK, I2S_PIN_WS, I2S_PIN_DOUT, I2S_PIN_DIN, I2S_PIN_MCK);
    i2s.setTimeout(1000);
    i2s.begin(I2S_MODE_STD, 16000, I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_STEREO);
  • Register callbacks for voice wake-up and speech recognition events, then start ESP-SR:

    ESP_SR.onEvent(onSrEvent);
    ESP_SR.begin(i2s, sr_commands, sizeof(sr_commands) / sizeof(sr_cmd_t), SR_CHANNELS_STEREO, SR_MODE_WAKEWORD);

Operation Result

07_I2C_qmi8658

Example Description

  • Initialize the QMI8658 chip via the I2C protocol, then read the corresponding attitude information every 200ms and print it to the terminal.

Hardware Connection

  • Connect the board to the computer using a USB cable.

Code Analysis

  • void i2c_qmi_loop_task(void *arg): Creates a QMI task to acquire attitude information. The task reads and prints accelerometer and gyroscope data at 200ms intervals, and outputs the results to the serial console.

Operation Result

  • Open the Serial Monitor to see the printed raw data from the IMU (Euler angles need to be converted by yourself), as shown in the figure below:

08_I2C_pcf85063

Example 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

  • void i2c_rtc_loop_task(void *arg): Creates an RTC task to implement the RTC function, reading the clock of the RTC chip every second and outputting it to the terminal.

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: