Skip to main content

Working with Arduino

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

Before Reading

Before running the examples, please ensure the following conditions are met:

  • The ESP32-C5-Touch-LCD-2.8 or ESP32-C5-Touch-LCD-2.8-EN development boards are ready.
  • The board is connected to your computer via a USB data cable.
  • You have installed the Arduino IDE.
  • You have installed the ESP32 Arduino Core. The corresponding example directory is Arduino-v3.3.10. Please use an Arduino-ESP32 version that matches the example package.
  • You have downloaded the product example program package.
  • To run 04_I2C_pcf85063, install the lib/SensorLib included in the example package. To run 06_lvgl_demo, install the lib/lvgl included in the example package.

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 the Arduino IDE to download and install the Arduino IDE.

2. Installing the ESP32 Board Support Package

Install the esp32 by Espressif Systems board support package in the Arduino IDE Board Manager.

Version Note

The ESP32-C5-Touch-LCD-2.8 uses the ESP32-C5-WROOM-1-N32R8 MCU module, which is a v1.2 chip. Please use version v3.3.10 of the esp32 by Espressif Systems package.

If you use an older version of the ESP32 Arduino core, the board may not be correctly identified, causing serial download failures or preventing the program from being flashed.

ESP32-C5-Touch-LCD-2.8 Board Installation Requirements:

Board NameBoard Installation RequirementVersion Requirement
ESP32 by Espressif Systems"Offline Installation" / "Online Installation"v3.3.10

After installation, select the appropriate ESP32-C5 board in Arduino IDE and select the USB serial port currently connected.

Arduino Project Parameter Settings

In the Arduino project settings for the ESP32-C5-Touch-LCD-2.8, select ESP32C5 Dev Module as the board.

3. Installing Libraries and Example Dependencies

The Arduino examples for this product minimize external library dependencies, primarily using the built-in capabilities of the Arduino ESP32 core and the source code provided within the example directories. The example package already includes the required libraries in the example/Arduino-v3.3.10/lib directory. You can copy them manually for offline installation, which is suitable for environments without internet access or where the Arduino Library Manager is inconvenient to use.

Library or FilePurposeRecommended Version / SourceExamplesInstallation Method
Arduino ESP32 coreESP32-C5 board support, download, and basic peripheral interfacesv3.3.10All examplesArduino IDE Board Manager
lvglLVGL graphics frameworklib/lvgl in example package, LVGL v8.4.006_lvgl_demoOffline copy to Arduino libraries directory
SensorLib / SensorPCF85063.hppPCF85063 RTC driverlib/SensorLib in example package, SensorLib v0.3.104_I2C_pcf85063Offline copy to Arduino libraries directory
LVGL Version

06_lvgl_demo is based on LVGL v8.4.0. Do not replace it with LVGL v9, as interfaces such as lv_disp_drv_t, lv_disp_draw_buf_t, lv_disp_drv_register(), and lv_indev_drv_t are not compatible.

Offline / Manual Installation:

  1. Download and extract the product example program package. Subsequent library installation can be done offline without downloading third-party libraries from the Arduino Library Manager.

  2. Locate the library directories in the example package:

    example/Arduino-v3.3.10/lib/lvgl
    example/Arduino-v3.3.10/lib/SensorLib
  3. Copy the lvgl and SensorLib folders to your Arduino libraries directory.

    The default libraries directory on Windows is usually:

    C:\Users\<username>\Documents\Arduino\libraries

    You can also check the Sketchbook location via File > Preferences in the Arduino IDE; the libraries folder under that path is the libraries directory.

  4. Restart Arduino IDE after installation, then open the examples to compile.

Installation Verification:

  • The lvgl and SensorLib directories can be seen under Documents\Arduino\libraries.
  • Documents\Arduino\libraries\SensorLib\library.properties shows version=0.3.1.
  • Documents\Arduino\libraries\lvgl\library.properties shows version=8.4.0.
  • The lv_conf.h file in the 06_lvgl_demo directory must remain in the example directory. Do not move it to the global Arduino libraries directory or delete it.
  • Do not keep multiple different versions of lvgl or SensorLib simultaneously to avoid header file conflicts.
  • If compiling 04_I2C_pcf85063 fails with SensorPCF85063.hpp not found, confirm that lib/SensorLib from the example package has been copied to the Arduino libraries directory.

