Oak Development Technologies
  • IcyBlue
  • Oak Blog
  • Prototyping
  • Livestream
  • Open Source Projects
  • Contact
Tutorials, Announcements, and More from...

Oak Development 
​Technologies

Connect with us on Facebook, Twitter & Instagram!

(Tutorial) Pushing Buttons And Pushing Numbers - Qt Py + Shift Register + Buttons

10/10/2020

0 Comments

 
Picture
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:
  1. CircuitPython compatible development board (QT PY as used in this tutorial)
  2. USB-C data cable
  3. 1x 74HC595 Serial Shift Register
  4. 1x 5161AS-1 7 Segment Display
  5. Momentary Buttons for Breadboards (Like these on Adafruit)
  6. A 1Kohm Resistor
  7. A 10kohm Resistor
  8. A solderless breadboard
  9. At least 17 jumper wires
  10. Mu Editor (Get that HERE)

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.
Picture
Wiring Steps:
  1. Connect the 5V pin on the Qt Py to the positive (+) rail of your breadboard.
  2. Connect the GND pin on the Qt Py to the negative (-) rail of your breadboard.
  3. Connect the VCC pin (pin 16) on the 74HC595 to the positive (+) rail of your breadboard.
  4. Connect the GND pin on the 74HC595 to the negative (-) rail of your breadboard.
  5. Connect the SCK/D8 pin on the Qt Py to pin 14 on the 74HC595.
  6. Connect the MI/D9 pin on the Qt Py to pin 11 on the 74HC595.
  7. Connect the MO/D10 pin on the Qt Py to pin 12 on the 74HC595.
  8. Connect pin 15 on the 74HC595 to pin 7 on the seven segment display.
  9. Connect pin 1 on the 74HC595 to pin 6 on the seven segment display.
  10. Connect pin 2 on the 74HC595 to pin 4 on the seven segment display.
  11. Connect pin 3 on the 74HC595 to pin 2 on the seven segment display.
  12. Connect pin 4 on the 74HC595 to pin 1 on the seven segment display.
  13. Connect pin 5 on the 74HC595 to pin 9 on the seven segment display.
  14. Connect pin 6 on the 74HC595 to pin 10 on the seven segment display.
  15. Connect the 1KOhm resistor to the negative (-) rail and pin 8 of the seven segment display.
  16. Connect the 10KOhm resistor to the top right pin of the button and the negative (-) rail of your breadboard.
  17. Connect the top right pin of the button to the RX/D7 pin of the Qt Py.
  18. Connect the top left pin of the button to the 3.3V pin of the Qt Py.

The Code:

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:

# Circuit Python Code
# Seven Segment + Button code

# Shift register 7 segment display code
from digitalio import DigitalInOut
from digitalio import Direction
import time
import board

