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.
from machine import Pin # create an output pin on GPIO0 p0 = Pin(0, Pin.OUT) p0.value(0) p0.value(1) # create an input pin on GPIO2 p2 = Pin(2, Pin.IN, Pin.PULL_UP) print(p2.value())
init(mode, pull=None, *, value)¶
Initialise the pin:
mode can be one of:
Pin.IN- input pin.
Pin.OUT- output pin in push-pull mode.
pull can be one of:
None- no pull up or down resistor.
Pin.PULL_UP- pull up resistor enabled.
if value is given then it is the output value to set the pin if it is in output mode.
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.
valuegiven, set the logic level of the pin.
valuecan 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.
Returns a list of the alternate functions supported by the pin. List items are a tuple of the form:
irq(*, trigger, handler=None)¶
Create a callback to be triggered when the input level at the pin changes.
triggerconfigures the pin level which can generate an interrupt. Possible values are:
Pin.IRQ_FALLINGinterrupt on falling edge.
Pin.IRQ_RISINGinterrupt on rising edge.
The values can be OR’ed together to trigger on multiple events.
handleris an optional function to be called when the interrupt triggers.
Returns a callback object.
Pinobjects supported by the board. Examples:
Pin.board.GP25 led = Pin(Pin.board.GP25, mode=Pin.OUT) Pin.board.GP2.alt_list()
The following constants are used to configure the pin objects. Note that not all constants are available on all ports.