How to control GPIO via C or Python 3

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

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