This guide explains how to build MicroPython firmware for the Raspberry Pi Pico with your custom AIDriver libraries integrated. The build system automatically creates firmware with your libraries frozen into the firmware for fast loading, while main.py is placed on the filesystem for IDE editing with hardware recovery capability.
- Codespaces or a local devcontainer (already set up in this repo)
- Internet connection (optional - script works offline)
- Raspberry Pi Pico board
One-Command Build:
cd /workspaces/AIDriver_MicroPython_Challanges/.devcontainer
./build_firmware.shThe script automatically:
- ✅ Validates the build environment
- ✅ Updates MicroPython to the latest version (if internet available)
- ✅ Copies custom libraries to frozen modules (fast loading)
- ✅ Embeds
main.pyin firmware for filesystem creation - ✅ Validates Python syntax
- ✅ Builds firmware with parallel compilation
- ✅ Saves to
_Firmware/AI_Driver_RP2040.uf2 - ✅ Creates build metadata for tracking
project/lib/aidriver.py→ Frozen into firmwareproject/lib/gamepad_driver_controller.py→ Frozen into firmwareproject/lib/gamepad_pico.py→ Frozen into firmware
Benefits: Instant loading, memory efficient, cannot be accidentally corrupted
project/main.py→ Created on device filesystem during first boot
Benefits: Visible in IDE, editable, preserves user changes across reboots
ports/rp2/main.c→ one line added so the frozen_aidriver_stop.pyruns every timemain.pyends (finished, crashed or Ctrl-C), braking the motors. The patch is applied to a fresh copy of the stock file on every build (patch_main_cincommon_functions.sh); if a MicroPython update changes the line that runsmain.py, the build stops with an error instead of producing firmware without it.
Pin 4 Hardware Recovery: If your main.py becomes corrupted or problematic, recovery resets the robot's filesystem to a clean state.
Warning
Recovery deletes every file and folder on the robot except main.py, event_log.txt and event_log_prev.txt, and then resets those too. Copy any code you want to keep before using it.
- Power off the Pico
- Connect GPIO pin 4 to ground (use jumper wire to any GND pin)
- Power on or reset the Pico
- Recovery activates → every other file and folder is deleted,
main.pyis restored to default content and the event logs are cleared - Remove jumper from pin 4
- Reset → Normal operation resumes with fresh
main.py
Only the filesystem the student can see is wiped. The libraries frozen into the firmware (aidriver, eventlog, hm10_controller, etc.) are not files and cannot be deleted. The wipe removes, among other things:
boot.py- runs beforemain.py, so a broken one would stop the default code running- Root-level copies of the frozen libraries (
aidriver.py,eventlog.mpy, ...) - MicroPython searches/before the frozen modules, so these would replace the built-in versions /liband any other folders, student modules, and*.bak/*.partfiles left by uploads
Console output during recovery:
RECOVERY MODE: Pin 4 detected grounded - wiping filesystem and restoring main.py
RECOVERY MODE: deleted aidriver.py
RECOVERY MODE: deleted lib
RECOVERY MODE: 4 file(s) deleted
Recovery complete: main.py restored to default content
./build_firmware.sh --skip-updateUse this for iterative development when you don't need the latest MicroPython.
./build_firmware.sh --force-cleanUse this if you encounter build issues or want to ensure a completely fresh build.
./prepare_modules.shJust copies modules without building firmware. Useful for testing module syntax.
./build_firmware.sh --help
./prepare_modules.sh --help- Connect your Raspberry Pi Pico to your computer while holding the BOOTSEL button
- Pico mounts as USB drive (RPI-RP2)
- Copy firmware file:
# From: /workspaces/AIDriver_MicroPython_Challanges/_Firmware/AI_Driver_RP2040.uf2 # To: RPI-RP2 USB drive
- Pico reboots automatically and runs your integrated code
- First boot:
main.pyautomatically created on filesystem with your test code - Subsequent boots: Existing
main.pypreserved (user edits maintained) - IDE connection:
main.pyvisible and editable in Thonny, VS Code, etc. - Library access:
from aidriver import AIDriverworks immediately (frozen modules)
The firmware's version is __version__ in project/lib/aidriver.py (a date such as 2026.11.26). The build records it in _Firmware/build_info.txt as AIDriver Version.
When a student connects a robot in the simulator, the simulator checks the version of the built-in library (ignoring any aidriver.py copy saved on the robot). If the AIDriver firmware is missing, or older than RobotConnection.FIRMWARE_VERSION in app/js/robot-connection.js, the student is told to give the robot to their teacher. The status shows Firmware update needed, and the debug panel explains how to flash it (the steps above). Uploading still works.
To release firmware that every robot must be updated to:
- Set the new date in
__version__inproject/lib/aidriver.py. - Set the same value in
FIRMWARE_VERSIONinapp/js/robot-connection.js. - Rebuild (
./.devcontainer/build_firmware.sh) and commit_Firmware/.
The app tests fail if these three don't match. Robots with newer firmware than the simulator expects are accepted.
- ✅ Terminal shows
✅ AIDriver firmware build complete! - ✅
AI_Driver_RP2040.uf2file exists in_Firmware/folder (~630-650KB) - ✅ No error messages in terminal output
- ✅ Build reports module counts: "Copied X modules to frozen modules directory"
Permission Error:
chmod +x /workspaces/AIDriver_MicroPython_Challanges/.devcontainer/*.shFiles Not Found:
- Verify files exist:
project/lib/*.pyandproject/main.py - Check exact filenames:
aidriver.py,gamepad_driver_controller.py,gamepad_pico.py
Syntax Errors:
- Script automatically validates Python syntax
- Fix reported syntax errors in your Python files
Build Fails:
./build_firmware.sh --force-clean # Fresh buildNo Internet Connection:
- Script automatically detects and continues with current MicroPython version
- Use
--skip-updateto explicitly skip updates
Environment Issues:
- Script validates environment and shows specific error messages
- Restart devcontainer if needed
Pin 4 Recovery Not Working:
- Ensure GPIO pin 4 is connected to ground (GND)
- Try different GND pins if available
- Verify jumper wire connection during boot
- Edit code in
project/folder - Build firmware:
./build_firmware.sh --skip-update - Flash to Pico (copy .uf2 file)
- Test on device
- Repeat
- Flash firmware with your code
- Edit main.py on device (introduce error)
- Connect pin 4 to ground and reset
- Verify recovery restores original code
If you need to customize the build or prefer manual control:
# Copy modules manually
cp /workspaces/AIDriver_MicroPython_Challanges/project/lib/*.py /micropython/ports/rp2/modules/
# Build manually
cd /micropython/ports/rp2
make submodules
make clean
make -j$(nproc)
# Copy firmware
cp build-RPI_PICO/firmware.uf2 /workspaces/AIDriver_MicroPython_Challanges/_Firmware/AI_Driver_RP2040.uf2Note: Manual process doesn't include the pin 4 recovery feature or filesystem main.py handling.
Default: Raspberry Pi Pico
./build_firmware.sh # Builds for standard PicoPico W (WiFi):
cd /micropython/ports/rp2
make BOARD=PICO_W # Manual build for Pico WPico 2:
cd /micropython/ports/rp2
make BOARD=PICO2 # Manual build for Pico 2- MicroPython RP2 Port Documentation
- Raspberry Pi Pico Documentation
- MicroPython Build Troubleshooting
If you need help:
- Check error messages in terminal output
- Try
./build_firmware.sh --force-clean - Verify file structure and syntax
- Ask your instructor
- Open an issue in this repository
Happy Building! 🚀