Arduino Peripheral Development
The Arduino-TuyaOpen framework provides a rich set of peripheral drivers and communication libraries, letting you implement WiFi connectivity, Bluetooth communication, audio/video capture, display rendering, file storage, cloud access, and more in the Arduino IDE. The examples below are organized by functional category so you can master each peripheral library step by step.
Peripheral & Networking Libraries
โโโ Log - Log Output
โโโ Ticker - Software Timer
โโโ Peripherals - Button Driver
โโโ SPI - SPI Bus Communication
โโโ Wire - I2C Bus Communication
โโโ FS / FSDemo - LittleFS / SD Card File System
โโโ Audio - Audio Recording & Playback
โโโ Display - LCD Display & LVGL
โโโ Camera - Camera Capture & Preview
โโโ BLE - BLE GATT Service
โโโ WiFi - WiFi STA/AP/Scan/Events
โโโ TuyaIoT - Tuya IoT Cloud Access
โโโ HTTPClient - HTTP/HTTPS Client
โโโ MQTTClient - MQTT Messaging
โโโ DNSServer - DNS Server (Captive Portal)
Log outputโ
The Log library provides unified log output with control over different log levels.
logOutputโ
Basic log output example. It uses the Log class and the PR_DEBUG macro to output debug log information โ the fundamental logging usage for all projects.
- Uses
Log.begin()to initialize the log system. - Supports log levels including
PR_DEBUG,PR_NOTICE,PR_ERR, and more. - Hardware platform: all development boards
Software timerโ
The Ticker library provides lightweight software timers that execute callbacks on a schedule, suitable for periodic tasks.
Blinkerโ
Timer-based LED blink example. It uses the Ticker class to create a timer that toggles the LED state at a fixed interval to produce a blinking effect.
- Uses
ticker.attach()to set the period and callback function. - Hardware platform: all development boards
TickerParameterโ
Parameterized timer callback example. It uses the Ticker class's parameterized callback to pass custom parameters to the timer callback, enabling more flexible timed tasks.
- Uses the parameterized overload of
ticker.attach(). - Hardware platform: all development boards
Peripheral interfacesโ
Buttonโ
Multi-button event-driven example. It uses the Button class to manage multiple buttons, configuring GPIO pins, active levels, and pull-up modes for each, and registers callbacks for events such as single click, double click, and long press.
- Uses
ButtonConfig_t/PinConfig_tto configure button parameters. - Supports event types including
BUTTON_EVENT_SINGLE_CLICK,BUTTON_EVENT_DOUBLE_CLICK,BUTTON_EVENT_LONG_PRESS, and more. - Hardware platform: all development boards
spiDemoโ
SPI bus communication example. It uses the SPI class for SPI data transfer โ after initializing the SPI bus, data is sent and return values received via SPI.transfer().
- Uses the standard Arduino SPI interface.
- Hardware platform: all development boards that support SPI
masterWriterโ
I2C master write example. It uses the Wire class as an I2C master to send data to a slave device โ after initializing the I2C bus, byte data is periodically sent to a specified slave address.
- Uses the standard Arduino Wire interface:
Wire.beginTransmission(),Wire.write(),Wire.endTransmission(). - Hardware platform: all development boards that support I2C
File systemโ
The FSDemo library provides file operation interfaces for both the LittleFS internal file system and SD card external storage, using the unified VFSFILE class for file read/write and directory management.
LittleFSDemoโ
LittleFS file system operations example. It demonstrates basic LittleFS operations, including file creation, writing, reading, seeking (seek/position), file size query, directory creation and deletion, file renaming, and cross-file copying.
- Uses
VFSFILE fs(LITTLEFS)to initialize LittleFS. - Supports operations such as
open,read,write,lseek,readtillN,mkdir,rename, andremove. - Hardware platform: all development boards that support LittleFS
SDCardDemoโ
SD card file operations example. It demonstrates the complete SD card workflow, including basic operations (create directory, create file, get file size), file read/write operations (multi-line writing, full read, append writing, line-by-line reading), and directory traversal (listing all files and sizes in a directory).
- Uses
VFSFILE fs(SDCARD)to initialize the SD card. - Includes SD card mount detection and error handling.
- Hardware platform: T5AI-Board development board only (requires SD card)
Audioโ
The Audio library provides audio recording and playback, supporting PCM raw data and MP3 decoded playback, suitable for voice capture, audio playback, and similar scenarios.
Audio2SDcardโ
Button-triggered recording-to-SD-card example. It controls microphone recording via a button, saving the captured PCM raw audio data or WAV formatted audio to an SD card. It supports switching between PCM and WAV save formats via a macro definition.
- Uses the
Audioclass to configure sample rate, bit depth, channel count, and other audio parameters. - Uses the
Buttonclass to start recording on press and stop on release. - WAV mode automatically generates the file header.
- Hardware platform: T5AI-Board development board only (requires SD card)
AudioRecorderโ
Audio recording and playback example. It records an audio clip and immediately plays it back โ recording is triggered by a button press, and playback starts automatically upon completion. Audio data is temporarily stored in a memory buffer.
- Suitable for intercom, voice message, and similar scenarios.
- Hardware platform: TUYA-T5AI series only
AudioSpeakerโ
MP3 audio playback example. It demonstrates three ways to play MP3 audio: from a C array (embedded in firmware), from the Flash file system, and from an SD card file, with playback status callback support.
- Uses the
AudioclassplayMp3()series of interfaces. - Supports playback completion callback notification.
- Hardware platform: TUYA-T5AI series only
Displayโ
The Display library provides basic LCD rendering capabilities and LVGL graphics library integration, supporting color block filling, image display, rotation, and LVGL UI development.
DisplayFillโ
Screen color fill example. It uses the Display class to fill color blocks on the LCD screen โ after initializing the display, the entire screen is filled with random colors at regular intervals.
- Uses
display.fill()for full-screen or regional color filling. - Hardware platform: T5AI-Board development board only
DisplayPictureโ
Image display and rotation example. It displays images on the LCD screen and sets the screen rotation direction (0ยฐ/90ยฐ/180ยฐ/270ยฐ) using display.setRotation(), showing images in different orientations.
- Supports loading image data from C arrays.
- Hardware platform: T5AI-Board development board only
LVGLdemoโ
LVGL graphics library example. It uses LVGL (Light and Versatile Graphics Library) to create a UI on the device โ after initializing the LVGL environment, a label widget displaying "Hello World" text is created.
- Uses
lv_vendorfor LVGL initialization and refresh. - Can be extended to build more complex UI interfaces.
- Hardware platform: T5AI-Board development board only
Cameraโ
The Camera library provides camera capture capabilities, supporting YUV422 real-time preview and JPEG/H264 encoded output, suitable for photo capture, video recording, and similar scenarios.
Camera2Displayโ
Camera real-time preview example. It initializes the camera and display, continuously captures image frames in YUV422 format, and displays them on the LCD screen in real time for a live camera preview.
- Uses the
Cameraclass to capture YUV422 frames and theDisplayclass to render them. - Hardware platform: T5AI-Board development board only
Camera2SDcardโ
Camera photo/video capture example. It controls the camera via a button to take photos or record video, saving the data to an SD card. It supports two modes (switchable via macro definition): in JPEG mode, a single button click captures one photo; in H264 mode, holding the button records continuously and releasing stops recording.
- Real-time YUV422 preview runs simultaneously with encoding output.
- JPEG photos are named with timestamps; H264 video is written frame by frame.
- Hardware platform: T5AI-Board development board only (requires SD card)
Bluetooth Low Energy (BLE)โ
The BLE library provides BLE GATT server functionality, supporting custom Services and Characteristics for data interaction with phones or other BLE devices.
ble_serverโ
BLE GATT server example. It creates a BLE GATT server โ defining custom Service UUIDs and Characteristic UUIDs, setting read/write properties and Notify/Indicate notification capabilities, and handling client connection, read/write, and subscription events through callback functions.
- Uses
BLEDEV,BLEServer,BLEService, andBLECharacteristicclasses to build the GATT service. - Supports both Notify and Indicate notification methods.
- Hardware platform: development boards with BLE support
WiFi networkingโ
The WiFi library provides comprehensive wireless networking, including STA mode for connecting to a router, AP mode for creating a hotspot, network scanning, event listening, static IP configuration, multi-AP automatic switching, and more. The following examples cover common WiFi development scenarios.
SimpleWiFiServerโ
HTTP server example in WiFi STA mode. It starts a simple HTTP server after connecting to WiFi, letting you control an LED on a GPIO pin by visiting the device's IP address in a browser.
- Uses
WiFiServerto listen on port 80; parses HTTP request paths/Hand/Lto control the LED. - Hardware platform: all WiFi-capable development boards
WiFiAccessPointโ
WiFi AP (hotspot) mode example. It uses WiFi.softAP() to create a wireless hotspot and starts an HTTP server on it, enabling devices connected to the hotspot to control the onboard LED via a browser.
- Suitable for direct device control scenarios without a router.
- Hardware platform: all WiFi-capable development boards
WiFiClientโ
WiFi client data reporting example. It uses WiFiClient to connect to the ThingSpeak cloud platform, periodically upload sensor data and read historical records, showing the complete read/write flow of HTTP GET requests.
- Replace with your own ThingSpeak Channel ID and API Key.
- Hardware platform: all WiFi-capable development boards
WiFiClientBasicโ
Basic WiFi TCP client example. It uses WiFiClient to establish a TCP connection, send an HTTP GET request to a remote server, and read the response data โ the most fundamental network communication example.
- Hardware platform: all WiFi-capable development boards
WiFiClientConnectโ
WiFi connection state machine example. It implements complete connection state management by polling the WiFi.status() return value, handling states such as connected, AP not found, and connection failed, with support for button-triggered disconnection.
- Demonstrates the recommended approach for production-grade WiFi connection management.
- Hardware platform: all WiFi-capable development boards
WiFiClientEventsโ
WiFi event callback example. It uses WiFi.onEvent() to register global event callbacks and specific event callbacks (for example, IP acquired or disconnected), covering event handling for all event types including STA, AP, WPS, and Ethernet.
- Supports multiple callback registration methods including function pointers and lambda expressions.
- Hardware platform: all WiFi-capable development boards
WiFiClientStaticIPโ
WiFi static IP configuration example. It uses WiFi.config() to set a static IP address, gateway, subnet mask, and DNS server. After a successful connection, the full network configuration is printed and an HTTP request is initiated.
- Suitable for deployment scenarios requiring a fixed IP address.
- Hardware platform: all WiFi-capable development boards
WiFiMultiโ
Multi-AP automatic switching example. It uses the WiFiMulti class to add multiple WiFi hotspots. The device automatically selects the hotspot with the strongest signal and switches to another available hotspot when disconnected.
- Suitable for scenarios requiring seamless roaming between multiple WiFi environments.
- Hardware platform: all WiFi-capable development boards
WiFiScanโ
WiFi network scanning example. It uses WiFi.scanNetworks() to scan nearby WiFi networks and print each network's SSID, RSSI signal strength, channel, and encryption type.
- Supports identification of OPEN, WEP, WPA PSK, WPA2 PSK, WPA3, and other encryption types.
- Hardware platform: all WiFi-capable development boards
WiFiScanDualAntennaโ
WiFi dual-antenna scanning example. It uses WiFi.setDualAntennaConfig() to configure dual-antenna GPIO pins and TX/RX antenna modes, then performs a network scan with dual antennas enabled.
- Requires hardware support for dual-antenna functionality.
- Hardware platform: development boards with dual-antenna support
WiFiTelnetToSerialโ
WiFi Telnet-to-Serial bridge example. It bridges a WiFi Telnet connection with a hardware serial port โ after connecting to the device via a Telnet client, data sent and received through Telnet is transparently forwarded to the serial port, and vice versa.
- Uses
WiFiMultifor multi-AP support, listening on port 23. - Suitable for remote serial debugging scenarios.
- Hardware platform: all WiFi-capable development boards
WiFiUDPClientโ
WiFi UDP communication example. It uses the WiFiUDP class for UDP data exchange โ listening on a specified port, printing the source IP, port, and content when a UDP packet is received, and replying with a confirmation message.
- Suitable for low-latency LAN communication scenarios.
- Hardware platform: all WiFi-capable development boards
Tuya IoT cloud accessโ
The TuyaIoT library provides device access to the Tuya IoT platform. Through the DP (Data Point) model, it enables data interaction between the device, the cloud, and the mobile app, supporting device activation, data reporting, command receiving, weather queries, and more.
quickStartโ
Tuya IoT quick start example. It demonstrates the complete Tuya IoT device development workflow โ configuring the device License, initializing TuyaIoT, handling device binding, cloud connection, and DP command receiving through event callbacks, and achieving two-way control between the phone app and the onboard LED.
- A short button press toggles the LED state and reports it; a long press removes the device binding.
- Uses
TuyaIoT.write()/TuyaIoT.read()for DP data read/write. - Hardware platform: Tuya-compatible development boards (T2, T3, T5AI, ESP32, LN882H, or XH_WB5E series)
- How to create a PID and configure DPs for cloud-device communication
dpTypeโ
Tuya DP data types example. It demonstrates the read/write methods for all DP data types supported by the Tuya IoT platform, including Bool, Enum, Value (integer), String, and Raw (pass-through). It serves as a comprehensive DP development reference.
- Demonstrates
TuyaIoT.read()andTuyaIoT.write()for different data types. - Includes hexadecimal data send/receive examples for the Raw type.
- Hardware platform: Tuya-compatible development boards (T2, T3, T5AI, ESP32, LN882H, or XH_WB5E series)
weatherGetโ
Tuya weather service example. It retrieves weather data via the Tuya Cloud, including current weather conditions (temperature, humidity, apparent temperature, pressure, UV index), wind information, sunrise/sunset times, Air Quality Index (AQI), and a 7-day weather forecast.
- Uses the
TuyaIoTWeatherClassencapsulated weather query interface. - Some interfaces (for example, wind scale and AQI ranking) are supported only in the China mainland data center.
- The device must be activated and time-synced before querying.
- Hardware platform: Tuya-compatible development boards (T2, T3, T5AI, ESP32, LN882H, or XH_WB5E series)
HTTP clientโ
The HTTPClient library provides convenient HTTP/HTTPS request interfaces, encapsulating connection management, request sending, and response parsing.
BasicHttpClientโ
HTTP GET request example. It uses the HTTPClient class to initiate an HTTP GET request, connecting to WiFi and then accessing a specified URL to print the response status code and content.
- Uses
http.begin()to set the URL andhttp.GET()to initiate the request. - Hardware platform: all WiFi-capable development boards
BasicHttpsClientโ
HTTPS GET request example. It uses the HTTPClient class to initiate a secure HTTPS request, configuring the CA root certificate to verify the server identity and enable encrypted communication.
- Requires providing the server's CA certificate for TLS verification.
- Hardware platform: all WiFi-capable development boards
MQTT clientโ
The MQTTClient library provides an MQTT protocol client implementation, supporting message publishing, subscribing, authentication, and more. It is ideal for message communication between IoT devices and the cloud.
mqtt_basicโ
Basic MQTT publish/subscribe example. After connecting to WiFi, it configures the MQTT Broker information, establishes a connection, publishes messages to outTopic, and subscribes to inTopic to receive messages, with a callback function printing the received message content.
- Uses the public test Broker
broker.emqx.io. - Calls
mqtt.loop()inloop()to maintain the connection. - Hardware platform: all WiFi-capable development boards
mqtt_authโ
MQTT authenticated connection example. It connects to an MQTT Broker that requires authentication using a username and password, showing how to configure the username and password fields in mqtt_client_config_t.
- Hardware platform: all WiFi-capable development boards
mqtt_publish_in_callbackโ
Publishing messages within an MQTT callback example. It republishes a message inside the message receive callback โ subscribing to inTopic and forwarding received messages to outTopic, implementing a message relay function.
- Demonstrates how to safely call
mqtt.publish()within a callback function. - Includes automatic reconnection logic on disconnect.
- Hardware platform: all WiFi-capable development boards
DNS serverโ
The DNSServer library provides lightweight DNS server functionality, commonly used in Captive Portal scenarios.
CaptivePortalโ
Captive Portal example. It creates a WiFi hotspot and starts a DNS server that redirects all domain name resolution requests to the device's own IP address. Combined with an HTTP server, it implements a captive portal page โ users see a configuration page automatically upon connecting to the hotspot.
- Uses
WiFi.softAP()to create the hotspot,DNSServerto hijack DNS, andWiFiServerto serve the HTTP page. - Suitable for device provisioning, local configuration, and similar scenarios.
- Hardware platform: all WiFi-capable development boards