Welcome to Raspberry Pi 5 repository
6. AI Script Peripheral Control
OpenClaw AI Script Peripheral Control Tutorial
Before starting, please complete [01-Peripheral Hardware Configuration], [02-OpenClaw API-KEY Configuration], [03-OpenClaw Model Switching], [04-AI Voice Interaction Configuration], and [05-Three Dialogue Configuration Tests] in the
Preparation Before Usedirectory. If you are only using TUI/WhatsApp, you can skip04. This article assumes the basic environment is ready and only covers the "having AI generate independently runnable scripts" part.
1. Tutorial Objectives
This article demonstrates that OpenClaw can not only directly control peripherals but also help you save control logic into script files through the approach of "natural language generation of hardware scripts". After completing this tutorial, readers will be able to:
- Understand the basic approach to generating hardware control scripts with OpenClaw
- Communicate with OpenClaw via WhatsApp, TUI, and voice commands
- Generate four types of common scripts: RGB, servo, temperature and humidity, OLED
2. Hardware Preparation
- OpenClaw main control board
- Servo / RGB light strip / OLED / DHT temperature and humidity sensor
[Optional] AI voice module + speaker
Note:
- This tutorial does not require connecting all peripherals at once
- If you want to demonstrate which type of script, just connect the corresponding hardware first
3. Software Preparation
Code entry points used in this tutorial:
- Script generation skill:
/home/pi/.openclaw/workspace/skills/script-runner/SKILL.md - Script default save directory:
/home/pi/Desktop/ - Hardware command example directory:
/home/pi/hardware_hal_demo/ - Voice entry:
/home/pi/voice_to_openclaw/src/main.py
The core of this capability is not "execute an immediate command" but having OpenClaw help you generate an independently runnable Python script. According to existing skill conventions:
- When users say "generate script", "write a script", "save to desktop", it will preferentially use
script-runner - Hardware scripts use
openclaw_libby default - Default serial port is
/dev/ttyAMA0 - Default baud rate is
115200 - Scripts are usually saved to
/home/pi/Desktop/
You can think of it as two usage methods:
- Method 1: Generate script and run immediately
- Method 2: Only generate script, save to desktop first, then modify or run again later
4. Testing Script Generation Capability
Before starting this tutorial, please complete an openclaw tui basic conversation self-check first; if you haven't done this yet, see [05-Three Dialogue Configuration Tests]. After confirming OpenClaw can respond normally, proceed with the script generation-specific test.
Enter TUI by typing the following in the terminal:
openclaw tui
It is recommended to start with the simplest script requirements, for example:
- Help me write a script that makes RGB red lights flash 5 times, save to desktop and run
- Generate a script that reads temperature and humidity, don't run it yet
- Write a script that makes servo 1 swing back and forth between 0, 90, 180 degrees
- Generate a script that displays "Hello OpenClaw" on OLED
5. Three Ways to Communicate with OpenClaw
| Method | Suitable Scenario | Advantages | Suggested Use |
|---|---|---|---|
| Remote requirement sending, classroom demonstration | Can directly describe "help me write what script" | Used to demonstrate natural language generation capability | |
| TUI | Local debugging, verify generation results | Easiest to see script generation and run returns | First choice for debugging solution |
| Voice Code | Voice interaction, complete AI experience | Can directly verbally describe script requirements | As the complete demonstration solution |
If you have the optional AI voice module + speaker, the TUI and WhatsApp conversations need to broadcast OpenClaw's replies. You can run the following program in the terminal. This is not needed for AI voice module conversations.
- Modify the TTS enable parameter
xxxxxxxxxx/home/pi/voice_to_openclaw/.env
xxxxxxxxxxENABLE_TTS=true # This parameter controls whether to broadcast replies. After saving, run the program below in the terminal.- Terminal input
xxxxxxxxxxcd ~/voice_to_openclawsource .venv/bin/activatepython -m src.openclaw_session_tts
5.1 WhatsApp Solution
For WhatsApp integration steps, please refer directly to [05-Three Dialogue Configuration Tests]. This section only includes dialogue examples suitable for the script generation tutorial.
You can converse directly, for example:
- Generate a script that makes servo swing left and right for 10 seconds and run it


5.2 TUI Solution
If you have already completed the TUI self-check from chapter 0, simply type the following command in the terminal to continue the conversation:
xxxxxxxxxxopenclaw tuiThen you can directly input script requirements, for example:
- Help me write a script that controls RGB to become blue breathing light and run it

5.3 Voice Code Solution
xxxxxxxxxxRequires optional AI voice interaction module + speaker
For voice module installation, environment configuration, and basic wake-up flow, please refer directly to [04-AI Voice Interaction Configuration]. After waking up, you can directly speak control content, for example:
- Write a script that displays current temperature and humidity on the screen and run it

6. Comprehensive Cases
Note: All demonstrations below use the openclaw tui method for conversation. You can also choose WhatsApp or voice to complete.
6.1 Case 1: RGB Flashing Script
Experiment Objective
Have OpenClaw generate a basic lighting script to verify the "script creation + peripheral execution" chain.
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 later modification.
Example Commands
- Help me write a script that makes RGB red lights flash 5 times
- Generate a blue breathing light script, save to desktop and run
- Write a script that makes RGB do a rainbow light effect
6.2 Case 2: Servo Action Script
Experiment Objective
Generate a servo action script to solidify single conversation actions into repeatable programs.
Effect Description
This type of script is suitable for actions like "nodding", "head shaking", "swinging back and forth", "returning to center", and is also suitable for later expansion into classroom demonstrations or combined projects.
Example Commands
- Write a script that makes servo 1 move back and forth between 0, 90, 180 degrees
- Generate a servo nodding script
- Help me write a script that makes four servos return to 90 degrees together
6.3 Case 3: Temperature and Humidity Reading Script
Experiment Objective
Generate a script that reads current temperature and humidity and outputs results.
Effect Description
This type of script is suitable for sensor self-check, data sampling, or subsequent log recording, and is also the most commonly used type of basic script when doing scheduled tasks later.
Example Commands
- Generate a script that reads temperature and humidity, save to desktop
- Help me write a script that prints current temperature and humidity
- Write a script that reads DHT data once and runs it
6.4 Case 4: OLED Display Script
Experiment Objective
Generate a script that displays text or metric content on OLED.
Effect Description
This case is suitable for welcome pages, status pages, simple dashboards, and is also the foundation for later linking with capabilities like temperature and humidity and cameras.
Example Commands
- Help me write a script that displays "Hello OpenClaw" on OLED
- Generate a script that displays temperature and humidity on OLED
- Write a script that displays a page of simple status information
6.5 Case 5: Combined Script
Experiment Objective
Have OpenClaw generate a comprehensive script containing multiple peripheral actions at once.
Effect Description
This type of script can chain "read sensor", "display on OLED", "control RGB", "drive servo", suitable for transitioning from single peripheral control to small project scripts.
Example Commands
- Write a comprehensive script: read temperature and humidity and display on OLED
- Generate a script: first light up green light, then make servo rotate to 90 degrees
- Help me write a script: if temperature is too high, light up red light and prompt on OLED