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-5 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.

ESP32-S3-LCD-5 required development board installation description

Board NameInstallation RequirementVersion Requirement
ESP32 by Espressif Systems"Install Offline" / "Install Online"3.0.7

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 module-
05_IO_TestUART loopback test-
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_RS485_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, 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)
    • Sets ADC resolution to 12 bits (0~4095)
  • loop():

    • Calls analogRead() to read the raw ADC value
    • Calls analogReadMilliVolts() to read the corresponding voltage (mV)
    • Prints the results to the serial port and repeats sampling every 100 ms

Operation Result

  • The serial port continuously outputs the raw ADC value and voltage (mV).

    ESP32_Arduino_04

05_IO_Test

ESP32_Arduino_IO_connect

This example tests UART: received data is echoed back (loopback).

Hardware Connection

  • Connect the board to the computer using a USB cable

Code Analysis

  • setup():

    • Initializes UART (using Serial here, baud rate 115200)
  • loop():

    • Polls UART.available(): when data is received, it reads one byte and immediately writes it back.

Code Modification

In waveshare_io_port.h, there is a macro definition to select the resolution. A value of 0 corresponds to 800x480, and a value of 1 corresponds to 1024x600. Choose according to the purchased model.

 #define ESP_PANEL_USE_1024_600_LCD           (0)     // 0: 800x480, 1: 1024x600

Operation Result

  • After sending data via the serial port, the development board will echo it back.

    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

    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_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
  • Insert a TF card with images into the ESP32-S3-Touch-LCD-5

    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.

Code Modification

In waveshare_lcd_port.h, there is a macro definition to select the resolution. A value of 0 corresponds to 800x480, and a value of 1 corresponds to 1024x600. Choose according to the purchased model.

#define ESP_PANEL_USE_1024_600_LCD           (0)     // 0: 800x480, 1: 1024x600

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).

Code Modification

  • In ESP_Panel_Board_Custom.h, there are two macro definitions to select the resolution and whether to enable touch:
  • ESP_PANEL_USE_1024_600_LCD: 0 for 800x480, 1 for 1024x600, choose according to the purchased model.
  • ESP_OPEN_TOUCH: 0 to disable touch, 1 to enable touch.
#define ESP_PANEL_USE_1024_600_LCD           (0)     // 0: 800x480, 1: 1024x600
#define ESP_OPEN_TOUCH 0 // 1 initiates the touch, 0 closes the touch.

Operation Result

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

    lv_image