In our last tutorial, we walked you through how to expand the output pins of the Adafruit Qt Py by using a 74HC595 Shift Register. In this tutorial, we'll help you take it a step further by adding a momentary button and a few extra lines of code to implemented a push button counter.
If you haven't seen the first tutorial, you can find it here: https://www.oakdev.tech/tutorials-new-products/tutorial-expanding-qt-py-outputs-with-74hc595
What you'll need:
To get started, you'll need the following parts:
Getting everything wired up:
To get started, we first want to wire up our board. Connect your circuit just like the wiring diagram below. See the wiring steps if you need help.
You'll want to make sure your CircuitPython board is ready to use with the CircuitPython .uf2 installed. If you need to, reference the Getting Started With Circuit Python link at the top of the article.
Some background of the code we're going to be using.
Since we are using a momentary push button switch or a tactile switch, there is what is called "bounce." This sounds like what it is. When you push the button and release, it's not a smooth on and off contact for the button. This can provide additional inputs to the pin on the Qt Py. To combat this, we can add some debouncing in our code to make sure this isn't an issue. You'll see a sleep timer called in the button press counter code which makes sure that over a 0.5 second period, we only register 1 button press. If you hold the button down, you will see it update the seven segment display every one half second.
Let's get started coding.
First we are going to open the MU Editor. If your board is ready to use with Circuit Python, you should get no popups warning you about your board not being connected. For the sake of this post, we've included the code to display up to the number 5. You can easily expand this for 0 to 15 (F). In the editor window, we are going to paste the following code:
After you save the code as "code.py" on your Qt Py, you can open up the serial window and begin to press the button on your breadboard! Each button press will increment the number by one with each button press. You can see the number increase in the console!
Just like in our last tutorial, this use of the shift register is just one of many for expanding outputs on smaller development boards. You can drive a large number of devices, and even hook multiple shift registers together to create even larger output pin options.
Thanks for working through this tutorial with us. Comment if you've done this tutorial or need help. As always, share on social media and let us know what tutorials you want to see!