4. Arduino Project Parameter Settings

  1. Open Arduino IDE.
  2. Install esp32 by Espressif Systems v3.3.10.
  3. In Tools > Board, select ESP32C5 Dev Module.
  4. Select the USB serial port currently connected.
  5. Open the corresponding .ino file in example/Arduino-v3.3.10 to compile and flash.

When using arduino-cli, refer to the following commands:

arduino-cli core install esp32:esp32@3.3.10
arduino-cli compile --fqbn esp32:esp32:esp32c5 example/Arduino-v3.3.10/06_lvgl_demo

Example

The Arduino examples are located in the example/Arduino-v3.3.10 directory of the example package. Each example has been adapted to the actual hardware connections of the ESP32-C5-Touch-LCD-2.8.

1. Onboard Resources

FeatureDevice or InterfacePins or Notes
LCDST7789, SPI, 240 x 320, RGB565SCLK GPIO6, MOSI GPIO7, DC GPIO9, CS GPIO10
LCD ResetCH32V003 I/O ExpanderEXIO1
LCD BacklightCH32V003 PWMCH32 PWM
TouchCST3530, I2CSDA GPIO0, SCL GPIO1, INT GPIO5
Touch ResetCH32V003 I/O ExpanderEXIO0
I/O ExpanderCH32V003, I2C address 0x24SDA GPIO0, SCL GPIO1
6‑axis IMUQMI8658, I2C address 0x6BSDA GPIO0, SCL GPIO1
RTCPCF85063, I2C address 0x51SDA GPIO0, SCL GPIO1
Temp/Humidity SensorSHTC3, I2C address 0x70SDA GPIO0, SCL GPIO1
TFSDSPISCLK GPIO6, MOSI GPIO7, MISO GPIO8, CS GPIO23
Secondary Development Recommendations

Display, touch, backlight, reset, and sensor pins have already been adapted to the product hardware connections in the examples. For routine secondary development, we recommend focusing on application‑layer and UI logic modifications. Do not change the underlying pin definitions unless you are porting the code to a different board or modifying the hardware connections.

2. Example List

Example DirectoryBasic DescriptionMain Test Content
01_exioTests the onboard CH32V003 I/O expander, toggles EXIO0~EXIO7 output levels cyclicallyI/O expansion, I2C bus
02_I2C_qmi8658Reads QMI8658 6‑axis sensor data and outputs accelerometer and gyroscope data over serialIMU, I2C bus
03_SD_CardMounts a TF card via SDSPI, prints TF card info and capacity, and provides file read/write interfacesTF, SDSPI
04_I2C_pcf85063Initializes the PCF85063 RTC, sets a test time, and periodically prints RTC time over serialRTC, I2C bus
05_shtc3Reads SHTC3 temperature and humidity sensor ID, temperature, and humidity, and outputs over serialTemp/Humidity sensor, CRC check
06_lvgl_demoDisplays a touch example UI using ST7789, CST3530, and LVGL v8.4.0LCD, backlight, touch, LVGL

3. Directory Structure

The current Arduino examples directory contains the following projects:

example/Arduino-v3.3.10
|-- 01_exio
|-- 02_I2C_qmi8658
|-- 03_SD_Card
|-- 04_I2C_pcf85063
|-- 05_shtc3
|-- 06_lvgl_demo
`-- lib
|-- SensorLib
`-- lvgl

Each subdirectory is an independent Arduino example. To open an example, open the .ino file inside that directory, for example:

example/Arduino-v3.3.10/01_exio/01_exio.ino

lib/SensorLib is the RTC driver library used by 04_I2C_pcf85063 (SensorLib v0.3.1). lib/lvgl is the LVGL v8.4.0 library used by 06_lvgl_demo. Before compiling 04_I2C_pcf85063, you need to copy SensorLib at minimum. Before compiling 06_lvgl_demo, you need to copy lvgl at minimum.

