UltiCon Part 6: Buttons
Thisāll be a short one since, there are soooo many blinky/gpio tutorials out there and Iām sure someone else does it way better than me. The next most important part is the buttons. As long as we have the analog sticks, the screen and keyboard, and the buttons, we have a controller!
Since I havenāt written the main program yet, Iām trying out pigpio, one of tooons of gpio libraries for the Pi, just to check out the differences to wiringpi (spoiler: from the GPIO perspective, I didnāt find them super different...Iāll admit it is kind of nice not having to do all this abstraction ourselves. If you do want to level up your direct register access and embedded skillz and youāve never done it, give it a shot! If youāre an aspiring firmware engineer, you WILL do it on the job).
The only interesting part here is that the Pi allows pullups/pulldowns on all GPIOs and interrupts on all of them as well (which is not true with all microcontrollers). That gives us a huge amount of freedom when we need to pick what needs to go to which pin.
With our little dev board, weāve got a great starting point since all of the GPIOs are put into a spot and we just need to reference the GPIO ports we want to read from (promise, schematic is coming, just havenāt set it in stone since I need to get all these tests out of the way).
First, letās start by downloading pigpio. Follow the instructions on this page: http://abyz.me.uk/rpi/pigpio/download.html.
If you check out the repo, under dev/buttons (https://github.com/amckeeota/UltiCon/tree/master/dev/buttons), I have two examples: buttontest.c and buttonIntTest.c. buttontest uses a polling loop and delays to check the button lines and see if anything has been pressed. Then it prints combos on the screen (try any combo and it should show which buttons you pressed). buttonIntTest registers a single function where we handle what to do with button presses and tell it weāre interested in both low to high and high to low transitions.
To build them, just type
make button
for buttontest.c or
make button_int
for buttonInt.c
For programming in C, the API docs are here: http://abyz.me.uk/rpi/pigpio/cif.html
And for python, theyāre here: http://abyz.me.uk/rpi/pigpio/python.html
Iām only using C since I want the final project to be coded in C.
I only used a handful of API functions:
gpioInitialise and gpioTerminate to initialize the pigpio library
gpioSetMode to set all of the button GPIO to input
gpioSetPullUpDown to set the internal pull up resistor to the button
gpioRead for buttontest.c to read the GPIO in the polling loop
gpioSetISRFunc for buttonIntTest.c to set an interrupt in response to a button press or release
Feel free to check through my code for how to use them.
The only thing we seem to be missing is debouncing. Iām a big proponent of debouncing living in hardware just because itās a little sloppy to deal with in software. This will guide some revision to my original board.
Oof sorry to be so barren from images this post but it turns out GPIO is pretty straightforward if youāre not doing it form scratch. Any questions though and Iāll gladly answer!
















