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.
Quick Usage Example
from machine import Pin# initialize `P9` in gpio mode and make it an outputp_out =Pin('P9',mode=Pin.OUT)p_out.value(1)p_out.value(0)p_out.toggle()p_out(True)# make `P10` an input with the pull-up enabledp_in =Pin('P10',mode=Pin.IN,pull=Pin.PULL_UP)p_in()# get value, 0 or 1
Constructors
class machine.Pin(id, ...)
Create a new Pin object associated with the string id. If additional arguments are given, they are used to initialise the pin. See pin.init()
Methods
pin.init(mode, pull, * , 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.
pin.id()
Get the pin id.
pin.value([value])
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([value])
Pin objects are callable. The call method provides a (fast) shortcut to set and get the value of the pin.
Example:
See pin.value() for more details.
pin.toggle()
Toggle the value of the pin.
pin.mode([mode])
Get or set the pin mode.
pin.pull([pull])
Get or set the pin pull.
pin.hold([hold])
Get or set the pin hold. You can apply a hold to a pin by passing True (or clear it by passing False). When a pin is held, its value cannot be changed by using Pin.value() or Pin.toggle() until the hold is released. This Can be used to retain the pin state through a core reset and system reset triggered by watchdog time-out or Deep-sleep events. Only pins in the RTC power domain can retain their value through deep sleep or reset.