It is recommended to run the examples in the following order:

01_exio
-> 02_I2C_qmi8658
-> 03_SD_Card
-> 04_I2C_pcf85063
-> 05_shtc3
-> 06_lvgl_demo
  • Start with 01_exio to verify the CH32V003 I/O expander and I2C bus. LCD reset, touch reset, and backlight are all related to CH32V003, so confirm this example works first.
  • Then use 02_I2C_qmi8658 to verify 6‑axis sensor data reading.
  • Then use 03_SD_Card to verify the TF card and SDSPI.
  • Then use 04_I2C_pcf85063 to verify RTC time setting and reading.
  • Then use 05_shtc3 to verify SHTC3 temperature/humidity reading and CRC checking.
  • Finally, run 06_lvgl_demo to verify LCD, backlight, touch, and LVGL all together.

5. Usage Notes

  • The Arduino example directory is example/Arduino-v3.3.10. Do not mix it with the ESP‑IDF example directory.
  • This product uses a shared I2C bus: SDA GPIO0, SCL GPIO1. CH32V003, QMI8658, PCF85063, SHTC3, and CST3530 are all on this I2C bus.
  • Insert a FAT or FAT32‑formatted TF card before running 03_SD_Card.
  • 04_I2C_pcf85063 writes a fixed test time on every power‑up by default. To enable continuous RTC time‑keeping, comment out i2c_rtc_setTime().
  • 06_lvgl_demo has already been adapted for ST7789 display and CST3530 touch. No additional GFX display library or third‑party touch library is required.
  • If you copy code from other projects, pay special attention to the LCD and touch reset pins: LCD_RST = CH32 EXIO1, TP_RST = CH32 EXIO0.
  • For compilation, display, touch, I2C address, and other troubleshooting, refer to the FAQ.

6. Example Details

01_exio

Function Description

This example verifies communication with the onboard CH32V003 I/O expander. CH32V003 communicates with the ESP32‑C5 via I2C at address 0x24. The example configures EXIO0~EXIO7 as outputs and toggles them every second.

CH32V003 is pre‑programmed with firmware at the factory; no separate CH32 firmware flashing is required.

Code Entry

01_exio/01_exio.ino
01_exio/io_extension.cpp
01_exio/io_extension.h

Recommended key code sections:

CodePurpose
Wire.begin(I2C_SDA, I2C_SCL)Initializes I2C with SDA GPIO0, SCL GPIO1
IO_EXTENSION_ADDRCH32V003 I2C, default 0x24
IO_EXTENSION_Init(Wire)Initializes the I/O expander
IO_EXTENSION_IO_Mode(0xFF)Configures EXIO0~EXIO7 as outputs
IO_EXTENSION_Output()Sets the output level of a specified EXIO pin

Expected Behavior

  • Serial log output should look similar to this:

Arduino EXIO test serial log

Troubleshooting

SymptomPossible CauseAction
Serial shows IO extension not found at 0x24I2C communication failureConfirm SDA GPIO0, SCL GPIO1 and ensure the board is powered
LCD or touch malfunctions laterCH32V003 not communicatingFirst ensure 01_exio works, then debug display and touch
Need to re‑flash CH32V003 firmwareNot requiredCH32V003 is pre‑programmed at the factory; no action needed for normal use

02_I2C_qmi8658

Function Description

This example reads the onboard QMI8658 6‑axis sensor. After initializing I2C, the program creates a qmi8658c_example task that reads accelerometer and gyroscope data every second and outputs it over serial.

Code Entry

02_I2C_qmi8658/02_I2C_qmi8658.ino
02_I2C_qmi8658/i2c_bsp.cpp
02_I2C_qmi8658/qmi8658c.cpp

Recommended key code sections:

CodePurpose
I2C_master_Init()Initializes I2C with SDA GPIO0, SCL GPIO1, 400 kHz
QMI8658_SLAVE_ADDR_HQMI8658 I2C address, default 0x6B
qmi8658_init()Initializes QMI8658
qmi8658_read_xyz()Reads accelerometer and gyroscope data

