← Back to Deep Dive

Software & Firmware

Overview

The software stack consists of three components: Arduino Nano firmware (C++) for EMG acquisition, Raspberry Pi Zero Python control script, and Raspberry Pi Pico firmware (MicroPython) for servo PWM generation.

Communication between components uses UART (Arduino ↔ Pi Zero) and I²C (Pi Zero ↔ Pico). All code is open-source and available on GitHub.

Architecture

Arduino Nano Firmware

Written in C++ using Arduino framework. Samples EMG envelope from analog pin A0 at 500 Hz, applies simple moving average filter, and transmits values via serial at 115200 baud.

  • ADC resolution: 10-bit (0–1023)
  • Transmission format: ASCII decimal, newline-terminated
  • Flash usage: ~8 KB / 30 KB available

Raspberry Pi Zero 2 W Control Script

Python 3.9+ script handles calibration, threshold detection, proportional control, and I²C communication to Pico. Uses pyserialfor UART and smbus2 for I²C.

  • Dependencies: pyserial, smbus2, numpy (optional for advanced filtering)
  • Config file: JSON format storing calibration parameters
  • Main loop: 100 Hz using asyncio for non-blocking I/O

Raspberry Pi Pico Firmware

MicroPython firmware acts as I²C slave, receiving servo position commands and generating 50 Hz PWM signals for four MG996R servos. Uses hardware PWM channels for precise timing.

  • I²C address: 0x42 (configurable)
  • Command format: 4-byte array, one byte per servo (0–180 degrees)
  • PWM frequency: 50 Hz, pulse width 1000–2000 µs

Communication Protocol

# Arduino → Pi Zero (UART, 115200 baud)
# Format: "<ADC_VALUE>\n"
# Example: "512\n"

# Pi Zero → Pico (I²C, address 0x42)
# Write 4 bytes: [servo0_angle, servo1_angle, servo2_angle, servo3_angle]
# Each byte: 0–180 (servo angle in degrees)
# Example: [90, 90, 90, 90]  # All servos to 90°

Serial protocol is human-readable for debugging. I²C uses compact binary format for minimal latency.

Installation & Usage

Arduino Nano

1. Open silverhand_emg.ino in Arduino IDE
2. Select Board: Arduino Nano, Processor: ATmega328P
3. Upload to Arduino Nano

Raspberry Pi Pico

1. Flash MicroPython to Pico (uf2 file)
2. Copy pico_servo_controller.py to Pico as main.py
3. Reset Pico (will auto-start on boot)

Raspberry Pi Zero 2 W

pip install pyserial smbus2
python silverhand_control.py --calibrate  # Run calibration
python silverhand_control.py              # Normal operation

Downloadable Files

Complete Firmware Package

Arduino, Pico, and Pi Zero source code

Download

GitHub Repository

Full source with git history and issues

View on GitHub →