Buttons are a ubiquitous user interface - of course, you've seen them everywhere! The humble button is often the fastest way to create a control interface for your project too. This guide will help you get started with a PiicoDev® Button.

Transcript

The PiicoDev Button is an intuitive user input. It's easy to get started with buttons, 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 your PiicoDev Button and a Raspberry Pi Pico. We'll connect these two together and get some example code running to detect whether the button was pressed, and then we'll remix that code to control how the Pico works based off the button.

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 of course need a Raspberry Pi Pico with the pin soldered facing down, a PiicoDev Expansion Board for Raspberry Pi Pico, a PiicoDev Button, and a Pigative cable to connect everything together. Mount your Pico into the Expansion Board and make sure pin zero is to the left of the USB connector. Plug your PiicoDev cable into the bottom and connect the other end to your Pigative Button, and connect to your computer with a USB cable.

Now to download the driver files, in the article find the download section, select the Raspberry Pi PicoTab and find the PiicoDev Unified link. Right click and save link as, and I'll save that to a PiicoDev directory in my documents. Do the same with Picca.Switch.py. With those files downloaded, open up Thonny if you've never used Sony and the Rise Proof Hypiga before - check the article for help getting started. I've navigated to the PiicoDev directory and I can see the two files I just downloaded.

I'll connect to my Raspberry Pi Pico to where my files are saved, select both files and upload them to the Pico. I'll return to the article and find the first example. I'll check if the button was pressed, highlight all of that code and paste it into a new script in funny. I'll click the Green Run button and save it to my Raspberry Pi Pico as main.py. Our code immediately starts running and we can see a bunch of zeros plotted to the shell and a graph of that value. This is the state of the button when I click the button, we can see a spike in that graph and that correlates to a value of 1 being printed in the Shell. So, we have zero when the button isn't pressed and we have one when the button is pressed.

I've rerun the script and if I click click click, we can see three pulses in the graph. Interestingly, if I press and hold, we don't get a constant value, we still only get one pulse. We get one pulse for one press. Let's take a closer look at the code. We start by importing PiicoDev switch. This is a generic switch class that applies to the picada button and may apply to other PiicoDev switches in the future. We also import a sleep function to create a delay. Next, we initialize our button using the PiicoDev switch initialization function. We assign that to the object button, so anytime we see button in the script, we're referring to this physical button. Next, we have an infinite Loop and we're checking if button.was pressed. The.was pressed attribute returns true or false and so if the button was pressed, we will print a 1 to the Shell else we print a zero and that's what creates this scrolling stream of numbers in the shell and our plot. Now, the waspressed attribute checks if the button was pressed since the last time you checked. That's it's only looking for that actual press event. This is what's filtering out that.

We held an event to change this to "is the button pressed?". Right now, we don't care about the transition, we just want to know the state of the button. So, here we have our scrolling zeros and if I pulse the button, we get that pulse. But if I press and hold now, we get a constant value because the button is currently pressed.

Okay, so we can tell if our button was pressed or is pressed. But let's do something real with that. Let's control something about our Pico and without introducing any other Hardware to the system, we can just use the onboard LED that's on the Pico. Let's remix this code so that every time we press the button, it changes the flash rate of the LED. Maybe we have some pool of predefined flash rates and it will step to the next one in that pool, maybe from slow to fast or faster to slow. And when it reaches the end, it should wrap around to the beginning, so we can change that flash rate in a cycle. This could be used for like an LED lighting project to control some patterns.

Okay, so to control the LED, we'll need to import machine. From machine import pin. Pin is the class that's going to be used to control Hardware connected to pins like the LED. Now to initialize the LED, we can say that LED is pin. On a Raspberry Pi Pico, it's pin 25 and that's a pin dot out, so we're setting it to an output.

Now ignoring our button Logic for the moment, let's just create that flashing LED. We can do that by simply calling LED dot toggle. Every time this line is called, the LED will move to the state that it wasn't on, to off, off to on. And then we have our sleep for 100 milliseconds that's actually going to set our flash rate. Let's just prove that we can get a flashing LED for now. I'll press Ctrl R to run and we have our LED blinking with a 100 millisecond delay between.

We have our LED flashing at a constant rate now. Let's select from a pool of flash rates. I'll create a list of delays and we'll start with 100 milliseconds, which is what we have down here right now. Maybe we'll choose 200, 500, and 1000 milliseconds. We'll also need an index to step through this list and we'll start that off as zero.

So now instead of calling sleepMs for 100 milliseconds, we can call delays, and because index is currently set to zero, it's going to be the same thing. It's going to select 100 milliseconds, but if we update index when we press the button, it will step through this list and select a different delay.

And that's exactly what we'll do next. If a button was pressed, we'll increment index. Index += 1. We don't need this else statement and we want something to wrap back to the start. We don't want index to go beyond the length of the list, so we can just say index = index % the length of the delays. This will loop back to zero.

While we're at it, we'll print a little bit of debugging. We'll print the index. Index is equal to one because I pressed the button in between running this code. So we have index equal to one and I click the button, we have index equal to two and our flash speed is even slower. Click the button again, index is equal to three now it's really slow. This will be the thousand millisecond delay. When I click it again, we should go back to zero. There it is, we have index zero and we're flashing with that 100 millisecond delay. Nice!

So we've remixed our example code into a real control project. We're using this button to actually do something useful.

It is now possible to add multiple buttons to a project, the only requirement being that they each have a unique ID switch setting. When we set up our first button, all the ID switches were off, which is the default setting, so we don't need to pass any information into our initialization function. I'm going to include a second switch and before I do, I need to set an ID switch setting. I'll use a pen to set the first ID switch to on and then add that to the daisy chain.

Now to read the second button in code, I can just initialize another button. This one I'll call Button B = PikativeSwitch and because we set the first ID switch, we have to include that information. We have to include all four ID switch positions; we have the first one on and the remaining three are off, so that's all zeros. We've got two instances of a PiicoDev switch in our code. For completeness, I'll call the first one Button A so we don't get confused and update that as well.

Now we can use one of our buttons to count up and the other to count down. If Button A was pressed, we increment the index. If Button B was pressed, we can decrement the index. Index is minus equal one and I'll just move both of these outside any if statements just for simplicity. So now we have Button A incrementing the index and Button B decrementing the index.

I run my script and we have index 0 being printed to the shell. If I click my increment button, we get one, two, three. And now the moment of truth, I click my decrement button and we go down to two, down to one, and down to zero. How good! Very easy to include that second button, we just had to initialize another instance in our code and because of the way this modulo arithmetic works, if I decrement down past to zero, it actually does wrap around the other way.

We wrapped up our multiple button project by expanding upon our remix code. If you want a simpler example, there is just an up down counter example in the other examples section under the multiple buttons tab. This does a very similar job except we just have a counter being incremented and decremented. There's no LED in the mix to confuse things.

We connected a button to our Raspberry Pi Pico and were able to measure if the button was pressed or if the button is currently pressed. We even remixed that example code to control some real physical hardware. Then we introduced a second button to expand our features.

If you make anything cool from this startup project or just have some questions, let us know in our forums. We're full-time makers and happy to help. Until next time, happy making!

Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.