The PiicoDev Button is an intuitive user input. It's easy to get started with; buttons are one of the most common user interfaces out there and for good reason - they're easy to understand and operate. I'm going to show you how to get started with the PiicoDev Button and a Raspberry Pi. We'll connect these two together and get some example code working to read the state of the button. Then we'll remix that code to do something a little more interesting like execute arbitrary code or launch some programs on the Raspberry Pi.
But first, a closer look at the button. The button features a clicky tactile button on the top and a bunch of electronics on the bottom. There are two PiicoDev connectors for daisy-chaining connections, four ID switches that allow multiple buttons to be connected together (leave all of these switches in the off position for now) and a small power LED. You may need to install the switch cap onto your button; simply hold it over the top and press until you feel it snap into place.
To follow along, you'll need a Raspberry Pi Single-Board Computer already set up to work like a desktop computer and to run PiicoDev projects. I've already got my PiicoDev adapter installed. You'll also need a PiicoDev button and a PiicoDev cable to connect everything together. Check the article if you need help setting up your Pi. Connect your PiicoDev cable to the adapter and connect the other end to your button.
I've booted up my Raspberry Pi, but before we begin we need to make sure that under the Preferences > Raspberry Pi Configuration that we have the I2C interface turned on. Find that under Interface > I2C and make sure that's turned on. Now we can open up Python and make sure that we have the latest version of PiicoDev installed. For Python, go to Tools > Manage Packages and search for Picadev. Here we are and make sure it's installed.
Sure, you upgrade or install as necessary. Now we're ready to begin working with the button in the article for this tutorial. Find the first example to check if the button was pressed. Highlight all of that example code and copy it into funny. Click run current script and if prompted to save, I'm going to save this as read_button.py and I'm saving that to a PiicoDev directory in my home directory. We can immediately see we're printing a bunch of zeros into the terminal and we can view that as a plotted graph. This is the state of the button; zero means the button is not pressed and when I press the button we get a spike in the graph to indicate the button was pressed. Click, click, click - three clicks, three spikes.
Nice! Time for a closer look at the code. We begin by importing the pukadev switch module. This is a generic module that we can use for the button, but it may also be useful for other PiicoDev switch devices in the future. We also import a function to create a delay, and then we initialize the button as a PiicoDev switch. We call the initialization function which returns a switch object and we call that button. So, every time you see button in their script, we're referring to this physical PiicoDev button.
Next, we have an infinite loop with the while true and we check if button.was_pressed. This is an attribute that can be true or false; it's true when the button was pressed. So, when the button was pressed we print a 1 to the Shell, else the button wasn't pressed we print a zero. And of course, there's a short delay. Now running the script, we can see that if I press and hold the button we still only get one pulse on the graph even though I'm holding the button down. That's because was_pressed is only interested in that one event - that pressing event. It basically means has the button been pressed? I can press and hold the button and the window will stay open
Since the last time we checked, if you want to get the state of the button right now, you can use the is pressed attribute. Now, when we run the script, I can press and we get that pulse, but if I press and hold, we can create a constant value. So then we have two ways to read from the button: was pressed, which checks if it was pressed since we last checked the button, and is pressed, which we would have to pull a lot more frequently, but we get the live state of the button right now.
Okay, so we can check the state of a button. What now? Let's do something more interesting than just print a one or a zero. Let's execute a system-level command, a Raspberry Pi Linux system-level command, when the button is pressed. You could use this to say shut down your Pi or to launch a specific program or even to launch your own script to run arbitrary code.
I've opened the terminal to show you that if I execute the command PC man FM, that will launch the file manager. That is the command that launches the built-in Raspberry Pi file manager. I propose that we remix our example code so that we launch the file manager at the press of a button. To do so, we'll need to import the subprocess module. Subprocess is used for executing shell commands. Now we can check for if button dot was pressed, we can open our file manager. We assign the process P subprocess dot run and subprocess dot run takes a list. The first entry in the list is the command PC man FM, and following items in that list are any arguments we want to use with that command. So we can say slash home slash Pi and we don't really care about the else statement, so we'll just remove that.
Moment of truth: we run the script and nothing's happened, but until we press the button. There it is! I've pressed the button and the window has appeared at my home directory. In fact, I think I can press and hold the button and the window will stay open. is a bit more interesting
It would have appeared at this directory if we had just executed the PC man FM. We could have it go to my Pica Dev directory like this: now when I run the script, press the button, here we are. We've opened it at the exact directory that we specified. How good! Now there's nothing stopping you from executing any command you want here, even to invoke more complex scripts. This is just the start.
Now it's possible to connect more than one PiicoDev button to a PiicoDev bus. To do this, they just each need unique ID switch settings. Recall that our first PiicoDev button we left all the ID switches off; this is the default state, and when we use a switch in the default state we can just initialize it like so.
For my second PiicoDev switch, I'm going to set the first ID switch to on, so now id1 is on. I can add that to the daisy-chain and now to include my second button in the code, I just need to initialize another PiicoDev switch. I can create button B and assign he could switch, and this is where we include the ID switch settings. We include the argument ID equals and then the four switch positions; the first switch is on so one and the other three switches are off, so three zeros. So we have I'll call this first button button a.
For completeness, if button a was pressed we'll launch our file manager at some directory and if button B dot was pressed we can say Q equals sub process dot run, we can just run the Chromium browser. I'll run this example. Okay, button a, we get our file manager that just appeared and button B, do we get the chromium? There it is, the Chromium browser has opened up almost immediately. Very nice!
In the other examples section of the article, under the multiple buttons tab, there is a simpler example that just uses the buttons to increment and decrement a counter, but I think that this one is a bit more interesting.
We were able to connect a single PiicoDev button to our Raspberry Pi and just check the state of the button. We used to Fork whether we were doing one thing printing a one or another thing printing a zero. From that, we remixed that example and included a couple of lines of code extra. Now we can launch any programs or code that we want from a mechanical button.
To wrap it up, we included another button launched another program. Of course, you could extend this out if you want to. If you make something cool from these starter projects or you just have some questions, let us know on our forums. We're full-time makers and happy to help. Until next time, happy making!
Makers love reviews as much as you do, please follow this link to review the products you have purchased.