| layout | default |
|---|---|
| title | L9: Digital Input |
| description | Read ON/OFF signals on the CPX with digital input. Wire up external buttons, tackle the floating-pin problem with pull-up and pull-down resistors, and use internal resistors in MakeCode. |
| image | https://img.youtube.com/vi/raIc-EuHfmc/hqdefault.jpg |
| parent | Circuit Playground Express |
| has_toc | true |
| comments | true |
| nav_exclude | false |
| usetocbot | true |
{: .no_toc }
{: .no_toc .text-delta }
- TOC {:toc}
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.
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).
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.
{: .note } 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.
-
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).
In this video, we introduce external pull-up resistors and then using internal pull-up and pull-down resistors on the CPX with MakeCode.
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.
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.
- 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)
- 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.
Here are some additional resources:
- MakeCode Pins
- MakeCode's Digital Read Function
- MakeCode Input States
- Adafruit MakeCode Learning: Digital Input
- Using Digital Input on the CPX with Arduino C/C++





