A comprehensive collection of MicroPython libraries and tools for building advanced LEGO robots. Designed for LEGO SPIKE Prime, MINDSTORMS Robot Inventor, and ESP32-based robotics projects.
Create synchronized motor movements, advanced animations, remote control systems, and more with this powerful robotics toolkit. For in-depth articles and tutorials, visit antonsmindstorms.com.
- Features
- Installation
- Quick Start
- Core Libraries
- Example Projects
- Documentation
- Supported Platforms
- Contributing
- License
- Author
- ๐ฎ Remote Control: BLE RC transmitter/receiver with gamepad-like controls via btbricks
- โ๏ธ Motor Synchronization: Advanced motor coordination with animation curves and timing functions
- ๐จ LED Animations: Display utilities for hub LEDs and 5x5 matrix displays
- ๐ SerialTalk: Symmetrical UART/BLE/WebSocket communication for multi-hub projects
- ๐ค Pybricks-like API: Familiar interface for SPIKE/MINDSTORMS hubs with SI units
- ๐ ๏ธ Helper Utilities: Scaling, clamping, interpolation, and timing functions
- ๐ก Multiple Communication Protocols: BLE UART, LEGO Protocol, SerialTalk, and more
- Copy the code from the install script (use the Copy raw contents button)
- Paste into an empty Python project in the LEGO SPIKE/MINDSTORMS app
- Run the script once (installation takes ~1 minute)
- IMPORTANT: Watch the console output and unplug the hub when prompted before restart completes
Note: If the app requests a firmware update, you can safely ignore it. Accepting the update will require reinstallation. To exit the update prompt, long-press the center button and power cycle the hub.
To uninstall, remove the /projects/mpy_robot_tools/ directory using a script or file manager (like Thonny IDE).
For a complete factory reset: In the LEGO app, click Hub Connection Icon โ More (ยทยทยท) โ Reset Settings โ OK.
from projects.mpy_robot_tools.motor_sync import Mechanism, linear_interpolation
from projects.mpy_robot_tools.pybricks import Motor, Port
# Define motors
left = Motor(Port.A)
right = Motor(Port.B)
# Create animation with keyframes: (time_ms, angle_degrees)
movement = linear_interpolation([
(0, 0),
(1000, 180),
(2000, 0)
])
# Run synchronized mechanism
mech = Mechanism([left, right], [movement, movement])
mech.start_loop(duration=2000)from projects.mpy_robot_tools.rc import RCReceiver, R_STICK_VER, L_STICK_HOR
from projects.mpy_robot_tools.pybricks import Motor, Port
# Setup motors and RC receiver
left_motor = Motor(Port.A)
right_motor = Motor(Port.B)
rcv = RCReceiver(name="myrobot")
print("Waiting for RC connection...")
while True:
if rcv.is_connected():
# Get joystick values (-100 to 100)
throttle = rcv.controller_state(R_STICK_VER)
steering = rcv.controller_state(L_STICK_HOR)
# Tank drive control
left_motor.dc(throttle + steering)
right_motor.dc(throttle - steering)from projects.mpy_robot_tools.light import image_99
from hub import display
# Display numbers up to 99 on the 5x5 matrix
display.show(image_99(42))from projects.mpy_robot_tools.serialtalk import SerialTalk
from hub import port
# Initialize SerialTalk on a UART port
talk = SerialTalk(port.A)
# Define a callable function
@talk.call
def set_motor_speed(speed):
print(f"Setting motor to {speed}")
# Your motor control code here
# Main loop - handle incoming commands
while True:
talk.process() # Non-blocking command processorAdvanced motor synchronization and animation system.
Mechanism(motors, time_functions)- Synchronizes multiple motors using time-based functionslinear_interpolation(keyframes)- Creates smooth motion between keyframessine_wave(amplitude, period)- Generates sinusoidal motionAMHTimer()- High-precision millisecond timer
Pybricks-compatible API for SPIKE Prime and MINDSTORMS Robot Inventor.
Motor(port)- Motor control with encoders, SI units, and async supportPort- Port definitions (A, B, C, D, E, F)wait(ms)- Millisecond-precision wait functionUltrasonicSensor(port)- Distance sensing in mm/cm
Bluetooth Low Energy communication library. See btbricks documentation for full API.
UARTCentral()/UARTPeripheral()- Nordic UART Service for hub-to-hub communicationRCReceiver()/RCTransmitter()- Remote control with gamepad interfaceMidiController()- Send MIDI over BLEBtHub()- Control LEGO hubs via Bluetooth
Symmetrical communication protocol for UART, BLE, and WebSocket connections.
SerialTalk(port)- Remote procedure calls between devices@talk.call- Decorator to expose functions for remote calling- Supports UART, Bluetooth, and WebSocket transports
- Non-blocking command processing loop
LED display utilities for SPIKE/MINDSTORMS hubs.
image_99(number)- Display 0-99 on 5x5 LED matrixCONNECT_IMAGES- Animation frames for connection indicators- Custom image utilities and display helpers
General utility functions.
clamp_int(n, floor, ceiling)- Limit values to a rangescale(val, src_range, dst_range)- Map values between rangeswait_until(func, condition)- Conditional waiting
For HuskyLens AI vision sensor integration, see the dedicated PyHuskyLens library which includes a SPIKE Prime/Robot Inventor installer script.
The Example projects/ directory contains fully working examples:
rc_hotrod_car_receiver.py- RC car with BLE remote controlrc_snake.py- Multi-segment robot snake with synchronized movementble_uart_simple_central.py/ble_uart_simple_peripheral.py- Hub-to-hub communicationbluepad_mecanum_wheels.py- Mecanum drive with gamepad controlinventor_ble_midi_guitar.py- MIDI instrument using hub sensors
Browse the Example projects/ folder for more inspiration!
Comprehensive documentation is available at:
All classes and functions include detailed docstrings. Use help() in the REPL:
from projects.mpy_robot_tools.motor_sync import Mechanism
help(Mechanism)Visit antonsmindstorms.com for:
- Step-by-step tutorials
- Advanced techniques
- Building instructions
- Video demonstrations
- LEGO SPIKE Prime (all versions)
- LEGO MINDSTORMS Robot Inventor (51515)
- LEGO SPIKE Essential (limited support)
- ESP32 with MicroPython (via LMS-ESP32 board)
- Other MicroPython boards with compatible peripherals
Contributions are welcome! Help improve this project by:
- ๐ Adding documentation, docstrings, or tutorials
- ๐ Reporting bugs or issues
- โจ Submitting new features or examples
- ๐ง Improving existing code
Please fork the repository and submit pull requests.
This project is licensed under the GNU General Public License v3.0.
See LICENSE.md for full details.
Anton Vanhoucke
- Website: antonsmindstorms.com
- GitHub: @antonvh