Skip to content

Edge Cases

vihaanvp edited this page Jul 4, 2026 · 3 revisions

Edge Cases & Notes


Board

  • Multiple Board instances: Each creates its own serial connection. The last one created becomes the active board for Servo.
  • get_active_board() before any Board exists: Raises RuntimeError.
  • stby pin: If provided, automatically set HIGH (wake) on creation. Set LOW (sleep) on close().
  • Context manager: Always calls close() even if an exception occurs inside the with block.

Motor

  • Speed range: Integer or float. 0 = stopped, 100 = full. Converted internally to 0.0–1.0 for pyfirmata2 PWM.
  • forward() / backward() with no argument: Defaults to 100 (full speed).
  • set_speed() changes speed without affecting direction pins.
  • stop() vs brake(): stop() lets the motor coast (inertia); brake() shorts the terminals for abrupt halting.
  • Reusing after stop: Just call forward() or backward() again.

Servo

  • Angle clamping: All angles are clamped to [0, 180]. Passing 200 behaves the same as 180. Passing -10 behaves the same as 0.
  • attach() requires a prior Board: Uses Board.get_active_board() internally.
  • read() requires a prior write(): The library tracks the last commanded angle, not the physical angle (servos don't report position).
  • move_smooth() when current_angle is None: Jumps directly to target without stepping.
  • move_smooth() when already at target: No-op (no writes sent).
  • sweep() direction: Automatically determined by start vs end. step must be positive.
  • detach(): Clears all state. Call attach() again to reuse.

VL53L0X Sensor

  • Lazy imports: adafruit_vl53l0x, board, busio, digitalio are imported only when VL53L0X() or auto_address() is called — safe to import megawrapper on any platform.
  • Auto I2C: If no i2c is passed, busio.I2C(board.SCL, board.SDA) is created. Requires Blinka on the host (e.g. Raspberry Pi).
  • Single sensor: Just call VL53L0X() — uses default address 0x29.
  • Multi-sensor limits: auto_address() assigns addresses 0x300x3F → max 16 sensors. The order of xshut_pins determines the order of the returned list.
  • auto_address pin conversion: The xshut_pins parameter accepts integer GPIO numbers (BCM numbering). Internally, each integer is converted to a board pin object via getattr(board, f'D{pin}', pin) before being passed to DigitalInOut. This makes the API convenient ([17, 27, 22]) while satisfying Blinka's pin-object requirement.
  • distance units: Returns centimetres (raw mm ÷ 10).
  • Independent of Board/Firmata: The VL53L0X talks directly to the host's I²C bus. It does not require an Arduino.
  • No exception wrapping: If the underlying adafruit_vl53l0x raises (e.g. no sensor found at the address), the raw exception propagates. There is no SensorError in the MegaWrapper hierarchy.

General

  • Port already in use: pyfirmata2 will raise an exception, which MegaWrapper converts to BoardConnectionError.
  • Serial latency: Some Arduino clones may need extra time to initialise after opening the serial port.
  • Power: Always use an external power supply for motors and servos. Do not draw significant current from the Arduino's 5 V pin.
  • delay() vs time.sleep(): delay(ms) is a thin wrapper around time.sleep(ms / 1000) — they are equivalent. Use whichever you prefer.

Clone this wiki locally