How to control GPIO via C or Python 3

i think i might have.
i went and uninstalled it then rebooted.
i ran

sudo apt show python3-libgpiod

again and got the same as what i posted. when i ran the python code its saying that there is no module named gpiod
do i reinstall using pip3 or another method?

Also sudo pip3 uninstall gpiod.

after running "sudo pip3 uninstall gpiod i got :

Skipping gpiod as it is not installed

Not sure what you did but just tested on the Raspbian image and it worked fine. What image are you using?

$ sudo apt install python3-libgpiod
$ python3 gpioget.py 
Traceback (most recent call last):
  File "/home/dxue/gpioget.py", line 17, in <module>
    raise TypeError('usage: gpioget.py <gpiochip> <offset1> <offset2> ...')
TypeError: usage: gpioget.py <gpiochip> <offset1> <offset2> ...
$ python3 gpioget.py 0 5
1 

I have no idea what happened with me but I had the same issue as you. I rebooted again & ran python3 -m pip uninstall gpiod and then it found it.

Now the script seems to work (at least it does not produce an error).

Following on this, I have had an LED & current limiting resistor hooked up & doing testing on pin 11 & nothing has worked.

When I finally got the Python test code to work (uninstalling the pip installed library —which for some reason would not work on the first couple of tries— did the trick), I thought back to how I also could not get it to work with the command line utilities and tried to put the LED on the pin next to it, pin 12.

It works now with that pin.

Is there any chance that I have a bad header pin?

The things I tried on the command line are:

  • sudo gpioset 0 8=1 (to turn pin 11 on: does not work).
  • sudo gpioset 0 8=0 (to turn pin 11 off: but who can tell?).
  • sudo lgpio set 11=1 (to turn pin 11 on: does not work).
  • sudo lgpio set 11=0 (to turn pin 11 off: but who can tell?).
  • sudo gpioset 0 6=1 (to turn pin 12 on: LED turns on).
  • sudo gpioset 0 6=0 (to turn pin 12 off: LED turns off).
  • sudo lgpio set 12=1 (to turn pin 12 on: LED turns on).
  • sudo lgpio set 12=0 (to turn pin 12 off: LED turns off).

Am I missing something here or do I have a bad pin 11?

Also, the get versions of those commands always seem to return a 1 (and turn off the LED if it was on). Am I lacking any understanding of what that command should do? That’s not what i expected.

See the GPIO map notes for AML-S905X-CC. Pin 11 requires you to move the HDMI CEC jumper.

gpioget returns arbitrary results unless you set either the default bias or pull it up to 3.3V or down to 0V.

1 Like

Correct. That was the issue.

sorry for the late response, seasons greetings everyone.
the issue has been resolved now, after uninstalling then reinstalling gpiod it works.
i have successfully blinked an led on several pins with my test script

thanks much guys for the help!!!

1 Like

A curious observation. I left the Le Potato on for days (probably since sometime around my last comment here so maybe around 3 days or more?) in the off state (I think) and the pin seemed… leaky (not fully on & not fully off)? I tried turning it off & it went off. I tried turning it on again & it went fully on.

What would be the reason for this and how do I address this?

If the GPIO is not set or driven to a specific state, it is in high Z state and could be high or low. This is the nature GPIOs and not specific to our boards.

They can also have default weak pull up or pull down. You can find more material on how GPIOs work online.

This thread is what I was looking for and while I know you’ve answered this questoin, I’m hoping to learn some more about this.

On a RPi3 when I apply power to the device (even before it’s booted up) the pins that I’m using always default to a LOW state. With my newly acquired AML-S905X-CC, it’s the opposite - and that’s not good for my scenario.

On the RPi3, I’ve read that the following is true:

  • GPIOs up to 8: default state is 1 (HIGH, or close to 3.3V).
  • GPIOs 9 to 27: default state is 0 (LOW, or close to 0V).

Is there a way to change the default behavior of the pins on this device, or is the “answer” for me to install a 10k Ohm resistor to ground to pull it down?

Thanks for any assistance here.

You have to create an overlay using the libretech-wiring-tool with changes to the default pin behavior.

See the pinctrl@14 section of aml-s905x-cc.dts.

1 Like

For whomever needs it: to be able to get/set GPIOs without root permissions, it is sufficient to add the user to the dialout group

adduser ubuntu dialout

Do not forget to logout to have the group changes take effect.

I believe that this is because /dev/gpiochip0 and /dev/gpiochip1 have permissions root:dialout by default.
I tested both the gpioset command and the python-periphery Python package, and neither gave me a Permission denied error.

1 Like

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