gpio API reference
Deprecated: use the
gpiolibrary (gpio.open(path)->gpio.Chip); removed in 0.29.0
Raspberry-Pi (and any Linux SBC) GPIO over sysfs. /sys/class/gpio is plain files, so fs is the whole backend: export a pin, set its direction, read / write its value, unexport it. "Blink an LED from a five-line script." It is stateless and pin-keyed (no handle is needed). A pin is the line offset on the Raspberry Pi header chip - the BCM GPIO number - and each call adds that chip's sysfs base (0 on old kernels, 512 and up since Linux 6.2) to get the global number sysfs wants. The header chip is found by its label (pinctrl-bcm2835, pinctrl-bcm2711, pinctrl-rp1); gpio.useChip picks another chip by label or switches to raw global numbers (gpio.GLOBAL). The sysfs root defaults to /sys/class/gpio; set the JENNIFER_GPIO_BASE environment variable to point elsewhere (a differently mounted sysfs, or a mock tree under test). This is the sysfs backend, which is deprecated in favour of the /dev/gpiochip character device but stays available on the hobbyist Pi kernels this targets. GPIO is a Linux-platform feature: off a GPIO-capable host every call reports a clear "base directory not found" error rather than crashing.
Deprecated: the gpio library (use gpio;) drives the same pins over the /dev/gpiochipN character device, the interface the kernel keeps. Open the chip once and pass it to each call: gpio.open("/dev/gpiochip0"), then gpio.setup($chip, 17, gpio.OUT), gpio.write($chip, 17, 1), ... The module is removed in 0.29.0.
Import with import "gpio.j" as gpio;. See the gpio guide for prose and examples.
Contents
| Name | What it is |
|---|---|
read | Return a pin's current value (0 or 1). |
release | Unexport a pin. |
setup | Export a pin and set its direction (gpio.IN or gpio.OUT). |
useChip | Select the chip whose lines the pin numbers name, for this process (it sets the JENNIFER_GPIO_CHIP_LABEL en... |
write | Set an output pin's value (0 or 1). |
| constants | the module's constants |
Functions
read
gpio.read(pin as int)Deprecated: use gpio.read(chip, pin) from the gpio library; removed in 0.29.0
Return a pin's current value (0 or 1).
pin- the line offset on the selected chip (the BCM GPIO number on a Raspberry Pi)
- Returns
int - the pin's current value, 0 or 1
- Throws
Error - kind "gpio" on a negative pin, an absent sysfs GPIO tree, no or several matching chips, a pin past the chip's lines, a read failure, or a value file holding anything but 0 / 1
release
gpio.release(pin as int)Deprecated: use gpio.release(chip, pin) from the gpio library; removed in 0.29.0
Unexport a pin.
pin- the line offset on the selected chip (the BCM GPIO number on a Raspberry Pi)
- Throws
Error - kind "gpio" on a negative pin, an absent sysfs GPIO tree, no or several matching chips, a pin past the chip's lines, or a write failure
setup
gpio.setup(pin as int, direction as string)Deprecated: use gpio.setup(chip, pin, direction) from the gpio library; removed in 0.29.0
Export a pin and set its direction (gpio.IN or gpio.OUT). An output starts low. Right after the export, writes are retried for about 1 s in total while udev grants access to the new attribute files.
pin- the line offset on the selected chip (the BCM GPIO number on a Raspberry Pi)
direction- the pin direction, gpio.IN or gpio.OUT (the "in" / "out" strings)
- Throws
Error - kind "gpio" on a negative pin, a direction other than gpio.IN / gpio.OUT, an absent sysfs GPIO tree, no or several matching chips, a pin past the chip's lines, or a write failure
useChip
gpio.useChip(label as string)Deprecated: use gpio.open(path) from the gpio library; removed in 0.29.0
Select the chip whose lines the pin numbers name, for this process (it sets the JENNIFER_GPIO_CHIP_LABEL environment variable, which can also be set outside the program). gpio.AUTO (the default) finds the Raspberry Pi header chip; a label selects the chip with exactly that sysfs label; gpio.GLOBAL makes pin numbers sysfs global numbers.
label- gpio.AUTO, gpio.GLOBAL, or a chip label such as "pinctrl-bcm2711"
write
gpio.write(pin as int, value as int)Deprecated: use gpio.write(chip, pin, value) from the gpio library; removed in 0.29.0
Set an output pin's value (0 or 1).
pin- the line offset on the selected chip (the BCM GPIO number on a Raspberry Pi)
value- the value to write, 0 or 1
- Throws
Error - kind "gpio" on a negative pin, a value other than 0 / 1, an absent sysfs GPIO tree, no or several matching chips, a pin past the chip's lines, or a write failure
Constants
gpio.AUTO as string- Chip selection for
gpio.useChip: find the Raspberry Pi header chip by its label (the default). gpio.GLOBAL as string- Chip selection for
gpio.useChip: pin numbers are sysfs global numbers, written toexportas given (no chip lookup, no range check). gpio.IN as string- Pin direction "input", for
gpio.setup: the sysfsdirectionstring, named so callers writegpio.setup(17, gpio.IN)(as RPi.GPIO'sGPIO.IN). A raw "in" still works, since the value is exactly that string. gpio.OUT as string- Pin direction "output", for
gpio.setup(gpio.setup(17, gpio.OUT)); the sysfsdirectionstring "out".