Welcome to RDK X5 Board repository
6. AI Script Peripheral Control
OpenClaw AI Script Writing for Peripheral Control Tutorial
Before starting, please complete [01-Peripheral Hardware Configuration], [02-OpenClaw API-KEY Configuration], [03-OpenClaw Model Switching], [04-OpenClaw Prompts], [05-AI Voice Interaction Configuration], and [06-Three Conversation Scheme Configuration and Testing] in the
Preparation for Usedirectory. If you are currently only using TUI / WhatsApp, you can skip05for now. This document assumes the basic environment is already prepared and only expands on the "Let AI generate independently runnable scripts" part.
1. Tutorial Objectives
This tutorial demonstrates through the method of "natural language generating hardware scripts" that OpenClaw is not only capable of directly controlling peripherals but can also help you save control logic into script files. After completing this tutorial, readers will be able to:
- Understand the basic idea of OpenClaw generating hardware control scripts
- Interact with OpenClaw via three methods: WhatsApp, TUI, and voice code
- Generate four common types of scripts: RGB, Servo, Temperature/Humidity, OLED
2. Hardware Preparation
- OpenClaw main board
- Servo / RGB light strip / OLED / DHT temperature and humidity sensor
[Optional] AI voice module + speaker
Description:
- This tutorial does not require all peripherals to be connected at once
- Connect the corresponding hardware first for the type of script you plan to demonstrate
3. Software Preparation
Code entry points used in the tutorial
- Script generation skill:
/home/sunrise/.openclaw/workspace/skills/script-runner/SKILL.md - Default script save directory:
/home/sunrise/Desktop/ - Hardware command example directory:
/home/sunrise/hardware_hal_demo/ - Voice entry point:
/home/sunrise/voice_to_openclaw/src/main.py
The core of this capability is not "executing an instant command," but letting OpenClaw help you generate an independently runnable Python script. According to existing skill conventions:
- When users say "generate a script," "write a script," or "save to desktop," it will prioritize using
script-runner - Hardware scripts default to using
openclaw_lib - Default serial port is
/dev/ttyS1 - Default baud rate is
115200 - Scripts are usually saved in
/home/sunrise/Desktop/
You can understand it as two usage methods:
- Method 1: Generate a script and run it immediately
- Method 2: Only generate the script, save it to the desktop first, and modify or re-run it later
4. Testing Script Generation Capability
Before starting this tutorial, please complete a basic openclaw tui conversation self-check first; if you haven't done so, see [05-Three Conversation Scheme Configuration and Testing]. After confirming that OpenClaw can respond normally, proceed to the script generation specific test.
Enter the following command in the terminal to enter TUI:
openclaw tui
It is recommended to start with the simplest script requirements, for example:
- Help me write a script to make the RGB red light flash 5 times, save to desktop and run
- Generate a script to read temperature and humidity, do not run it yet
- Write a script to make servo 1 swing back and forth between 0, 90, and 180 degrees
- Generate a script to display "Hello OpenClaw" on the OLED
5. Three Schemes for Conversing with OpenClaw
| Scheme | Suitable Scenarios | Advantages | Suggested Positioning |
|---|---|---|---|
| Remote requirement submission, classroom demonstration | Can directly describe "help me write what script" | Used to demonstrate natural language generation capability | |
| TUI | Local debugging, verifying generation results | Easiest to see script generation and execution return | Preferred debugging scheme |
| Voice Code | Voice interaction, complete AI experience | Can directly orally describe script requirements | As a complete demonstration scheme |
If you have selected the AI voice module + speaker, Feishu dialogue, tui, and WhatsApp dialogue need to broadcast the content of openclaw's reply. You can run the following program on the terminal. The AI voice module dialogue does not need to be run repeatedly
- Modify whether to enable broadcast parameter
xxxxxxxxxx/home/sunrise/voice_to_openclaw/.env
xxxxxxxxxxENABLE_TTS=true # This parameter controls whether to broadcast. After modifying and saving, run the following program in the terminal.- Enter in terminal:
xxxxxxxxxxcd ~/voice_to_openclawsource .venv/bin/activatepython -m src.openclaw_session_tts
5.1 WhatsApp Scheme
For WhatsApp access steps, please refer directly to [06-Three Conversation Scheme Configuration and Testing]. This section only retains conversation examples suitable for the script generation tutorial.
You can converse directly, for example:
- Generate a script for all servos to swing left and right for 5 seconds and run it

5.2 TUI Scheme
If you have completed the TUI self-check in Part 6, you can continue the conversation by entering the following command in the terminal:
xxxxxxxxxxopenclaw tuiThen you can directly input script requirements, for example:
Help me write a script to control RGB to become a blue breathing light and run for 10 seconds

5.3 Voice Code Scheme
xxxxxxxxxxRequires selection of AI voice interaction module + speaker
For voice module installation, environment configuration, and basic wake-up process, please refer directly to [06-AI Voice Interaction Configuration]. After wake-up, you can directly speak control content, for example:
- Write a script to display current temperature and humidity on the screen and run it

6. Comprehensive Cases
Note: The following are conversation demonstrations conducted via openclaw tui. You can also choose WhatsApp or voice methods to complete them.
6.1 Case 1: RGB Flashing Script
Experiment Objective
Let OpenClaw generate a basic lighting script to verify the "script creation + peripheral execution" pipeline.
Effect Description
OpenClaw will organize your natural language requirements into a Python script. After the script is saved to the desktop, it can be run directly or kept for subsequent modification.
Example Instructions
- Help me write a script to make the RGB red light flash 5 times
- Generate a blue breathing light script, save to desktop and run
- Write a script to make the RGB perform a rainbow light effect
6.2 Case 2: Servo Action Script
Experiment Objective
Generate a servo action script to solidify single-dialogue actions into a repeatedly runnable program.
Effect Description
This type of script is suitable for actions like "nodding," "shaking head," "swinging back and forth," or "returning to center," and is also suitable for subsequent expansion into classroom demonstrations or combination projects.
Example Instructions
- Write a script to make servo 1 move back and forth between 0, 90, and 180 degrees
- Generate a servo nodding script
- Help me write a script to make all four servos return to 90 degrees together
6.3 Case 3: Temperature and Humidity Reading Script
Experiment Objective
Generate a script to read current temperature and humidity and output the results.
Effect Description
This type of script is suitable for sensor self-checks, data sampling, or subsequent log recording, and is also one of the most common types of basic scripts for making scheduled tasks later.
Example Instructions
- Generate a script to read temperature and humidity and save it to the desktop
- Help me write a script to print current temperature and humidity
- Write a script to read DHT data once and run it
6.4 Case 4: OLED Display Script
Experiment Objective
Generate a script to display text or metric content on the OLED.
Effect Description
This case is suitable for making welcome pages, status pages, or simple dashboards, and is also the basis for subsequent linkage with capabilities such as temperature/humidity and cameras.
Example Instructions
- Help me write a script to display "Hello OpenClaw" on the OLED
- Generate a script to display temperature and humidity on the OLED
- Write a script to display a page of simple status information
6.5 Case 5: Combination Script
Experiment Objective
Let OpenClaw generate a comprehensive script containing multiple peripheral actions at once.
Effect Description
This type of script can string together "reading sensors," "displaying on OLED," "controlling RGB," and "driving servos," suitable for transitioning from single-peripheral control to small project scripts.
Example Instructions
- Write a comprehensive script: read temperature and humidity then display on OLED
- Generate a script: first light up green light, then make the servo turn to 90 degrees
- Help me write a script: light up red light and prompt on OLED if temperature is too high