Expected Behavior

  • Serial outputs QMI8658 initialization information, followed by sensor data every second.
  • When you gently tilt or rotate the board, the accelerometer and gyroscope values change accordingly. The log should look like this:

Arduino QMI8658 test serial log

Troubleshooting

SymptomPossible CauseAction
Serial shows qmi8658_init failQMI8658 I2C communication failureConfirm I2C pins are GPIO0/GPIO1; run 01_exio or 05_shtc3 first to verify I2C
Data remains unchangedBoard is stationary or task not runningGently rotate the board and confirm the serial baud rate is 115200
Compilation errors related to I2C interfacesArduino ESP32 core version mismatchUse esp32 by Espressif Systems v3.3.10

03_SD_Card

Function Description

This example verifies the TF card interface. It uses SDSPI mode to mount the TF card at /sd_card by default, and prints TF card information and capacity upon success.

Code Entry

03_SD_Card/03_SD_Card.ino
03_SD_Card/sd_card_bsp.cpp
03_SD_Card/sd_card_bsp.h

Recommended key code sections:

CodePurpose
PIN_NUM_CLKTF SCLK, GPIO6
PIN_NUM_MOSITF MOSI, GPIO7
PIN_NUM_MISOTF MISO, GPIO8
PIN_NUM_CSTF CS, GPIO23
SD_card_Init()Initializes SPI bus and mounts TF card
s_example_write_file() / s_example_read_file()Example file write/read interfaces

Expected Behavior

  • With a FAT32‑formatted TF card inserted, the serial prints TF card type, capacity, and other information. The log should look like this:

Arduino TF card test serial log

Troubleshooting

SymptomPossible CauseAction
TF card mount failsNo card inserted, invalid format, or poor contactUse a FAT/FAT32 TF card, re‑insert, and retest
Compilation errors related to SDMMC/SDSPI interfacesArduino ESP32 core version mismatchUse v3.3.10
File read/write failsMount failure or incorrect pathConfirm SD_card_Init() prints card info successfully

04_I2C_pcf85063

Function Description

This example verifies the onboard PCF85063 RTC. The program initializes the RTC and calls the following in setup():

i2c_rtc_setTime(2025, 9, 9, 14, 51, 30);

It then creates an i2c_rtc_loop_task task that reads and prints RTC time every second.

Code Entry

04_I2C_pcf85063/04_I2C_pcf85063.ino
04_I2C_pcf85063/rtc_bsp.cpp
04_I2C_pcf85063/rtc_bsp.h

Recommended key code sections:

CodePurpose
rtc.begin(Wire, SENSOR_SDA, SENSOR_SCL)Initializes PCF85063 with SDA GPIO0, SCL GPIO1
i2c_rtc_setTime()Sets RTC time
i2c_rtc_get()Reads RTC time structure
i2c_rtc_loop_task()Periodically reads and prints RTC time over serial

Expected Behavior

  • Serial outputs RTC time every second. The log should look like this:

Arduino PCF85063 test serial log

Since the example writes a fixed test time on every power‑up, if you want to verify continuous RTC time‑keeping, comment out or delete i2c_rtc_setTime().

Troubleshooting

SymptomPossible CauseAction
Compilation fails with SensorPCF85063.hpp not foundMissing SensorLib dependencyCopy example/Arduino-v3.3.10/lib/SensorLib to Arduino libraries directory
Serial shows Failed to find PCF85063RTC I2C communication failureConfirm I2C pins are GPIO0/GPIO1; test other I2C examples first
Time always resets to fixed value on power‑upExample actively writes test timeComment out or delete i2c_rtc_setTime() and retest

05_shtc3

Function Description

This example reads the onboard SHTC3 temperature and humidity sensor. The program uses Wire to send SHTC3 commands, wakes the sensor, reads its ID, performs a soft reset, and reads temperature and humidity every second in loop().

CRC checking is implemented to prevent invalid data from being treated as normal temperature/humidity readings.

Code Entry

05_shtc3/05_shtc3.ino

