How to control GPIO via C or Python 3

This is operating system specific and depends on the udev rules that map certain files to certain groups.

Yes. I am using the Ubuntu 22.04 Jammy LTS Desktop image.

Hello! Iā€™m having similar issues as some others in this thread, where I get an AttributeError for the module ā€˜gpiodā€™ not having an attribute. ā€˜line_requestā€™ in my case.

I have a fresh install of Raspbian from the Libre Computer distro (Index of /ci/raspbian/11/) on Le Potato.

I tried the following commands in this order (also see screenshot):

  • sudo pip3 uninstall gpiod
    • Result: already not installed
  • sudo apt install python3-libgpiod
    • Result: already had newest version (1.6.2-1)
  • python3 gpioget.py
    • Result: canā€™t open file, no file or directory
  • sudo apt show python3-libgpiod
    • Result: shows version 1.6.2-1 is installed
  • pip3 uninstall gpiod
    • Result: already not installed
  • lgpio info 3 gpiod
    • Result: 0 5

I also created a test Python script to use gpiod to light up an LED. The library can be imported and used to access the chip through chip = gpiod.Chip('0');. But, then fails on line_config = gpiod.line_request();

I am unsure where the problem is occurring. Any help would be appreciated!

You didnā€™t download the file. Why would you expect it to be there?

You donā€™t have to run any command for Raspbian. Everything is already pre-configured.

$ python3 gpioget.py 0 5
1

Hi,

I really Hope I can find an answer to my question here being that 99.98% of any resources I search for on the web looking for a simple Python Example to Read/Write GPIOs has the planet using import RPi.GPIO as GPIO and GPIO.setmode(GPIO.BCM) in the Python Codeā€¦ The not well designed architecturally and not well coded approach I take it.

To test out the ā€˜gpiodā€™ library I ran the sample command line examples from the replies above:
$ sudo lgpio set 29=0
$ sudo lgpio set 31=1
And this works great {I get GPIO 5 and 6 to go High & Low}ā€¦

Running the provided Python Examples ::
$ python3 gpioget.py 0 5
1
The gpioget.py python example appears to work as well.

However, when I ran the provided code for ā€œgpioset.pyā€ ::
$ python3 gpioset.py 0 29=0
I get an error messageā€¦

I saw on another LibreComputer HUB Post someone asked the question of ā€œTurning on an LED with GPIO pinsā€. Is there a more direct way to set a GPIO Low/High in Python using the gpiod library? Like with an example. Pretty Please with a Cherry on Topā€¦

Would the Python command look something like gpiod.Chip(0,29=0) ?
The only other resource Iā€™ve seen with libgpiod Iā€™ve seen is using C/C++ code ā€” nothing in Python besides the gpioset.py example.

Thanks in Advance

It would help to mention the error you get.

Ok Hopefully Iā€™ve sorted out the gpiod issue.

This is what I get with the two examples :-

$ python3 gpioget.py 0 5
1
$ python3 gpioset.py 0 29=0
Traceback (most recent call last):
File ā€œ/home/pi/Iain/gpioset.pyā€, line 27, in
lines = chip.get_lines(offsets)
OSError: [Errno 22] Invalid argument

I was hoping I should have run $ python3 gpioset.py 1 29=0 based upon the pinout Map and the Pin info below but the command just hangs ?

$ lgpio info 5
Chip Line sysfs Name Pad Ref Desc
0 4 505 GPIOAO_4 A10 I2C_SCK_AO I2C_SCK_AO // I2C_SLAVE_SCK_AO // UART_TX_AO_B
$ lgpio info 29
Chip Line sysfs Name Pad Ref Desc
1 96 497 GPIOX_17 B5 BT_EN BT_EN

A full ā€˜Blinkā€™ Python Example for the Le Potato might be a big help if anyone has got one !

You are using the wrong chip and line. GPIO header pin 29 is chip 1 and line 96. You are sending chip 0 line 29. It does not work because chip 0 only has 10 lines.

Thank You Iain_R.

That is exactly what a lot of us are asking :: Can someone provide a Python Example besides the gpioget.py and gpioset.py program that Bartosz Golaszewski wrote which prompts a user for parameters which are unclear.

A Simple Direct Read of a Status of GPIO #5 and a Simple Direct Write for a High/Low to GPIO #6 in Python using libgpiod; the well supported, stable, reliable, and well designed and actually Only Way LePotato can interface with GPIOs.

Thank You Very Much.

Hey Phrobenius & co

Try
$ lgpio info 31
Chip Line sysfs Name Pad Ref Desc
1 97 498 GPIOX_18 B7 BT_WAKE_HOST BT_WAKE_HOST
$ python3 gpioset.py 1 97=1
bye

My Led on (pin31/GPIOX_18 (Pi GPIO6)) lights up fine and Iā€™m sure pushbutton will work ok too. I guess it was all there to see from lgpio info but wasnā€™t completely obvious to me especially as sudo lgpio set 31=1 works !!!

Not sure why the input () is in the python gpioset.py code, but again obvious why the program hangs as its waiting for a Keyboard Entry - Hence the ā€œbyeā€.

The Handy Spreadsheet below can be accessed from the AML-S905X-CC Overview Resources and Guides above.

