Interface Pin

A General Purpose Input/Output (GPIO) pin.
The pin can be either in output or input mode, allowing for setting or reading the level.
Only one Pin instance of any given GPIO number can be open at any given point in time. This is a system wide restriction.
To release the resources associated with the Pin, call Pin.close.

Interface summary


constructor num/int --input/bool= --output/bool= --pull-up/bool= --pull-down/bool= --open-drain/bool= --allow-restricted/bool= --value/int=
Opens a GPIO pin on num in a custom mode.

Constructors

constructor num/int --input/bool=false --output/bool=false --pull-up/bool=false --pull-down/bool=false --open-drain/bool=false --allow-restricted/bool=false --value/int=0
Opens a GPIO pin on num in a custom mode.
If the pin is to be used by another peripheral, both input and output can be left as false. The library that uses the pin should call configure with the configuration it needs.
If a pin should be used both as input and as an output, open-drain is often needed to avoid short-circuits. See configure for more information.
Some pins should usually not be used. For example, the ESP32 uses pins 6-11 to communicate with flash and PSRAM. These pins can not be instantiated unless the allow-restricted flag is set to true.
If the pin is configured as output, the initial value can be set with the value parameter.

Statics

in num/int --pull-up/bool=false --pull-down/bool=false --allow-restricted/bool=false -> Pin
Opens a GPIO pin num in input mode.
While the pin is in input mode, pull-up and pull-down resistors are applied as configured.
See constructor for more information.

linux num/int --path/string --input/bool=false --output/bool=false --pull-up/bool=false --pull-down/bool=false --open-drain/bool=false --value/int=0 -> Pin
Opens a GPIO pin on the chip identified by the given path and num (often called "offset").
This constructor only works on Linux.
See constructor num for more information on the remaining parameters.

linux num/int --chip/Chip --input/bool=false --output/bool=false --pull-up/bool=false --pull-down/bool=false --open-drain/bool=false --value/int=0 -> Pin
Opens a GPIO pin on the given chip and num (often called "offset").
This constructor only works on Linux.
See constructor num for more information on the remaining parameters.

linux --name/string --path/string=null --input/bool=false --output/bool=false --pull-up/bool=false --pull-down/bool=false --open-drain/bool=false --value/int=0 -> Pin
Opens a GPIO pin based on the given name.
This constructor only works on Linux.
If a path to a chip is provided, finds the pin on that chip. Otherwise, searches all available chips (see Chip.list).
Names are not guaranteed to be unique. If multiple pins have the same name, the first pin found is returned.
See constructor num for more information on the remaining parameters.

linux --name/string --chip/Chip --input/bool=false --output/bool=false --pull-up/bool=false --pull-down/bool=false --open-drain/bool=false --value/int=0 -> Pin
Opens a GPIO pin based on the given name on the given chip.
This constructor only works on Linux.
If a chip is provided, finds the pin on that chip. Otherwise, searches all available chips (see Chip.list).
Names are not guaranteed to be unique. If multiple pins have the same name, the first pin found is returned.
See constructor num for more information on the remaining parameters.

out num/int --allow-restricted/bool=false -> Pin
Opens a GPIO pin num in output mode.
Use Pin.set to set the output value. The default value is 0.
See constructor for more information.

Methods

close -> any
Closes the pin and releases resources associated with it.

config --input/bool=false --output/bool=false --open-drain/bool=false -> any
Changes the configuration of this pin.
If open-drain is true, the output configuration will use - pull-low for 0 - open-drain for 1
Deprecated. Use configure instead. Note that configure behaves differently than config when a pin was initialized with a pull-up or pull-down resistor. This function (config) maintains the pull-up/pull-down configuration of the pin. However, configure resets that configuration.

configure --input/bool=false --output/bool=false --pull-up/bool=false --pull-down/bool=false --open-drain/bool=false --value/int=null -> none
Changes the configuration of this pin.
If input is true, the pin is configured as an input.
If output is true, the pin is configured as an output. If open-drain is set, then the pin value is set to 1 (not pulling to ground). Otherwise the pin outputs 0.
It is safe to use a pin as input and output at the same time, but typically this requires the open-drain flag.
If a pin is used as input and output without open-drain, then the pin can only read the value that was set with set. It can/should not read a value that was set by the outside. In fact, doing so could damage the microcontroller, as the external device would need to short circuit the pin.
If a pin is configured to be an input, it can have a pull-up or pull-down.
If the pin is configured as output, the value can be set with the value parameter. The new value might be written *before* the configuration is changed. If the pin is configured from push-pull to open-drain care must be taken that the new value is not 1, thus potentially short-circuiting the pin if another device is pulling the line low. When switching from push-pull to open-drain it is thus recommended to go through a short period of high-impedance (input) mode to avoid this issue.
If open-drain is set, then the pin can only pull the pin to the ground. Together, with a pull-up resistor this still allows the pin to emit both 0 and 1. In this configuration, connected devices can also safely pull the pin to ground without damaging the microcontroller. This configuration is typically used in communications that only use one data bus for input and output, such as the DHT11/DHT22, the i2c bus, and the one-wire bus. Note, that the corresponding libraries (like the i2c library) already take care of setting this configuration for you.
Note that it is not safe to ground an open-drain pin and to connect it externally to VCC.
Also note, that only one entity on an open-drain bus needs to pull the bus high. As such, it can be useful to set open-drain without pull-up.

do [block] -> any
Calls the given block on each edge on the pin.
An edge means a transition from high to low, or low to high.

get -> int
Gets the value of the pin.
It is an error to call this function when the pin is not configured to be an input.

num -> int
The number or offset of this pin.

set value/int -> any
Sets the value of the output-configured pin.

set-open-drain value/bool -> any
Sets the open-drain property of this pin.
This is a low-level function that doesn't affect any other configuration of the pin.

set-pull --up/bool=false --down/bool=false --off/bool=false -> any
Sets the pull-up/down resistor depending on the set flag.
If up is true, then the pull-up resistor is set.
If down is true, then the pull-down resistor is set.
If off is true, then neither pull-up nor pull-down resistor is set.
One, and only one of these flags must be set.

wait-for value/any -> none
Blocks until the Pin reads the value configured.
Use with-timeout to automatically abort the operation after a fixed amount of time.