Skip to content

Board API

vihaanvp edited this page Jul 3, 2026 · 4 revisions

Board API

The Board class manages a serial connection to an Arduino running StandardFirmata. It serves double duty:

  1. Motor users — create a Board, then attach motors to it.
  2. Servo users — create a Board (the last one created becomes the "active" board), then Servo will find it automatically.

Mode Selection

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".


Constructor

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.


Board.get_active_board()

@staticmethod
Board.get_active_board() -> Board

Returns 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  # True

Board.attach_motor()

Board.attach_motor(
    direction1: int,
    direction2: int,
    pwm: int,
    name: str | None = None,
) -> Motor

Creates 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()

Board.stop_all() -> None

Calls motor.stop() on every motor in board.motors.

board.stop_all()  # every motor coasts to a stop

Board.wake() / Board.sleep()

Board.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 again

Board.close()

Board.close() -> None
  1. Stops all motors.
  2. Asserts STBY LOW (if configured).
  3. Calls exit() on the underlying pyfirmata2 connection.

Always call this when done, or use the context manager.


Context Manager

with Board("/dev/ttyUSB0") as board:
    motor = board.attach_motor(2, 4, 11)
    motor.forward(100)
# board.close() called automatically on exit

board.motors

A 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]

Clone this wiki locally