Skip to content

Latest commit

 

History

History
120 lines (79 loc) · 7.09 KB

File metadata and controls

120 lines (79 loc) · 7.09 KB
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

{{ page.title | replace_first:'L','Lesson '}}

{: .no_toc }

Table of Contents

{: .no_toc .text-delta }

  1. TOC {:toc}

<iframe width="100%" src="https://www.youtube.com/embed/raIc-EuHfmc?si=-KCgO3ypF9kPKBVd" title="Overview of digital input on the Circuit Playground Express" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>
**Video.** By the end of this lesson, you'll wire external buttons to the CPX—and understand the floating-pin problem and how pull-up and pull-down resistors solve it. {: .fs-1 }

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.

<iframe width="100%" src="https://www.youtube.com/embed/mFXvAfsiRx0?si=_6ks-_M8sWD0XlSQ" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>

{: .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.

Circuit Diagrams

Circuit diagrams showing a button wired with an external pull-down resistor and with an external pull-up resistor on the CPX

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.

<iframe width="100%" src="https://www.youtube.com/embed/JT4sQ72HJAM?si=ib8fGLSrsdvz5T-u" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>

Circuit Diagrams

Circuit diagram for a button using the CPX's internal pull-up resistor configuration

Circuit diagram for a button using the CPX's internal pull-down resistor configuration

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.

<iframe width="100%" src="https://www.youtube.com/embed/nExLP211ZUA?si=5_z8wM0QDisPTTo8" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>

Circuit Diagrams

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

Circuit diagram wiring an arcade button to the CPX using an external pull-down resistor

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

Circuit diagram wiring an arcade button to the CPX using an internal pull-down resistor, without the embedded LED

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

Circuit diagram wiring an arcade button with its embedded LED to the CPX using an internal pull-down resistor

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 Lesson
Analog Input