Connecting Input Keys to Raspberry Pi through Device Tree and GPIO
Disclaimer: The following article mentions multiple topics like electronic circuits, breadboards, device trees, Raspberry Pis and more. It’s not intended to go deep into any of those but there are countles resources in the internet to gain more knowledge about them.
Learning Computer Science, Electronics, Robotics and/or Engineering is no easy tasks. Sometimes it’s hard to picture concepts in your head without being able to have physical representations to relate them to.
In comes the Raspberry Pi. These are a powerful and yet entry level series of Single Board Computers. The Raspberry Pi has become hugely popular with both professional and enthusiasts, being used in all kinds of fields, like Entertainment, Robotics, Education, Cloud, Energy, etc.
In this article we will take two 4-pin push buttons and connect them as if they were Keyboard-like input events to a Raspberry Pi 4 Model B using a Device Tree Overlay.
Note: In case that you’re just getting started and don’t have a monitor/keyboard/mouse for your RPI, this tutorial is great.
Parts
- 1 x Breadboard.
- 2 x 4-Pin Push Buttons.
- 3 x Female-To-Male Dupont Wires.
- 2 x Jumper-Wires.
- 1 x Raspberry Pi 4.
Note: For this tutorial we used the Raspberry Pi OS (32-Bit) installed through the Raspberry Pi Imager, but probably it won’t be an issue to use another Linux distro.
Layout
Hardware Configuration
- Insert the two 4-pin push buttons into the breadboard with some spacing between them and connect them with the two jumper wires into what will be our ground rail.
- Connect both buttons to the GPIOs of your choosing. For this article GPIO 2 and 3 will be used.
- Finally connect what will be the ground rail (The one with the black cables) to the ground pin on the RPI.
Software Configuration
- Log into the RPI and open a text editor. Write down the following modifyng as neccessary. Save the file with the
.dts
extension.
The code above is a Device Tree specification that will be added to the RPI’s Device Tree without having to modify the OS source code (This cannot be done in all Linux distro’s, some of them will require adding the modification, configuring it properly and then re-compiling the OS).
In the above code we use the Linux input event codes 0x100
and 0x101
for each of our buttons’ nodes so that these will be recieved by the OS system-wide when the buttons are pressed.
- Open a terminal and add the overlay to the OS’ Device Tree, it needs to be compiled first using the following command.
dtc -I dts -O dtb -o <filename>.dtbo <filename>.dts
- Load the compiled overlay (The one ending on
.dtbo
), the following command is used:
sudo dtoverlay <filename>.dtbo
Note: When working with Device Tree overlays, the same overlay can be added twice, so in case you want to make some changes and experiment, remember to remove the previous overlay using
sudo dtoverlay -r <name_of_overlay>
.
- Test the overlay with
evtest
by running the following command.
evtest /dev/input/event<event_numver>
The same file descriptor can be used to access the input events from other programs.
Note: In the case of the implemented system there was only one input event file descriptor (
/dev/input/event0
), but the input event file descriptors can be checked by running the program without any parameters.
Conclusion
Electronics and IoT are very fun and interesting fields to delve deeper into. Be it by working with leds, fans and buttons, or by doing some more complex ventures like the one described in this article. It was somewhat difficule to reach this outcome and thus I thought it would be useful for others and for myself in the future.