class Pin – control I/O pins

A pin is the basic object to control I/O pins (also known as GPIO - general-purpose input/output). 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:

Module pins are identified by their string id:

from machine import Pin
p = machine.Pin('P10', mode=Pin.OUT, pull=None, drive=Pin.MED_POWER, alt=-1)


class machine.Pin(id, ...)

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


Pin.init(mode, pull, *, drive, alt)

Initialise the pin:

  • mode can be one of:

    • Pin.IN - input pin.
    • Pin.OUT - output pin in push-pull mode.
    • Pin.OPEN_DRAIN - input or output pin in open-drain mode.
  • pull can be one of:

    • None - no pull up or down resistor.
    • Pin.PULL_UP - pull up resistor enabled.
    • Pin.PULL_DOWN - pull down resistor enabled.
  • alt is the id of the alternate function.

Returns: None.

Get the pin id.


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.

Pin objects are callable. The call method provides a (fast) shortcut to set and get the value of the pin. See Pin.value() for more details.


Toggle the value of the pin.


Get or set the pin mode.


Get or set the pin pull.[drive])

Get or set the pin drive strength.


class Pin.exp_board

Contains all Pin objects supported by the expansion board. Examples:

led = Pin(Pin.exp_board.G16, mode=Pin.OUT)
class Pin.module

Contains all Pin objects supported by the module. Examples:

led = Pin(Pin.module.P9, mode=Pin.OUT)


The following constants are used to configure the pin objects. Note that not all constants are available on all ports.


Selects the pin mode.


Enables the pull up or pull down resistor.