PYNQ-Z2 Special Episode: Controlling Hardware with AI Agent (Nanobot)
In this episode, we have a special content. Since the AI Agent trend is currently very strong, especially projects like OpenClaw which have received a lot of attention, but for the embedded systems field, we need lighter options that use fewer resources. Nanobot is an AI Agent inspired by OpenClaw, but designed to use less memory, making it suitable for running on small boards. In this article, we will try running Nanobot on the PYNQ-Z2 board to experiment with controlling the LCD screen (via Framebuffer) that we created in previous episodes, serving as a guideline for applying AI to control hardware at the low level.
Steps of Operation
1. Debian Linux Installation
Start by preparing the Debian operating system for PYNQ-Z2.
Download Debian Image:
Download from: rcn-ee.com (Minimal FS)
In this article, I am using Debian 12.1 (Bookworm)Prepare SD Card:
Use a tool likegpartedto partition as follows:- Partition 1: FAT32 (for Bootloader and Kernel)
- Partition 2: EXT4 (for Root Filesystem)
Copy System Files:
Copy the files obtained from the PetaLinux build to Partition 1:$ sudo mount /dev/sdb1 /mnt/sdb1 $ cd ~/pynq_z2/images/linux/ $ sudo cp BOOT.BIN image.ub /mnt/sdb1/ $ sync $ sudo umount /mnt/sdb1Extract the Debian RootFS files onto Partition 2:
$ tar xJf debian-12.1-minimal-armhf-2023-08-22.tar.xz $ sudo mount /dev/sdb2 /mnt/sdb2 $ sudo tar xfvp debian-12.1-minimal-armhf-2023-08-22/armhf-rootfs-debian-bookworm.tar -C /mnt/sdb2 $ sync $ sudo umount /mnt/sdb2Configure U-Boot Environment:
Insert the SD Card into the PYNQ-Z2 and configure via the Serial Console:setenv bootargs console=ttyPS0,115200 root=/dev/mmcblk0p2 rw rootwait saveenv
2. Framebuffer Management
2.1 Testing Driver Loading
We will load the LCD screen module (ILI9486) that we built previously:
debian@arm:~$ sudo insmod fb-ili9486.ko
fb_ili9486: loading out-of-tree module taints kernel.
fb_ili9486: V2-20251213
Console: switching to mono frame buffer device 40x30
# Test running Demo LVGL
debian@arm:~$ chmod +x demo_lvgl
debian@arm:~$ ./demo_lvgl
[DEBUG] LVGL multi-box speed test start
[DEBUG] Multi-box test running
[LVGL] running... 1772452985
2.2 Configure Auto-load on Startup
To make the driver load automatically every time the board boots, create a Systemd Service:
sudo nano /etc/systemd/system/fbvideo.service
[Unit]
Description=Load fb_ili9486 Driver
After=multi-user.target
[Service]
Type=oneshot
ExecStart=/sbin/insmod /home/debian/fb-ili9486.ko
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
3. Nanobot Installation
Since we are using Debian 12, installing Python and various libraries is easy using apt.
- Install Dependencies:
sudo apt update sudo apt install python3-pip python3-venv python3-full -y - Clone and Install Nanobot:
git clone https://github.com/HKUDS/nanobot.git cd nanobot python3 -m venv venv source venv/bin/activate pip install --upgrade pip pip install -e . - Configure AI Model:
In this example, we will use Gemini as the agent's main brain. Edit the Config file (assuming you runnanobot onboardto generate the initial file first):
nano ~/.nanobot/config.json
"agents": {
"defaults": {
"workspace": "~/.nanobot/workspace",
"model": "gemini/gemini-2.5-flash",
"provider": "gemini",
...
"gemini": {
"apiKey": "xxxxxxxxxxxxxxxxxxxxxxxxx",
"apiBase": null,
"extraHeaders": null
4. Creating a Skill for Nanobot to Control the Framebuffer
The highlight of Nanobot is that we can teach it new skills by creating a SKILL.md file to describe its capabilities to the AI.
- Create a folder for the skill:
mkdir -p ~/.nanobot/workspace/skills/led_control - Create a guide file for the AI (SKILL.md):
~/.nanobot/workspace/skills/led_control/SKILL.md
NOTE
LED Test Skill
This skill allows controlling an LED (simulated via framebuffer) by filling the screen with red or blue.
Actions
turn_on_red_led
Turns on the red LED (fills the screen with red).
default_api.exec(command="python3 fb_test_fill_red.py", working_dir="/home/debian/.nanobot/workspace/skills/led_test")turn_on_blue_led
Turns on the blue LED (fills the screen with blue).
default_api.exec(command="python3 fb_test_fill_blue.py", working_dir="/home/debian/.nanobot/workspace/skills/led_test")test_led
when user want to test led, please use below command
default_api.exec(command="./test_rgb", working_dir="/home/debian/.nanobot/workspace/skills/led_test")
- Write Python Script for Hardware Control:
Example filefb_test_fill_red.pyto fill red color on/dev/fb0:
import os
import struct
import mmap
# Adjust these to match your framebuffer
WIDTH = 480
HEIGHT = 320
BYTES_PER_PIXEL = 2 # 16-bit
# RGB565 red = 0b1111100000000000 = 0xF800
RED = 0xF800
# Open framebuffer
fb = os.open("/dev/fb0", os.O_RDWR)
# Map framebuffer to memory
screen_size = WIDTH * HEIGHT * BYTES_PER_PIXEL
fb_mem = mmap.mmap(fb, screen_size, mmap.MAP_SHARED, mmap.PROT_WRITE | mmap.PROT_READ)
# Create one red pixel in RGB565
red_pixel = struct.pack("
Test Results
Once everything is ready, we can command Nanobot via voice or text, such as "Turn on the red light". The Agent will read SKILL.md and decide to run the Python file to change our LCD screen color immediately!
Example of actual operation