cancel
Showing results for 
Search instead for 
Did you mean: 

How to code to control GPIO from python

KNaka.6
Associate II

Hello, I have a question.

What are some ways to control GPIO from python?

I want to do that with a starter package.

Best Regards!

5 REPLIES 5
Pavel A.
Evangelist III

Does this Linux expose GPIOs in sysfs? /sys/class/... ?

Is this an interview question? :)

On the RPi I use this, I'd imagine ST has something similar for a GPIO library

https://pypi.org/project/RPi.GPIO/

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Olivier GALLIEN
ST Employee

Hi @KNaka.6​ ,

OpenSTLinux is not supporting "/sys/class/gpio" but only the /dev/gpiochip* devices together with libgpiod.

See https://wiki.st.com/stm32mpu/wiki/How_to_control_a_GPIO_in_userspace

Then for Python, interfacing with libgpiod command might be straightforward, or eventually using python3-gpiod.

Olivier

Olivier GALLIEN
In order to give better visibility on the answered topics, please click on 'Accept as Solution' on the reply which solved your issue or answered your question.
KNaka.6
Associate II

Thank you for your reply. I'm a Linux beginner and I'm sorry I couldn't explain it well. I'm using OpenStLinux-5.10-dunfell

​I have tried the following but I get a permission error. I don't know how to resolve. Or is this the wrong way?

root@stm32mp1:/dev# sudo apt install python3-libgpiod

>>> import gpiod

>>> chip=gpiod.chip('gpiochip0')

>>> prt=chip.get_lines([14])

>>> prt.set_values([1])

Traceback (most recent call last):

 File "<stdin>", line 1, in <module>

 File "/home/root/.local/lib/python3.8/site-packages/gpiod/libgpiodcxx/__init__.py", line 1083, in set_values

  self._m_bulk[i].set_value(values[i])

 File "/home/root/.local/lib/python3.8/site-packages/gpiod/libgpiodcxx/__init__.py", line 629, in set_value

  raise OSError(

PermissionError: [Errno 1] Operation not permitted: 'error setting GPIO line value'

Best Regards!

K-Naka

Olivier GALLIEN
ST Employee

Hi @KNaka.6​ ,

Please confirm first that playing with libgpiod works as explained in wiki :

Board $>gpioset gpiochip0 14=0  -->to set GPIO PA14 low // green led on
 
Board $>gpioset gpiochip0 14=1  -->to set GPIO PA14 high // green led off

Then @PatrickF​  just tell me he successfully tested this on DK2 out of the box ( and connected to network) :

python3 -m pip install -U --user pip gpiod 
 
python3 -m gpiod.test.button gpiochip0 14 both
 
-> button user1 is ok 
 
 python3 -m gpiod.test.blink gpiochip0 14
 
-> led blink ok 

Hope it help,

Olivier

Olivier GALLIEN
In order to give better visibility on the answered topics, please click on 'Accept as Solution' on the reply which solved your issue or answered your question.