Peripheral trait defines the interface for hardware peripherals in ZeroClaw. Implement this trait to integrate physical devices like microcontrollers (STM32, Arduino), GPIO boards (Raspberry Pi), sensors, and actuators as agent-controllable capabilities.
Overview
Peripherals are the agent’s “arms and legs” - remote devices that run minimal firmware and expose hardware capabilities as tools. The agent connects to peripherals, retrieves their tool definitions, and can invoke hardware functions just like software tools.Trait Definition
Required Methods
name
Return the human-readable instance name of this peripheral.
&str
Unique instance identifier including index or serial (e.g., “nucleo-f401re-0”, “rpi-gpio-hat-1”)
board_type
Return the board type identifier.
&str
Stable lowercase identifier (e.g., “nucleo-f401re”, “rpi-gpio”)
connect
Establish a connection to the peripheral hardware.
anyhow::Result<()>
Success or error
- Open underlying transport (serial port, GPIO bus, I²C, etc.)
- Perform initialization handshake with firmware
- Verify device is ready for commands
- Open
/dev/ttyACM0at 115200 baud for STM32 - Export GPIO pins via sysfs for Raspberry Pi
- Perform version check handshake with firmware
disconnect
Disconnect from the peripheral and release all resources.
anyhow::Result<()>
Success or error
- Close serial ports
- Unexport GPIO pins
- Perform safe shutdown sequence
- Release any held locks or file descriptors
health_check() should return false until connect() is called again.
health_check
Check whether the peripheral is reachable and responsive.
bool
true if device responds within timeouttools
Return the tools this peripheral exposes to the agent.
Vec<Box<dyn Tool>>
Array of tool implementations that delegate to hardware
- Implements the
Tooltrait - Delegates execution to underlying hardware (GPIO, sensors, actuators)
- Handles communication protocol with firmware
- Returns structured results to the agent
gpio_read- Read digital pin stategpio_write- Set digital pin stateadc_read- Read analog sensor valuepwm_set- Control PWM outputsensor_read- Read temperature/humidity/etc.
Implementation Example
Here’s a simplified STM32 Nucleo peripheral:Communication Protocol
Typical firmware communication pattern:docs/hardware-peripherals-design.md for complete protocol specification.
Factory Registration
Register your peripheral in the factory:Configuration Example
Lifecycle Flow
- Startup: Agent reads peripheral config
- Factory: Creates peripheral instances via factory
- Connect: Agent calls
connect()on each peripheral - Tool Registration: Agent calls
tools()and merges into tool registry - Runtime: LLM can invoke peripheral tools like any other tool
- Health Monitoring: Periodic
health_check()calls - Shutdown: Agent calls
disconnect()on cleanup
Best Practices
Timeouts: Use reasonable timeouts for serial/I²C operations (typically 100-1000ms). Hardware can be slow.
Thread Safety: Use
Arc<Mutex<>> for shared resources like serial ports since tools may be called concurrently.Supported Board Types
Current implementations:- nucleo-f401re - STM32 Nucleo F401RE over USB serial
- rpi-gpio - Raspberry Pi GPIO via sysfs
Firmware Development
Seedocs/hardware-peripherals-design.md for:
- Communication protocol specification
- Firmware implementation guide
- Example firmware for STM32 (C/C++)
- Testing and debugging procedures
Testing
Related
- Tool Trait - Peripheral capabilities are exposed as tools
- Hardware Peripherals Design - Complete protocol specification
- Configuration Reference - Configure peripherals in config.toml