Well that was a tough afternoon trying to implement a trivial task of sequencing 4 LEDā€™s using the Le Potato GPIO using a slightly modified gpioset.py with the input() removed.

I call the command using OS which is pretty nasty !!

https://www.youtube.com/shorts/WXtxmbiVg8Y

So far WiringPi from 10 years ago seems a lot easier ?

I did a quick test of GitHub - c0t088/libregpio: python module that aims to provide basic, straight-forward GPIO input/output operations for Libre Computer "Le Potato".

No Instructions of course but I thought I would try it for my 4 Flashing LED Test.

However it seems pretty logical and easy to follow : -

import time #for timing delays
from libregpio import OUT, IN

LED1 = OUT(ā€œGPIOX_18ā€)
LED2 = OUT(ā€œGPIOX_6ā€)
LED3 = OUT(ā€œGPIOX_7ā€)
LED4 = OUT(ā€œGPIOX_5ā€)

for i in range(0,10):

print("Switching on Led")
OUT.output(LED1,1)
time.sleep(.2)
OUT.output(LED2,1)
time.sleep(.2)
OUT.output(LED3,1)
time.sleep(.2)
OUT.output(LED4,1)
time.sleep(1)

print("Switching off Led")
OUT.output(LED1,0)
time.sleep(.2)
OUT.output(LED2,0)
time.sleep(.2)
OUT.output(LED3,0)
time.sleep(.2)
OUT.output(LED4,0)
time.sleep(1)
2 Likes

Thank You Very Much Ian R ā€” you made my Saturdayā€¦

I canā€™t Believe to set a Stupid General Purpose I/O (GPIO) pin with a Python program we have to use a Generic Python program for driving logic and the implementation weā€™d need to use (x2) other Python Programs for this thing to workā€¦
libregpio.py
pin_mapping.py

I guess this is what we have to do to save about $120 on hardware only rich dummies are willing to pay for.

No Problem - Pay it Forward !

In all fairness pretty much every bit of code relies on multiple behind the scenes libraries - usually a bit more evident in C - which I mess with more often.

One of the attractions of the Pi originally was its price point, which allowed a Hobbyist/ Student to get started, but it is important to be able to make some progress quickly even with a ā€˜Blinkā€™ program.

Iā€™m going to try to recreate a project, developed in 2014 on a Pi 2 to drive two hand soldered 8x8 LED Xmas window lights, using the Le Potato for the challenge. Iā€™ll probably use some I2C along the way so I gather Iā€™ll be experimenting with Overlays.

Iā€™m planning to leverage the libregpio library, which does call on gpioset, as there will be plenty of I/O unless somone can recommend a better option.

Happy Programming

Shout out to Roberto Chen the owner of the libregpio repository who has just added some very good documentation to his library.

https://libregpio.readthedocs.io/en/latest/

1 Like

These are standard Linux GPIO libraries and python bindings. You write it once and it runs anywhere. If you do it differently, you bear the cost of not being portable.

1 Like

I did try out the attached interface and works pretty well on the Le Potato.

HCDC RPi GPIO Status LED & Terminal Block Breakout Board HAT for Raspberry Pi A+ 3A+ B+ 2B 3B 3B+ 4B (xikentech.com)

Also the digitalio library under Adafruit Blinka operates successfully on the Le Potato.

@Iain_R is digitalio installed via Blinka ?

Iā€™ve experimented briefly with GitHub - hhk7734/python3-gpiod: gpiod pure Python library with almost the same usage as libgpiodcxx (on a Pi Zero) and it looks like it might work with Le Potato but Iā€™ve not had chance to try it yet.

ā€œsudo apt install python3-libgpiodā€ also installed, but again Iā€™ve not had chance to try it yet.

Iā€™m still trying to move some code away from RPi.GPIO and having some teething issues with SPI pin conflicts.

Yes digitalio is accessed using Blinka as a pre-requisite.

Adafruit-Blinka Ā· PyPI

This repository contains a selection of packages emulating the CircuitPython API for devices or hosts running CPython or MicroPython. Working code exists to emulate these CircuitPython packages:

  • digitalio - digital input/output pins, using pin identities from board+microcontroller packages

Make sure you have the very latest library as I worked with the team there to get some pin changes for the SPI library specifically for the Le Potato last week.

Iā€™ve been banging my head against the examples included in /usr/share/doc/python3-libgpiod/examples, trying to get some of them working.

My hope is to use a Le Potato to listen for a button press, so basically respond when one of a series of buttons is pressed.

However, I canā€™t seem to get a button hooked up to line 82 (physical pin 38) to register.

pi@raspberrypi:/usr/share/doc/python3-libgpiod/examples $ ./gpiomon.py periphs-banks 82
Traceback (most recent call last):
  File "/usr/share/doc/python3-libgpiod/examples/./gpiomon.py", line 37, in <module>
    lines.request(consumer=sys.argv[0], type=gpiod.LINE_REQ_EV_BOTH_EDGES)
OSError: [Errno 19] No such device

This is on Le Potato, running the latest Raspbian OS image for it, with everything updated as of yesterday.

gpioget.py does seem to work:

pi@raspberrypi:/usr/share/doc/python3-libgpiod/examples $ ./gpioget.py periphs-banks 82
1

(Though itā€™s always inverse from what I expectā€”shouldnā€™t it default to 0 unless I press the button and close a circuit?)