btnPin = DigitalInOut(board.D7)
regDisplay = DigitalInOut(board.D10)
regClk = DigitalInOut(board.D9)
regOut = DigitalInOut(board.D8)
regOut.direction = Direction.OUTPUT
regClk.direction = Direction.OUTPUT
regDisplay.direction = Direction.OUTPUT
btnPin.direction = Direction.INPUT
# numer | 0 | g | f | e | d | c | b | a 
# zero  | 0 | 0 | 1 | 1 | 1 | 1 | 1 | 1 == 0x3F
# one   | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 0 == 0x06
# two   | 0 | 1 | 0 | 1 | 1 | 0 | 1 | 1 == 0x5B
# three | 0 | 1 | 0 | 0 | 1 | 1 | 1 | 1 == 0x4F
# four  | 0 | 1 | 1 | 0 | 0 | 1 | 1 | 0 == 0x66
# five  | 0 | 1 | 1 | 0 | 1 | 1 | 0 | 1 == 0x6D
# six   | 0 | 1 | 1 | 1 | 1 | 1 | 0 | 1 == 0x7D
# seven | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 == 0x07
# eight | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 == 0x7F
# nine  | 0 | 1 | 1 | 0 | 0 | 1 | 1 | 1 == 0x67
# A     | 0 | 1 | 1 | 1 | 0 | 1 | 1 | 1 == 0x77
# B     | 0 | 1 | 1 | 1 | 1 | 1 | 0 | 0 == 0x7C
# C     | 0 | 0 | 1 | 1 | 1 | 0 | 0 | 1 == 0x39
# D     | 0 | 1 | 0 | 1 | 1 | 1 | 1 | 0 == 0x5E
# E     | 0 | 1 | 1 | 1 | 1 | 0 | 0 | 1 == 0x79
# F     | 0 | 1 | 1 | 1 | 0 | 0 | 0 | 1 == 0x71
zero = [0, 0, 1, 1, 1, 1, 1, 1]#0x3F
one = [0, 0, 0, 0, 0, 1, 1, 0]#0x06
two = [0, 1, 0, 1, 1, 0, 1, 1]#0x5B
three = [0, 1, 0, 0, 1, 1, 1, 1]#0x4F
four = [0, 1, 1, 0, 0, 1, 1, 0]#0x66
five = [0, 1, 1, 0, 1, 1, 0, 1]#0x6D
six = [0, 1, 1, 1, 1, 1, 0, 1]#0x7D
seven = [0, 0, 0, 0, 0, 1, 1, 1]#0x07
eight = [0, 1, 1, 1, 1, 1, 1, 1]#0x7F
nine = [0, 1, 1, 0, 0, 1, 1, 1]#0x67
A = [0, 1, 1, 1, 0, 1, 1, 1]#0x77
B = [0, 1, 1, 1, 1, 1, 0, 0]#0x7C
C = [0, 0, 1, 1, 1, 0, 0, 1]#0x39
D = [0, 1, 0, 1, 1, 1, 1, 0]#0x5E
E = [0, 1, 1, 1, 1, 0, 0, 1]#0x79
F = [0, 1, 1, 1, 0, 0, 0, 1]#0x71

def updateDisplay(num):
    if num == 0:
        for i in zero:
            regOut.value = i
            regClk.value = True
            time.sleep(0.01)
            regClk.value = False
    if num == 1:
        for i in one:
            regOut.value = i
            regClk.value = True
            time.sleep(0.01)
            regClk.value = False
    if num == 2:
        for i in two:
            regOut.value = i
            regClk.value = True
            time.sleep(0.01)
            regClk.value = False
    if num == 3:
        for i in three:
            regOut.value = i
            regClk.value = True
            time.sleep(0.01)
            regClk.value = False
    if num == 4:
        for i in four:
            regOut.value = i
            regClk.value = True
            time.sleep(0.01)
            regClk.value = False
    if num == 5:
        for i in five:
            regOut.value = i
            regClk.value = True
            time.sleep(0.01)
            regClk.value = False

    regDisplay.value = True
    time.sleep(0.1)
    regDisplay.value = False
    
def btnCount(btn,number):
    if number >= 5:
        number = 0
        print(number)
        time.sleep(0.5)
    else:
        number+=1
        print(number)
        time.sleep(0.5) # debounces the switch a little bit :)
    return number

#updateDisplay(0)
number = 0
updateDisplay(number)
while True:
    if btnPin.value is True:
        number = btnCount(btnPin.value,number)
        updateDisplay(number)


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!
Picture
Picture

Final Thoughts

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!

​-Seth
0 Comments



Leave a Reply.

    Authors

    Seth is embedded software engineer and open source hardware developer. 

    Archives

    May 2021
    April 2021
    January 2021
    November 2020
    October 2020
    September 2020

    Categories

    All
    New Products
    Product Spotlight
    Tutorials

    RSS Feed

Powered by Create your own unique website with customizable templates.
  • IcyBlue
  • Oak Blog
  • Prototyping
  • Livestream
  • Open Source Projects
  • Contact