Skip to main content

Managing GPIO pins via character device

Basics

For the set of GPIO lines (pins) we create an object called GPIOHandle, which manages their state. State is a tuple of 0's and 1's, one number per line.

This library doesn't require root access to the system, but it needs a read-write access to the gpiochip device. By default it uses /dev/gpiochip0.

To allow read-write access to the device for the user, run:

$ sudo chmod a+rw /dev/gpiochip0

Note that the system might have several GPIO chips, some of them can be exposed to the user (as /dev/gpiochip0 on Raspberry Pi) and some of them might be responsible for system functions, like WakeOnLan or system LED lights. Be carefull when choosing the device and allowing user access to it.

You can check the info on the GPIOChip device, by accessing its info() method. See example below.

Example

from gpiodev import GPIOHandle
import time

# Request handle for lines 12 and 23 from default /dev/gpiochip0 

DoubleLED = GPIOHandle((12,23))

# Define states of the Double LED

all = (1, 1)
none = (0, 0)
first = (1, 0)
second = (0, 1)

# Loop through the states

for state in [all, none, first, second, none, all, none]:
    DoubleLED.set_values(state)
    print(DoubleLED.get_values())
    time.sleep(1)

Use another device

To use another gpiochip device, for example /dev/gpiochip1, you can use a different way to setup a handle:

from gpiodev import GPIOChip

GPIO = GPIOChip("/dev/gpiochip1")

# Check info on the gpio chip

print(GPIO.info())

# Request handle for lines 12 and 23 from /dev/gpiochip1

DoubleLED = GPIO.get_handle((12,23)) # This will fail on RaspberryPi as /dev/gpiochip1 is a system gpio chip, with only 8 lines.

Background

New GPIO interface has been introduced in the kernel.

It exposes GPIO interface a character device(/dev/gpiochip0) and provides several ioctl syscalls for bulk operations on sets of GPIO pins.

Unlike Python bindings to the libgpiod C-library, we work with the kernel interface (ioctl calls) directly.

In gpiodev/src/gpioctl.c we wrap the ioctl calls into C-functions suitable for later use.

In gpiodev/gpio.py the ctypes bindings are created and then used to define the main GPIOHandle class.


Tested on Fedora 26+ armhfp, Raspberry Pi 3 Model B.

Metadata

Release files for gpiodev 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for gpiodev 0.0.1
File Size Uploaded Provenance Actions
gpiodev-0.0.1.tar.gz 10.6 kB Without provenance Details for gpiodev-0.0.1.tar.gz

Release files / gpiodev-0.0.1.tar.gz

Download URL gpiodev-0.0.1.tar.gz
Size 10.6 kB
Tags Source
SHA-256 checksum
How to use checksums
e49b9b75b118c7d433822dab72f0a45e83ef901005a825ddc36d88108e54e52d
BLAKE2b-256 checksum
How to use checksums
b8c88bbc19413ae5f0eec83f641f12c3517ed17aad9eecf85bb2e7580ceac537
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.2 pkginfo/1.4.2 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.37.0 CPython/3.7.5

Security for gpiodev 0.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.0.1 This release

1 release file No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page