Use a GPIO as a Button



  • I want to be able to use a GPIO as a button, I can check the state of a GPIO pin with cat but I would not like to poll it several times per minute because it eats cpu. Is there any way to do it.



  • @yaseen-almanna What you need is to be able to catch interrupts on the GPIO pin. I am not aware of any standard software package that easily accomplishes this.
    However, I have written a C/C++ code library that can do this (amongst other things).
    I also have written a standalone program that also allows you to set up for a command to be automatically run whenever the GPIO pin state changes.
    I have published the code and program at https://github.com/KitBishop/new-gpio
    Full documentation is in the file new-gpio.pdf
    The easiest form of the program (new-gpio) to use is at https://github.com/KitBishop/new-gpio/tree/master/bin/new-gpio/static-linked
    Copy the program to a suitable location on your Omega. Then, to use it to run a command whenever a designated GPIO pin changes state, use a command like:

    • ./new-gpio -s irq <pin-number> rising "<command-to-execute>" 200
      Where <pin-number> is the number of the GPIO pin
      rising indicates that the change of level from low to high is used to trigger the interrupt (use falling if you want high to low to trigger it)
      <command-to-execute> is the command that will be executed whenever the pin undergoes the relevant change - MUST be enclosed in quotes (") if it contains any special characters
      The 200 is a debounce time (in milliseconds) to allow for possibly noisy mechanical switches that could potentially trigger spurious interrupts.

    Whenever the pin undergoes the relevant change, the given command will be executed.

    When you no longer want the pin to trigger the command, it can be stopped using:

    • ./new-gpio -s irqstop <pin-number>

    Hope that helps :-)



  • @Kit-Bishop Thank you nice work, it will help alot.


Log in to reply
 

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