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

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

2. Installing Libraries

To run the demo, you need to install the corresponding library.

You can click this link to download the demo package for the ESP32-S3-Touch-LCD-4.3B development board. The Arduino\libraries directory within this package contains all the necessary library files required for this tutorial.

Library/File NameDescriptionVersionInstallation Method
ESP32_Display_PanelDisplay panel control library for ESP32 microcontrollersv0.1.4 or higher"Install Online" or "Install Offline"
ESP32_IO_ExpanderI/O expander library for ESP32v0.0.4 or higher"Install Online" or "Install Offline"
lvglLVGL graphics libraryv8.4.0Install via library manager or manually
lv_conf.hLVGL configuration file——Install manually
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.

Installation Steps:

  1. Unzip the downloaded demo package.
  2. Copy all folders from its Arduino\libraries directory to your Arduino libraries 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.

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

3. Arduino Project Parameter Settings

ESP32-Arduino-Setting

Demo

The Arduino demos are located in the demo package.

DemoBasic Program DescriptionDependency Library
01_I2C_TestTest I2C header-
02_RS485_TestTest RS485 header-
03_SD_TestTest TF card slot-
04_RTC_TestTest RTC clock-
05_IO_TestTest the I/O expansion/isolation-
06_TWAItransmitTest CAN header-
07_TWAIreceiveTest CAN header-
08_DrawColorBarTest RGB screenESP32_Display_Panel
09_lvgl_PortingTest LVGL portingLVGL, ESP32_Display_Panel

01_I2C_Test

This example tests if the I2C header is working properly: it scans for device addresses on the I2C bus and outputs the scan results.

Hardware Connection

  • Connect the board to the computer using a USB cable

Code Analysis

  • setup():

    • Initializes the serial port (115200) for logging output
    • Initializes I2C (SDA=8, SCL=9)
  • loop():

    • Scans addresses from 0x01 to 0x7E, performing Wire.beginTransmission() and Wire.endTransmission() for each address.
    • error == 0 indicates that a device responds at that address; the address is printed and counted.
    • After scanning, prints the number of devices found and repeats the scan every 5 seconds.

Operation Result

  • The screen shows nothing, and the connected LED light flashes at a frequency of 1 Hz..

    ESP32_Arduino_01

02_RS485_Test

This example demonstrates how to perform a serial loopback test using RS485 (it echoes back whatever is sent).

Hardware Connection

  • Connect the board to the computer using a USB cable

    ESP32_Arduino_CAN_Connect

Code Analysis

  • setup():

    • Initializes the RS485 serial port using Serial1 with baud rate 115200, 8N1
    • Specifies the RS485 RX/TX pins as GPIO43/GPIO44
  • loop():

    • Polls RS485.available(): when data is received, reads 1 byte and immediately write() it back, implementing a loopback test.

Operation Result

  • Data sent to the development board via the RS485 interface is echoed back.

    ESP32_Arduino_02

03_SD_Test

This example demonstrates how to mount a TF card and perform basic read/write and directory traversal tests.

Hardware Connection

  • Connect the board to the computer using a USB cable

Code Analysis

  • setup():
    • Initializes the CH422G I/O expander and configures relevant control pins (touch reset, LCD reset, backlight, USB_SEL, SD_CS)
    • Initializes SPI and calls SD.begin() to mount the TF card
    • Reads and prints the TF card type and capacity
    • Performs a series of file system read/write tests using listDir/createDir/removeDir/writeFile/appendFile/readFile/deleteFile/renameFile/testFileIO

Operation Result

  • Serial output shows the TF card type, capacity, and file system read/write test logs; if no card is inserted, a mount failure message is displayed.

    ESP32_Arduino_03

04_RTC_Test

This example demonstrates how to use the PCF85063A real-time clock module for time setting and alarm function testing.

Hardware Connection

  • Connect the board to the computer using a USB cable

Code Analysis

  • setup():

    • Initializes the serial port (115200)
    • Calls PCF85063A_Init() to initialize the RTC and set the current time and alarm time.
    • Configures the RTC interrupt pin as a pull-up input and registers an interrupt callback with attachInterrupt().
  • loop():

    • Periodically reads the current time, converts it to a string, and outputs it to the serial port.
    • Checks the Alarm_flag and prints a notification when the alarm is triggered, then re-enables the alarm.

