-
Notifications
You must be signed in to change notification settings - Fork 0
Board API
The Board class manages a serial connection to an Arduino running StandardFirmata. It serves double duty:
- Motor users — create a Board, then attach motors to it.
- Servo users — create a Board (the last one created becomes the "active" board), then Servo will find it automatically.
The Board supports two communication backends. Default is pyfirmata2; switch to pyserial for direct Firmata protocol over serial.
from megawrapper import set_mode, get_mode, Board
set_mode("pyserial") # switch to raw-serial Firmata
board = Board("/dev/ttyUSB0")set_mode("pyfirmata2") # switch back (default)| Mode | Dependency | When to use |
|---|---|---|
pyfirmata2 |
pyfirmata2 | Default. Full Firmata feature set. |
pyserial |
pyserial | Minimal Firmata. No pyfirmata2 needed. |
set_mode() affects all subsequent Board() calls. Call it once at the top of your script.
Raises: ValueError if mode is not "pyfirmata2" or "pyserial".
Board(port: str, stby: int | None = None)| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
port |
str |
Yes | — | Serial port of the Arduino. |
stby |
int |
No | None |
Digital pin for TB6612FNG standby (optional). |
Raises: BoardConnectionError if the Arduino cannot be reached.
Example:
# Minimal
board = Board("/dev/ttyUSB0")
# With standby pin for TB6612FNG motor driver
board = Board("/dev/ttyUSB0", stby=6)When a stby pin is provided, the driver is woken automatically (pin set HIGH) during construction.
@staticmethod
Board.get_active_board() -> BoardReturns the most recently created Board instance. This is how Servo finds the board.
Raises: RuntimeError if no Board has been created yet.
board = Board("/dev/ttyUSB0")
assert Board.get_active_board() is board # TrueBoard.attach_motor(
direction1: int,
direction2: int,
pwm: int,
name: str | None = None,
) -> MotorCreates a Motor and registers it in board.motors.
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
direction1 |
int |
Yes | — | First direction-control digital pin. |
direction2 |
int |
Yes | — | Second direction-control digital pin. |
pwm |
int |
Yes | — | PWM-capable pin for speed. |
name |
str |
No | None |
Optional label (e.g. "left"). |
Returns: The newly created Motor instance.
motor = board.attach_motor(2, 4, 11, name="drive")Board.stop_all() -> NoneCalls motor.stop() on every motor in board.motors.
board.stop_all() # every motor coasts to a stopBoard.wake() -> None # enable motor driver (STBY → HIGH)
Board.sleep() -> None # disable motor driver (STBY → LOW)Only available when stby was provided to the constructor.
Raises: StandbyNotConfiguredError if no STBY pin was set.
board = Board("/dev/ttyUSB0", stby=6)
board.sleep() # motors can't move
board.wake() # motors can move againBoard.close() -> None- Stops all motors.
- Asserts STBY LOW (if configured).
- Calls
exit()on the underlying pyfirmata2 connection.
Always call this when done, or use the context manager.
with Board("/dev/ttyUSB0") as board:
motor = board.attach_motor(2, 4, 11)
motor.forward(100)
# board.close() called automatically on exitA list[Motor] of every motor created via attach_motor() on this board. Updated automatically.
board = Board("/dev/ttyUSB0")
m1 = board.attach_motor(2, 4, 11)
m2 = board.attach_motor(5, 3, 10)
print(board.motors) # [m1, m2]