GPIO and Python?

  • Hi everyone. I just got my Onion Omega a couple of days ago. I'm a hardware person, but I've been away from software for a while. I'm learning Python, but I'd like to be able to control the GPIO pins directly from my Python code. Is that possible now? If not, is it coming soon?

  • Im not the offical word, But In my logic The onion omega runs linux, What else runs linux a rasberry pi, a rasberry pi also has gpio's and a Python library.

    So based off of that logic I would have to guess that that python library would work. I could be 100% wrong and that library could be pi specific.

    The only way to know is to try. Give it a shot and let us know.

  • @chris-mccaslin Thanks for the suggestion. I wasn't able to install that library, but that may be because I'm not familiar enough with Linux to understand what it needs. ;-) I did figure out how to access the ports through the filesystem (/sys/class/gpio). If nothing else gets posted before I get my code packaged nicely, I'll post it for others.

  • I've posted some simple code at on GitHub.

  • Is it possible to use Interrups (Falling;Rising;Change) like I know it from Arduino?
    I would like to read a rotery-encoder with my

    Thanks in advance.

  • @Uwe-Stahl209 While I know little about Python, I have written some C++ code that provides a library and access program for GPIO control in general - this includes code for catching and acting on interrupts. In case it is of any use for you, while I have posted information on it in this community, the information can best be found at - see the documentation in the PDF file there - new-gpio.pdf

  • Yes, this I know, but a Python library would be nice as well :)

    A general question: On AVR devices you have "real" interrupts in the sense that special pins provide this functionality.
    Is is possible on Omega as well? Or is you code just looking for pin-change with the ordinary read functionality of the pin status?



  • @Uwe-Stahl209 I'm too new to Python and the Omega to add interrupts to my code. I'm not sure that Python can handle interrupts directly, but I could be wrong.

  • @Uwe-Stahl209 The code I referenced above does use "real" interrupts via the use of the Omega library gpio-irq and usage of the /sys/kernel/debug/gpio-irq system file system.

    As I said I don't know much about Python and what facilities it provides in connection with interrupts. My guess is that some sort of wrapper code would be needed to provide such functionality (as I am doing and have largely working for Java).

    Also, while I don't know what you want to do with the interrupts in Python - i.e. to be able to respond to interrupts within Python code or to just run some Python code when a pin change occurs I would draw you attention to the capability of the new-gpio program to run ANY command in response to an interrupt on a pin. Just use a command like:

    • new-gpio irq <pin-number> <irq-type> "<command" <optional-debounce>
      • <pin-number> is the relevant pin
      • <irq-type> is one of rising or falling or both - depending upon what pin level change you want to trigger the interrupt
      • <command> is the command to be executed when the interrupt occurs. So long as you have not stopped interrupt handling on the pin by using the irqstop command to new-gpio for the pin this command will be executed each and every time the interrupt occurs (i.e. the pin undergoes the relevant change)
      • <optional-debounce> is a delay time in milliseconds to allow for debounce handling on noisy switches

    More information can be found in the documentation in new-gpio.pdf file I supplied.

  • Hi!

    I want to read a Rotary-Encoder and do some other stuff like sending data via serial output.On Arduino this is normally made with an interrupt pin.
    So I don't get it: Can I use your library in Python? Or could I use your program to run a small Python script that somehow sends the rotary-value to my main Pythons script where all the rest is done?



  • @Uwe-Stahl209 A few more comments that may assist you:

    1. On the Omega, all GPIO pins are capable of generating interrupts, unlike on the Arduino where there is a limited number of pins that can generate interrupts

    2. Interrupt handling is done on the hardware level and needs C/C++ to handle it. On the Arduino, all code is written in C/C++ and functions are provided that enable you to catch and react to the interrupts. My libnew-gpio code provides similar C/C++ functionality for interrupt handling.

    3. To handle interrupts in any other language (e.g. Python) requires the ability for that language to call the relevant C/C++ (e.g. via wrapper functions) to gain that functionality. I know how to do this for Java (and have done so) but I do not know how to do this for Python - perhaps someone else does.

    4. While as I have said, I don't know much about Python, if, as your message suggests, one Python script can send a message to another Python script, then your suggestion may work. Lets say you have two Python scripts:

      • python-a that reads you rotary encoder values and sends a message to python-b indicating that the encoder value has changed
      • python-b that can handle messages from python-a

    Then something like the following might work:

    • new-gpio irq <pin-number> <irq-type> "command to run python-a" <debounce-if-required>
      run python-b

  • @Brian-Piersel Thanks for the simple-to-use code! I've written a simple Python script that imports your omega_gpio script and controls three of four relays on a relay module to light various combinations of RGB LEDs on a light strip (the script is fairly well documented, so further description is probably not necessary here):

Log in to reply

Looks like your connection to Onion Community was lost, please wait while we try to reconnect.