Using External Buttons

One final iteration before we build something physical will replace touch input with large external buttons. This is where we step beyond just the Circuit Playground Express and add those alligator clip wires and arcade buttons that were listed in the parts list. The diagram below shows how to connect the buttons. Use the alligator clip wires to do this.

The code is below. Copy it to code.py on your Circuit Playground Express. A few things change due to using external buttons. The first is the import from the digitalio module:

from digitalio import DigitalInOut, Direction, Pull

the next is initialization of the input connections:

button_a = DigitalInOut(board.A4)
button_a.direction = Direction.INPUT
button_a.pull = Pull.UP

button_b = DigitalInOut(board.A3)
button_b.direction = Direction.INPUT
button_b.pull = Pull.UP

The three lines for each button:

  1. create the interface object for a specific I/O pad on the Circuit Playground Express
  2. set it to input (as opposed to using it for output), and
  3. enable the internal pullup resistor. The pullup keeps the input HIGH when the button isn't pressed.

Not all microcontrollers have an internal pulldown resistor so the standard has become to use a pullup and have the button connect the input to ground when it's pushed. You can see this in the wiring diagram: one side of each button is connected to ground.

Notice that this change is limited to changing the two touch functions to use digital inputs in place of touch inputs; the main loop didn't change in this iteration.

"""
Circuit Playground Express GBoard: arcade buttons generating keycodes

Adafruit invests time and resources providing this open source code.
Please support Adafruit and open source hardware by purchasing
products from Adafruit!

Written by Dave Astels for Adafruit Industries
Copyright (c) 2018 Adafruit Industries
Licensed under the MIT license.

All text above must be included in any redistribution.
"""

import board
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode
from digitalio import DigitalInOut, Direction, Pull

DOT_DURATION = 0.25
DASH_DURATION = 0.5

button_a = DigitalInOut(board.A4)
button_a.direction = Direction.INPUT
button_a.pull = Pull.UP

button_b = DigitalInOut(board.A3)
button_b.direction = Direction.INPUT
button_b.pull = Pull.UP

kbd = Keyboard()


def touch_a():
    return not button_a.value


def touch_b():
    return not button_b.value


while True:
    if touch_a():
        kbd.send(Keycode.PERIOD)
        while touch_a():
            pass
    elif touch_b():
        kbd.send(Keycode.MINUS)
        while touch_b():
            pass

As before, open up an app that accepts text. It can be a word processor like Google Docs or just be a text input box like the SMS/phone message app or a Twitter message input box.

Now, when you press the button connected to A4 a dot will be sent to the app, and when you press the button connected to A3 a dash will be.

This guide was first published on Jul 24, 2018. It was last updated on Jul 24, 2018. This page (Using External Buttons) was last updated on Nov 19, 2019.