Lesson 9: Digital Input

Table of Contents

  1. Lesson 8.1: Overview of Digital Input
  2. Lesson 8.2: Hooking up Buttons to the CPX & Why Pull-down Resistors?
    1. Circuit Diagrams
    2. Code
  3. Lesson 8.3: Using Internal Pull-up & Pull-down Resistors
    1. Circuit Diagrams
  4. Lesson 8.4: Hooking up Arcade Buttons to the CPX
    1. Circuit Diagrams
    2. Code
  5. Advanced Code
  6. Resources
  7. Previous Lesson

In Lesson 9, we continue exploring how to hook up and use external electronics with our CPX. In Lesson 8, we worked with analog input—which converts voltage signals from 0 - 3.3V to 0 - 1023. In Lesson 9, we’ll work with digital input, which converts voltage input signals to either ON (1) or OFF (0). This is useful for components like buttons.

Lesson 8.1: Overview of Digital Input

In this lesson, we learn about what is digital input and how to use it on the Circuit Playground Express (CPX). We begin similarly to our analog input lessons: introducing the 3.3V, GND, and A1 CPX connection pads and showing how the digital read function responds to different input voltages (e.g., 3.3V, GND).

Lesson 8.2: Hooking up Buttons to the CPX & Why Pull-down Resistors?

In this video, we introduce a solution to the “floating pin” problem using pull-down resistors and hook up our first button to the CPX.

If you want to skip all the theory about why pull-down resistors and just “follow the recipe” for hooking up a button, skip to 13:54 in our video. You can learn more about the “floating pin problem” and more in Arduino L1: Using Buttons.

Circuit Diagrams

Code

  • Example MakeCode for Pull-down Resistor. In the pull-down configuration, the default input to A1 is pulled down to 0V. Then, when the button is pressed, the A1 input goes to 3.3V. So, we turn on the NeoPixels when A1 goes to 3.3V (i.e., when the button is pressed).

  • Example MakeCode for Pull-up Resistor. In the pull-up configuration, the default input to A1 is pulled up to 3.3V. Then, when the button is pressed, the A1 input goes to 0V. So, we turn on the NeoPixels when A1 goes to 0V (i.e., when the button is pressed).

Lesson 8.3: Using Internal Pull-up & Pull-down Resistors

In this video, we introduce external pull-up resistors and then using internal pull-up and pull-down resistors on the CPX with MakeCode.

Circuit Diagrams

Lesson 8.4: Hooking up Arcade Buttons to the CPX

In this video, we show how to hook up arcade buttons to the CPX, which have cool embedded LEDs that we can turn on/off. We’ll show how to wire up the arcade button with without and with the embedded LED and, along the way, learn a bit about digital write.

Circuit Diagrams

Using an external pull-down resistor with the arcade button.

Using an internal pull-down resistor with the arcade button (but not hooking up the internal LED).

Hooking up internal LED. Here’s example code that turns on the internal LED when the button is pressed.

Code

  • Example Arcade Button Code. Example arcade button code for an internal pull-down resistor configuration that turns on the NeoPixels with a button press and the internal LED on the button itself (using digital write)

Advanced Code

  • Debouncing Buttons in MakeCode. Note: I haven’t really found the need to debounce buttons with MakeCode and CPX but if you’re getting unexpected multiple button presses when using a button, try this out. For more on debouncing, see our Debouncing Lesson.

Resources

Here are some additional resources:

Previous Lesson

Previous: Analog Input


This website was developed by Professor Jon E. Froehlich and the Makeability Lab using Just the Docs. If you found the website useful or use it in your teaching, we'd love to hear from you: jonf@cs.uw.edu. This website and all code is open source (website GitHub, Arduino GitHub, p5js GitHub). You can find the MakeabilityLab_Arduino_Library here. Found an error? File a GitHub Issue.