Skip to content

Latest commit

 

History

History
174 lines (107 loc) · 10.4 KB

File metadata and controls

174 lines (107 loc) · 10.4 KB
layout default
title L8: Analog Input
description Connect external sensors to the CPX with analog input. Read potentiometers, FSRs, and flex sensors, learn voltage dividers, and even build a lo-fi slide pot from paper and pencil.
image https://img.youtube.com/vi/S6X4Y5gfekc/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/S6X4Y5gfekc?si=RysXDQ5unRU7n79H" title="Overview of analog input on the Circuit Playground Express" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>
**Video.** In this lesson, we'll connect *external* analog sensors to the CPX—potentiometers, FSRs, flex sensors, and even a lo-fi slide potentiometer made from paper and a pencil. {: .fs-1 }

In Lesson 8, we move beyond working with internal sensors on the CPX (light 💡, microphone 🔊, accelerometer 🍎) and show how to connect external sensors using analog input.

Lesson 8.1: Overview of Analog Input

In this lesson, we introduce analog input on the Circuit Playground Express (CPX) primarily focusing on variable resistive sensors like rotary potentiometers, slide potentiometers, force-sensitive resistors (FSRs), flex sensors (Nintendo Power Glove!), softpot position sensors, and more!

Code

Lesson 8.2: What is Analog Input?

In this lesson, we learn more about what is analog input and how to use it on the CPX. We begin by introducing the 3.3V, GND, and A1 CPX connection pads and show how the function analog read responds to different voltages (e.g., 3.3V, GND). We then introduce the concept of dividing voltages via fixed resistors before building a lo-fi slide potentiometer using paper and a 2B pencil, which divides voltage dynamically.

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

Circuit Diagrams

We use the following circuit diagrams in the above video.

Begin by connecting a red alligator clip wire to 3.3V, a black wire to GND (0V), and a yellow wire to A1. While it's not necessary to follow these color-coded conventions, I strongly encourage you to always reserve red for connecting to your voltage source (in this case, 3.3V) and black for for GND. This will make it easier to debug your circuits.

{: .warning } Never touch the black wire (GND) to the red wire (3.3V) directly. If you do this, you could harm the CPX or your USB connection on your laptop. In practice, I've found that the CPX has internal protection circuitry that resets your board on a short (and your laptop may also observe that an amperage draw is too high and disconnect).

An image of the basic analog input circuit with a red wire connected to 3.3V, a black wire to GND, and a yellow wire to A1

Now, with this program running on the CPX, open up your "Device Console" and observe the waveform. Try touching the yellow wire to the red wire. You should see the graph jump to 1023 (the max value).

Touching the yellow wire to red

Now try touching the yellow wire to black. What happens? You should see the graph jump down to 0 (the min value).

Touching the yellow wire to black

Optional Theory

{: .note } It's OK if you don't understand exactly how voltage dividers work. The key is in developing a bit of intuition about how the CPX analog read function works and what physical phenomena it is reading on A1 (it's voltage!). Any sensor that can manipulate voltage can be used as input. If you want to learn more about potentiometers and voltage dividers, see our Arduino L4: Potentiometer lesson and Electronics L4: Voltage Dividers lesson

In the above examples, we showed how to read 3.3V and GND (0V) with the CPX by touching the yellow wire to the red and black respectively and graphing the result. But analog input is far more interesting than just two numbers: 0 and 1023. How can we play with everything in between?

We can do this by dividing the input voltage using resistors, which we cover in great detail in our Electronics L4: Voltage Divider lesson and touch on it a bit in the above video. We need not repeat ourselves here; however, if you want to experiment with manually controlling different input voltages on A1, try adding different resistor combinations like the following.

Intro to using a voltage divider with two resistors to control the input voltage on CPX pin A1

The key is not the raw resistor values themselves but rather the ratio between them. With equal resistors—in this case 330Ω—the 3.3V will be divided equally to 1.65V (and converted to 512 by the CPX).

Voltage divider on the CPX with two equal 330-ohm resistors splitting 3.3V to 1.65V on A1

Similarly, if we add a pair of 470 resistors, the 3.3V will again be divided equally to 1.65V (and again converted to 512 by the CPX)

Voltage divider on the CPX with two equal 470-ohm resistors splitting 3.3V to 1.65V on A1

But if we change that ratio, we can get different input voltages on A1.

Voltage divider on the CPX with unequal 1k and 2k resistors producing a different input voltage on A1

Lesson 8.3: Hooking up Resistive Sensors

In this lesson, we learn how to hook up three-legged variable resistors like rotary and slide potentiometers as well as two-legged variable resistors like force-sensitive resistors, photosensitive resistors, and flex sensors.

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

Circuit Diagrams

Below, we provide circuit diagrams covered in the video. You can right-click and open up each image in a new tab to enlarge.

You can test any of these circuits using this basic MakeCode, which simply graphs the output on A1 and lights up the corresponding NeoPixels (like a circular bargraph).

Rotary Potentiometer

Hooking up a rotary potentiometer

Circuit diagram for wiring a single rotary potentiometer to analog pin A1 on the CPX

Hooking up two rotary potentiometers (try out this code!):

Circuit diagram for wiring two rotary potentiometers to analog pins A1 and A2 on the CPX

Slide Potentiometer

Circuit diagram for wiring a slide potentiometer to analog pin A1 on the CPX

Pressure Sensor

You might be compelled to hook up your two-legged resistive sensors like pressure sensors, flex sensors, photosensitive resistors like this... but don't do this! It's wrong!

Incorrect circuit diagram wiring a two-legged pressure sensor directly to A1 without a divider resistor

Instead, you need to add in an additional resistor to turn the circuit into a voltage divider where one of the resistors is your sensor, which dynamically changes its resistance.

Correct circuit diagram wiring a pressure sensor with a fixed resistor as a voltage divider into A1

Photosensitive Resistor

Circuit diagram wiring a photosensitive resistor with a fixed resistor as a voltage divider into A1

Flex Sensor

Circuit diagram wiring a flex sensor with a fixed resistor as a voltage divider into A1 on the CPX

Code

Advanced Code

Here is some more advanced code:

Next Lesson

In the next lesson, we'll learn how to use digital input like buttons!

← Previous Lesson
CPX as a Mouse
Next Lesson →
Digital Input