Operation Result

  • The serial port periodically outputs the current time, and prints an alert when the alarm triggers.

    ESP32_Arduino_04

05_IO_Test

This example tests the I/O expansion/isolation functionality: it drives isolated DO/DI via the CH422G expansion chip and performs a simple self-test.

Hardware Connection

  • Connect the board to the computer using a USB cable

    ESP32_Arduino_IO_Connect

Code Analysis

  • setup():

    • Initializes the serial port (115200)
    • Calls waveshare_io_test() to perform the isolated I/O self-test.
    • Calls waveshare_lcd_init() to initialize the LCD for displaying test interfaces or status.
  • loop():

    • Prints a log every second; if EXAMPLE_ENABLE_PRINT_LCD_FPS is enabled, outputs the LCD refresh frame rate.

Operation Result

  • Upon program start, the I/O test is automatically performed, and the corresponding status is output on the serial port/screen.

    ESP32_Arduino_05

06_TWAItransmit

This example tests the CAN header: initializes the TWAI driver and periodically sends CAN frames.

Hardware Connection

  • Connect the board to the computer using a USB cable

Code Analysis

  • setup():

    • Initializes the serial port (115200)
    • Initializes the CH422G I/O expander and configures the relevant control pin states.
    • Calls waveshare_twai_init() to initialize the TWAI (CAN) driver and stores the initialization result in driver_installed.
  • loop():

    • If driver initialization fails, waits 1 second and retries.
    • If the driver is ready, calls waveshare_twai_transmit() to send a CAN frame.

Operation Result

  • Serial output shows transmission logs; after connecting to a CAN bus and a peer device, the CAN data can be received on the peer side.

    ESP32_Arduino_06

    ESP32_Arduino_CN_01

07_TWAIreceive

This example tests the CAN header: initializes the TWAI driver and receives CAN frames.

Hardware Connection

  • Connect the board to the computer using a USB cable

  • Connect the CAN interface (CANH, CANL) to a CAN bus

    ESP32_Arduino_CAN_Connect

Code Analysis

  • setup():

    • Initializes the serial port (115200)
    • Initializes the CH422G I/O expander and configures the relevant control pin states.
    • Calls waveshare_twai_init() to initialize the TWAI (CAN) driver and stores the initialization result in driver_installed.
  • loop():

    • If driver initialization fails, waits 1 second and retries.
    • If the driver is ready, calls waveshare_twai_receive() to read and print received CAN frames.

Operation Result

  • Serial output shows received CAN data; after connecting to a CAN bus and a peer device, received frames can be observed.

    ESP32_Arduino_07

    ESP32_Arduino_CN_02

    ESP32_Arduino_CN_03

08_DrawColorBar

This example tests the RGB screen: initializes the LCD and displays a color bar / test pattern.

Hardware Connection

  • Connect the board to the computer using a USB cable

Code Analysis

  • setup():

    • Initializes the serial port and outputs logs.
    • Calls waveshare_lcd_init() to initialize the RGB LCD and display the test pattern.
  • loop():

    • Outputs a log every second to confirm that the program is running.

Operation Result

  • After programming, the LCD displays a color bar/test pattern.

    ESP32_Arduino_09

    ESP32_Arduino_08

09_lvgl_Porting

This example tests LVGL porting: initializes ESP32_Display_Panel + LVGL and runs an LVGL Demo.

Hardware Connection

  • Connect the board to the computer using a USB cable

Code Analysis

  • setup():

    • Initializes the serial port and creates a Board object to initialize board-level peripherals (LCD/Touch/bus).
    • Initializes the LVGL port (lvgl_port_init()) and uses lvgl_port_lock()/lvgl_port_unlock() to protect LVGL API calls.
    • Creates three labels to display basic information and calls lv_demo_widgets() to run the built-in LVGL demo.
  • loop():

    • Prints a log every second to confirm the main loop is running (UI refresh is handled by the LVGL port's internal task).

Operation Result

  • After programming, the LVGL Demo interface (Widgets Demo) is displayed.

    lv_image