Recommended key code sections:

CodePurpose
SHTC3_ADDRSHTC3 I2C address, default 0x70
SHTC3_CMD_READ_IDReads sensor ID
SHTC3_CMD_MEASURE_T_RHTriggers temperature/humidity measurement
shtc3Crc()CRC check for SHTC3 data
readShtc3()Reads and converts temperature and humidity

Expected Behavior

  • Serial outputs the SHTC3 ID first, then periodically outputs temperature and humidity. The log should look like this:

Arduino SHTC3 temperature and humidity test serial log

Troubleshooting

SymptomPossible CauseAction
Serial shows SHTC3 not foundI2C communication failureConfirm I2C pins are GPIO0/GPIO1 and ensure the board is powered
Serial shows Failed to read SHTC3CRC failure or sensor not respondingKeep power stable, power cycle, and retest
Occasional abnormal temperature/humidity valuesInvalid I2C dataThe example includes CRC checking; use data that passes the check

06_lvgl_demo

Function Description

This example verifies LCD display, backlight, touch, and LVGL basic UI. It does not depend on the GFX display library. The LCD driver is adapted from the ST7789 example into Display_ST7789, the touch driver is adapted from the CST3530 example into Touch_CST3530, and CH32V003 I/O expansion, LCD reset, touch reset, and backlight control are consolidated in Board_IO.

The UI displays the board name, display status, touch status, touch coordinates, and backlight brightness.

Code Entry

06_lvgl_demo/06_lvgl_demo.ino
06_lvgl_demo/Board_IO.cpp
06_lvgl_demo/Board_IO.h
06_lvgl_demo/Display_ST7789.cpp
06_lvgl_demo/Display_ST7789.h
06_lvgl_demo/Touch_CST3530.cpp
06_lvgl_demo/Touch_CST3530.h
06_lvgl_demo/lv_conf.h

Recommended key code sections:

CodePurpose
LCD_WIDTH / LCD_HEIGHTLCD resolution, 240 x 320
EXAMPLE_PIN_NUM_SCLK / EXAMPLE_PIN_NUM_MOSILCD SPI pins, GPIO6/GPIO7
EXAMPLE_PIN_NUM_LCD_DC / EXAMPLE_PIN_NUM_LCD_CSLCD DC/CS pins, GPIO9/GPIO10
BOARD_LCD_RST_EXIOLCD reset, CH32V003 EXIO1
BOARD_TP_RST_EXIOTouch reset, CH32V003 EXIO0
CST3530_ADDRCST3530 I2C address, default 0x58
LCD_Init()Initializes SPI bus and ST7789 panel
TOUCH2_Init()Initializes CST3530 touch
create_demo_ui()Creates LVGL example UI

Expected Behavior

The LCD displays the ESP32-C5 Touch LCD 2.8 example UI. When you touch the screen, the UI updates with touch coordinates. Dragging the progress bar at the bottom adjusts backlight brightness. Serial output shows:

ESP32-C5-Touch-LCD-2.8 Arduino LVGL demo
Touch controller CST3530 initialized
LVGL demo started

If touch is not recognized, the UI displays Touch: not found.

Troubleshooting

SymptomPossible CauseAction
Compilation fails with lvgl.h not foundLVGL not installed from example packageCopy example/Arduino-v3.3.10/lib/lvgl to Arduino libraries directory
Compilation errors related to lv_disp_drv_t / lv_indev_drv_tLVGL v9 usedRemove the incorrect version and use LVGL v8.4.0 from the example package
Screen does not light upCH32V003, LCD reset, backlight, or ST7789 initialization failureRun 01_exio first to confirm CH32V003 is working; then restore original pin configuration in 06_lvgl_demo
Touch does not respondCST3530 not detected or touch reset issueConfirm I2C uses GPIO0/GPIO1, touch interrupt is GPIO5, and touch reset is CH32 EXIO0
Display colors are incorrectRGB565 byte order or color depth modifiedConfirm lv_conf.h uses LV_COLOR_DEPTH 16 and keep the LV_COLOR_16_SWAP configuration from the example