GPIO

Code Example

from periphery import GPIO

# Open GPIO 10 with input direction
gpio_in = GPIO(10, "in")
# Open GPIO 12 with output direction
gpio_out = GPIO(12, "out")

value = gpio_in.read()
gpio_out.write(value)

gpio_in.close()
gpio_out.close()

API

class periphery.GPIO(pin, direction='preserve')[source]

Bases: object

Instantiate a GPIO object and open the sysfs GPIO corresponding to the specified pin, with the specified direction.

direction can be “in” for input; “out” for output, initialized to low; “high” for output, initialized to high; “low” for output, initialized to low, or “preserve” for preserving existing direction. Default is “preserve”.

Parameters:
  • pin (int) – Linux pin number.
  • direction (str) – pin direction, can be “in”, “out”, “high”, “low”, or “preserve”.
Returns:

GPIO object.

Return type:

GPIO

Raises:
  • GPIOError – if an I/O or OS error occurs.
  • TypeError – if pin or direction types are invalid.
  • ValueError – if direction value is invalid.
  • TimeoutError – if waiting for GPIO export times out.
GPIO_EXPORT_STAT_RETRIES = 10
GPIO_EXPORT_STAT_DELAY = 0.1
read()[source]

Read the state of the GPIO.

Returns:True for high state, False for low state.
Return type:bool
Raises:GPIOError – if an I/O or OS error occurs.
write(value)[source]

Set the state of the GPIO to value.

Parameters:

value (bool) – True for high state, False for low state.

Raises:
  • GPIOError – if an I/O or OS error occurs.
  • TypeError – if value type is not bool.
poll(timeout=None)[source]

Poll a GPIO for the edge event configured with the .edge property.

timeout can be a positive number for a timeout in seconds, 0 for a non-blocking poll, or negative or None for a blocking poll. Defaults to blocking poll.

Parameters:

timeout (int, float, None) – timeout duration in seconds.

Returns:

True if an edge event occurred, False on timeout.

Return type:

bool

Raises:
  • GPIOError – if an I/O or OS error occurs.
  • TypeError – if timeout type is not None or int.
close()[source]

Close the sysfs GPIO.

Raises:GPIOError – if an I/O or OS error occurs.
fd

Get the file descriptor for the underlying sysfs GPIO “value” file of the GPIO object.

Type:int
pin

Get the sysfs GPIO pin number.

Type:int
supports_interrupts

Get whether or not this GPIO supports edge interrupts, configurable with the .edge property.

Type:bool
direction

Get or set the GPIO’s direction. Can be “in”, “out”, “high”, “low”.

Direction “in” is input; “out” is output, initialized to low; “high” is output, initialized to high; and “low” is output, initialized to low.

Raises:
  • GPIOError – if an I/O or OS error occurs.
  • TypeError – if direction type is not str.
  • ValueError – if direction value is invalid.
Type:

str

edge

Get or set the GPIO’s interrupt edge. Can be “none”, “rising”, “falling”, “both”.

Raises:
  • GPIOError – if an I/O or OS error occurs.
  • TypeError – if edge type is not str.
  • ValueError – if edge value is invalid.
Type:

str

class periphery.GPIOError[source]

Bases: exceptions.IOError

Base class for GPIO errors.