class Pin – control I/O pins — MicroPython latest documentation (original) (raw)

A pin object is used to control I/O pins (also known as GPIO - general-purpose input/output). Pin objects are commonly associated with a physical pin that can drive an output voltage and read input voltages. The pin class has methods to set the mode of the pin (IN, OUT, etc) and methods to get and set the digital logic level. For analog control of a pin, see the ADC class.

A pin object is constructed by using an identifier which unambiguously specifies a certain I/O pin. The allowed forms of the identifier and the physical pin that the identifier maps to are port-specific. Possibilities for the identifier are an integer, a string or a tuple with port and pin number.

Usage Model:

from machine import Pin

create an output pin on pin #0

p0 = Pin(0, Pin.OUT)

set the value low then high

p0.value(0) p0.value(1)

create an input pin on pin #2, with a pull up resistor

p2 = Pin(2, Pin.IN, Pin.PULL_UP)

read and print the pin value

print(p2.value())

reconfigure pin #0 in input mode with a pull down resistor

p0.init(p0.IN, p0.PULL_DOWN)

configure an irq callback

p0.irq(lambda p:print(p))

Constructors

class machine.Pin(id, mode=-1, pull=-1, *, value=None, drive=0, alt=-1)

Access the pin peripheral (GPIO pin) associated with the given id. If additional arguments are given in the constructor then they are used to initialise the pin. Any settings that are not specified will remain in their previous state.

The arguments are:

As specified above, the Pin class allows to set an alternate function for a particular pin, but it does not specify any further operations on such a pin. Pins configured in alternate-function mode are usually not used as GPIO but are instead driven by other hardware peripherals. The only operation supported on such a pin is re-initialising, by calling the constructor or Pin.init() method. If a pin that is configured in alternate-function mode is re-initialised with Pin.IN, Pin.OUT, orPin.OPEN_DRAIN, the alternate function will be removed from the pin.

Methods

Pin.init(mode=-1, pull=-1, *, value=None, drive=0, alt=-1)

Re-initialise the pin using the given parameters. Only those arguments that are specified will be set. The rest of the pin peripheral state will remain unchanged. See the constructor documentation for details of the arguments.

Returns None.

Pin.value([_x_])

This method allows to set and get the value of the pin, depending on whether the argument x is supplied or not.

If the argument is omitted then this method gets the digital logic level of the pin, returning 0 or 1 corresponding to low and high voltage signals respectively. The behaviour of this method depends on the mode of the pin:

If the argument is supplied then this method sets the digital logic level of the pin. The argument x can be anything that converts to a boolean. If it converts to True, the pin is set to state ‘1’, otherwise it is set to state ‘0’. The behaviour of this method depends on the mode of the pin:

When setting the value this method returns None.

Pin.__call__([_x_])

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

Pin.on()

Set pin to “1” output level.

Pin.off()

Set pin to “0” output level.

Pin.irq(handler=None, trigger=Pin.IRQ_FALLING | Pin.IRQ_RISING, *, priority=1, wake=None, hard=False)

Configure an interrupt handler to be called when the trigger source of the pin is active. If the pin mode is Pin.IN then the trigger source is the external value on the pin. If the pin mode is Pin.OUT then the trigger source is the output buffer of the pin. Otherwise, if the pin mode is Pin.OPEN_DRAIN then the trigger source is the output buffer for state ‘0’ and the external pin value for state ‘1’.

The arguments are:

This method returns a callback object.

The following methods are not part of the core Pin API and only implemented on certain ports.

Pin.low()

Set pin to “0” output level.

Availability: mimxrt, nrf, renesas-ra, rp2, samd, stm32 ports.

Pin.high()

Set pin to “1” output level.

Availability: mimxrt, nrf, renesas-ra, rp2, samd, stm32 ports.

Pin.mode([_mode_])

Get or set the pin mode. See the constructor documentation for details of the mode argument.

Availability: cc3200, stm32 ports.

Pin.pull([_pull_])

Get or set the pin pull state. See the constructor documentation for details of the pull argument.

Availability: cc3200, stm32 ports.

Pin.drive([_drive_])

Get or set the pin drive strength. See the constructor documentation for details of the drive argument.

Availability: cc3200 port.

Pin.toggle()

Toggle output pin from “0” to “1” or vice-versa.

Availability: cc3200, esp32, esp8266, mimxrt, rp2, samd ports.

Constants

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

Pin.IN

Pin.OUT

Pin.OPEN_DRAIN

Pin.ALT

Pin.ALT_OPEN_DRAIN

Pin.ANALOG

Selects the pin mode.

Pin.PULL_UP

Pin.PULL_DOWN

Pin.PULL_HOLD

Selects whether there is a pull up/down resistor. Use the valueNone for no pull.

Pin.DRIVE_0

Pin.DRIVE_1

Pin.DRIVE_2

Selects the pin drive strength. A port may define additional drive constants with increasing number corresponding to increasing drive strength.

Pin.IRQ_FALLING

Pin.IRQ_RISING

Pin.IRQ_LOW_LEVEL

Pin.IRQ_HIGH_LEVEL

Selects the IRQ trigger type.