class Pin – control I/O pins

A pin is the basic object to control I/O pins. It has methods to set the mode of the pin (input, output, etc) and methods to get and set the digital logic level. For analog control of a pin, see the ADC class.

Usage Model:

Board pins are identified by their string name:

g = pyb.Pin('GPIO9', af=0, mode=pyb.Pin.IN, type=pyb.Pin.STD, strength=pyb.Pin.S2MA)

You can also configure the Pin to generate interrupts. For instance:

def pincb(pin):

pin_int = pyb.Pin('GPIO10', af=0, mode=Pin.IN, type=pyb.Pin.STD_PD, strength=pyb.Pin.S2MA)
pin_int.callback (mode=pyb.Pin.INT_RISING, handler=pincb)
# the callback can be triggered manually
# to disable the callback

Now every time a falling edge is seen on the gpio pin, the callback will be executed. Caution: mechanical push buttons have “bounce” and pushing or releasing a switch will often generate multiple edges. See: for a detailed explanation, along with various techniques for debouncing.

All pin objects go through the pin mapper to come up with one of the gpio pins.


class pyb.Pin(name, ...)

Create a new Pin object associated with the name. If additional arguments are given, they are used to initialise the pin. See pin.init().


pin.init(af, mode, type, strength)

Initialise the pin:

  • af is the number of the alternate function. Please refer to the pinout and alternate functions table. for the specific alternate functions that each pin supports.

  • mode can be one of:

    • Pin.OUT - no pull up or down resistors.
    • Pin.IN - enable the pull-up resistor.
  • type can be one of:

    • Pin.STD - push-pull pin.
    • Pin.STD_PU - push-pull pin with pull-up resistor.
    • Pin.STD_PD - push-pull pin with pull-down resistor.
    • Pin.OD - open drain pin.
    • Pin.OD_PU - open drain pin with pull-up resistor.
    • Pin.OD_PD - open drain pin with pull-down resistor.
  • strength can be one of:

    • Pin.S2MA - 2mA drive capability.
    • Pin.S4MA - 4mA drive capability.
    • Pin.S6MA - 6mA drive capability.

Returns: None.


Set the pin to a high logic level.


Set the pin to a low logic level.


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.

Toggle the value of the pin.

Return a 5-tuple with the configuration of the pin: (name, alternate-function, mode, type, strength)

pin.callback(mode, priority=1, handler=None, wakes=pyb.Sleep.ACTIVE)

Create a callback to be triggered when data is received on the UART.

  • mode configures the pin level which can generate an interrupt. Possible values are:

    • Pin.INT_FALLING interrupt on falling edge.
    • Pin.INT_RISING interrupt on rising edge.
    • Pin.INT_RISING_FALLING interrupt on rising and falling edge.
    • Pin.INT_LOW_LEVEL interrupt on low level.
    • Pin.INT_HIGH_LEVEL interrupt on high level.
  • priority level of the interrupt. Can take values in the range 1-7. Higher values represent higher priorities.

  • handler is an optional function to be called when new characters arrive.

  • wakes selects the power mode in which this interrupt can wake up the board. Please note:

    • If wakes=pyb.Sleep.ACTIVE any pin can wake the board.
    • If wakes=pyb.Sleep.SUSPENDED pins GPIO2, GPIO4, GPIO10, GPIO11, GPIO17`` or GPIO24 can wake the board. Note that only 1 of this pins can be enabled as a wake source at the same time, so, only the last enabled pin as a pyb.Sleep.SUSPENDED wake source will have effect.
    • If wakes=pyb.Sleep.SUSPENDED pins GPIO2, GPIO4, GPIO10, GPIO11, GPIO17`` and GPIO24 can wake the board. In this case all this 6 pins can be enabled as a pyb.Sleep.HIBERNATE wake source at the same time.
    • Values can be ORed to make a pin generate interrupts in more than one power mode.

Returns a callback object.



input pin mode


output pin mode


push-pull pin type


push-pull pin with internall pull-up resistor


push-pull pin with internall pull-down resistor


open-drain pin


open-drain pin with pull-up resistor


open-drain pin with pull-down resistor


interrupt on falling edge


interrupt on rising edge


interrupt on rising and falling edge


interrupt on low level


interrupt on high level


2mA drive strength


4mA drive strength


6mA drive strength