class Pin – control I/O pins

A Pin object represents a single GPIO on the STM32. It provides methods to configure the pin’s mode (input, output, alternate function, analog) and pull resistors, and to read or drive its digital level. For analog sampling see pyb.ADC; for alternate-function enumeration see PinAF.

All header pins are predefined as pyb.Pin.board.<name>. Most STM32 OpenMV Cams expose the I/O header pins P0P9; the OpenMV Cam N6 exposes additional pins up to P18:

p0 = pyb.Pin.board.P0
g = pyb.Pin(pyb.Pin.board.P0, pyb.Pin.IN)

The underlying STM32 port/pin can also be addressed directly through pyb.Pin.cpu.<name>, named as the port letter followed by the pin number (for example pyb.Pin.cpu.A0). The mapping of each OpenMV header pin to a CPU pin is fixed by the board.

Pins may also be selected by string name:

g = pyb.Pin("P0", pyb.Pin.OUT_PP)

User-defined names can be added with Pin.dict():

MyMapperDict = {"LeftMotorDir": pyb.Pin.cpu.A0}
pyb.Pin.dict(MyMapperDict)
g = pyb.Pin("LeftMotorDir", pyb.Pin.OUT_OD)

and queried back:

pin = pyb.Pin("LeftMotorDir")

Alternatively, a custom mapping function can be installed with Pin.mapper():

def MyMapper(pin_name):
    if pin_name == "LeftMotorDir":
        return pyb.Pin.cpu.A0

pyb.Pin.mapper(MyMapper)

so a call to pyb.Pin("LeftMotorDir", pyb.Pin.OUT_PP) passes "LeftMotorDir" directly to the mapper.

The following order determines how a name gets mapped to a physical pin:

  1. A Pin object is passed directly.

  2. The user-supplied mapper function returns a pin.

  3. The user-supplied dictionary contains a matching key.

  4. The string matches a board pin name (P0, P1, …).

  5. The string matches a CPU port/pin name (A0, B7, …).

Call pyb.Pin.debug(True) to print diagnostic information about how each object is mapped to a pin.

Constructors

class pyb.Pin(id: str | Pin, *args, **kwargs)

Create a new Pin object associated with the given id. If additional arguments are given they are forwarded to Pin.init() to configure the pin.

Class methods

classmethod debug(state: bool | None = None) bool | None

Get or set the debugging state (True or False for on or off).

classmethod dict(dict: dict | None = None) dict | None

Get or set the pin mapper dictionary.

classmethod mapper(fun: Callable[[str], Pin] | None = None) Callable[[str], Pin] | None

Get or set the pin mapper function.

Methods

init(mode: int, pull: int = Pin.PULL_NONE, *, value: int | None = None, alt: int | str = -1) None

Initialise the pin:

  • mode can be one of:

    • Pin.IN - configure the pin for input;

    • Pin.OUT_PP - configure the pin for output, with push-pull control;

    • Pin.OUT_OD - configure the pin for output, with open-drain control;

    • Pin.ALT - configure the pin for alternate function, input or output;

    • Pin.AF_PP - configure the pin for alternate function, push-pull;

    • Pin.AF_OD - configure the pin for alternate function, open-drain;

    • Pin.ANALOG - configure the pin for analog.

  • pull can be one of:

    • Pin.PULL_NONE - no pull up or down resistors;

    • Pin.PULL_UP - enable the pull-up resistor;

    • Pin.PULL_DOWN - enable the pull-down resistor.

    When a pin has the Pin.PULL_UP or Pin.PULL_DOWN pull-mode enabled, that pin is pulled to 3V3 or GND respectively through an internal resistor (typically tens of kOhm – see the electrical characteristics in the STM32 datasheet for the OpenMV Cam in use).

  • value if not None will set the port output value before enabling the pin.

  • alt can be used when mode is Pin.ALT, Pin.AF_PP or Pin.AF_OD to set the index or name of one of the alternate functions associated with a pin. This argument was previously called af which can still be used if needed.

value(value: Any | None = None) int | None

Get or set the digital logic level of the pin:

  • With no argument, return 0 or 1 depending on the logic level of the pin.

  • With value given, set the logic level of the pin. value can be anything that converts to a boolean. If it converts to True, the pin is set high, otherwise it is set low.

__str__() str

Return a string describing the pin object.

af() int

Returns the currently configured alternate-function of the pin. The integer returned will match one of the allowed constants for the af argument to the init function.

af_list() List[PinAF]

Returns an array of alternate functions available for this pin.

gpio() int

Returns the base address of the GPIO block associated with this pin.

mode() int

Returns the currently configured mode of the pin. The integer returned will match one of the allowed constants for the mode argument to the init function.

name() str

Get the pin name.

names() List[str]

Returns the cpu and board names for this pin.

pin() int

Get the pin number.

port() int

Get the pin port.

pull() int

Returns the currently configured pull of the pin. The integer returned will match one of the allowed constants for the pull argument to the init function.

Constants

IN: int

Configure the pin as a digital input (high-impedance).

OUT_PP: int

Configure the pin as a digital output with a push-pull driver.

OUT_OD: int

Configure the pin as a digital output with an open-drain driver.

ANALOG: int

Configure the pin as an analog input (e.g. for use with ADC).

ALT: int

Configure the pin as an alternate function (input or output).

AF_PP: int

Configure the pin as an alternate function with a push-pull driver.

AF_OD: int

Configure the pin as an alternate function with an open-drain driver.

PULL_NONE: int

Disable both pull-up and pull-down resistors on the pin.

PULL_UP: int

Enable the internal pull-up resistor on the pin.

PULL_DOWN: int

Enable the internal pull-down resistor on the pin.