Pi IoT In Python Using Linux Drivers - GPIO Character Driver
Written by Mike James & Harry Fairhead   
Monday, 21 November 2022
Article Index
Pi IoT In Python Using Linux Drivers - GPIO Character Driver
Installing libgpiod and Python Binding
Working With More Than One Line

Working With More Than One Line

There are also “bulk” versions of the line functions which work with a set of lines at one time. The key to understanding these is the LineBulk object

lines=gpiod.LineBulk([List of Line objects])

For example:


This isn’t a particularly useful way of creating a LineBulk object and the get_lines method is more commonly used:

lines = chip.get_lines(offsets)

where offsets is a List of GPIO line offsets.

Using this the previous example can be written:


You can do the same thing using find_lines which returns a LineBulk object with lines defined by a List of names.

There is also a method that will get all of the lines supported by a GPIO chip:


Once you have a LineBulk object you can initialize all of the lines it represents using its request method:

request(consumer, type=None, flags=None, 

This sets all of the lines to the same consumer, type and flags. The default_vals is a list of values that are applied one per line.

For example, to set the two lines to output you would use:

type=gpiod.LINE_REQ_DIR_OUT, default_vals=[0,1])

Notice that consumer and type are set to the same value for each line, but the default values are different.

There is also a function that will close all of the lines that you have been working with:


Once you have set up the bulk lines you can get and set them:


For the get function the vals List is set to the state of all of the lines, including output lines, and for the set function the vals List is written to the lines.

How these functions all work is easy to understand after you see an example. In this case the program pulses two lines that are out of phase:

import gpiod
type=gpiod.LINE_REQ_DIR_OUT, default_vals=[0,1])

The new steps are the creation of a LineBulk object for lines GPIO 4 and GPIO 17. Once we have the line_bulk object we set the lines to output mode and then the set_values method sets them to 1,0 and 0,1.

You can see that despite the program implying that the lines are switched at the same time, they are not:

You can see that for a Pi 4 the pulse is about 2µs and 240kHz and there is a lag of 0.1µs. For a Pi Zero the pulse width is 16µs a frequency of 30kHz the lag is around 0.2µs.

In chapter but not in this extract

Using GPIO Lines – Measuring R and C


  • The GPIO character driver replaces the sysfs GPIO driver and it is the one to use for all future projects.

  • The GPIOD library provides a higher level way of using the GPIO character driver, but if you don’t want the overhead of using it then the direct ioctl interface is easy to use.

  • After the library has been installed there are a number of utilities that are sometimes useful at the command line or in scripts.

  • The C functions wrapped by the Python bindings do require you to get/open the GPIO line and configure it before you use it. Once used you have to release the line to allow another process to make use of it or to change its configuration.

  • You can work with multiple GPIO lines in a single function call. However, there are still delays between setting lines to particular values.

  • The Python methods can create pulses as fast as 40µs (Pi Zero) to 4µs (Pi 4).

  • Using the time-to-charge you can measure either the resistance or the capacitance of a circuit using a single GPIO line.

Raspberry Pi IoT In PythonUsing Linux Drivers

By Harry Fairhead & Mike James


Buy from Amazon.


  1.  Choosing A Pi For IoT
  2.  Getting Started With Python
  3.   Drivers: A First Program
  4.  The GPIO Character Driver ***NEW!!
  5.  GPIO Using I/O Control
  6.  GPIO Events
  7.  The Device Tree
       Extract: The DHT22   
  8.  Some Electronics
  9.  Pulse Width Modulation
       Extract: PWM 
  10. SPI Devices
  11. I2C Basics
       Extract: I2C 
  12. The I2C Linux Driver
  13. Advanced I2C
  14. Sensor Drivers
  15. 1-Wire Bus
       Extract 1-Wire And The DS18B20 
  16. Going Further With Drivers
  17. Appendix I



To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.


Visual Studio Code Adds More Audio Cues

Visual Studio Code has been updated with the addition of more audio cues, and improvements to the Python extension.

Google Indie Games Accelerator 2024

Google has announced that it has opened submissions for the Indie Games Accelerator 2024, a 10-week accelerator program for high potential indie game studios, designed to help programmers build a succ [ ... ]

More News




or email your comment to: comments@i-programmer.info





Last Updated ( Wednesday, 23 November 2022 )