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 P0 … P9; 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:
A
Pinobject is passed directly.The user-supplied mapper function returns a pin.
The user-supplied dictionary contains a matching key.
The string matches a board pin name (
P0,P1, …).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 toPin.init()to configure the pin.Class methods¶
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_UPorPin.PULL_DOWNpull-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_PPorPin.AF_ODto set the index or name of one of the alternate functions associated with a pin. This argument was previously calledafwhich 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
valuegiven, set the logic level of the pin.valuecan be anything that converts to a boolean. If it converts toTrue, the pin is set high, otherwise it is set low.
- 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.
Constants¶