# Circuit Playground Analog Input

## Overview

![](https://cdn-learn.adafruit.com/assets/assets/000/038/964/medium800/circuit_playground_signal_banner.jpg?1486595161)

In this guide we will use our Circuit Playground along with a potentiometer and some alligator clips to explore how we can read analog signals into our digital computer.

**No soldering required!**

# Required Parts

In addition to the Circuit Playground, you will need some alligator clips and a potentiometer. While not required, a knob makes turning the potentiometer easier and provides a nice reference mark to see where you are at.

- [Circuit Playground](https://www.adafruit.com/products/3000)
- [Alligator Clip Test Leads](https://www.adafruit.com/products/1008)
- [Panel Mount 10K Potentiometer](https://www.adafruit.com/products/562)
- [Knob](https://www.adafruit.com/products/2057)

![circuit_playground_items.jpg](https://cdn-learn.adafruit.com/assets/assets/000/038/946/medium640/circuit_playground_items.jpg?1486514449)

# Optional Parts

Another option is to use a [breadboard](https://www.adafruit.com/products/64) along with a [trim pot](https://www.adafruit.com/products/356) and [alligator to male jumper wires](https://www.adafruit.com/products/3255). The hardware outlined in the previous section is recommended, but if you already have these items, they will work as well.

# Before Starting

If you are new to the Circuit Playground, you may want to first read these overview guides.

- [Overview](../../../introducing-circuit-playground)
- [Lesson #0](../../../circuit-playground-lesson-number-0)

This project will use the Arduino IDE. Make sure you have added the board support for the Circuit Playground as well as installed the Circuit Playground library. **MUST DO BOTH.** This is covered in the guides linked above.

# Circuit Playground Analog Input

## Analog vs. Digital

# Analog World

Pretty much every phenomenon in the natural world is analog. What does "analog" mean? The key bits boil down to this phrase in the [definition for analog](https://www.merriam-webster.com/dictionary/analog):

> ...continuously variable physical quantities...

The "physical quantity" can be anything - temperature of an apple, pressure in a balloon, water level of some lake, total internal energy of the third moon of Omicron Persei 8, voltage between two points in an electrical circuit, etc.

To understand the "continuously variable" part, consider the plot of some physical quantity below as it happily marches along in time changing its value.

![](https://cdn-learn.adafruit.com/assets/assets/000/039/007/medium800/circuit_playground_signal1.jpg?1486688482)

If we were to zoom in on some part of the curve, we would again see a nice smooth variation. Zoom again, still the same. No matter how far we zoom in, we would see a nice smooth "continuous" variation in the value.

![](https://cdn-learn.adafruit.com/assets/assets/000/038/968/medium800/circuit_playground_signal2.jpg?1486599075)

Hurray for nature! But what about digital computers, like our Circuit Playground? Let's look at that next.

# Digital World

You've probably heard the phrase before, something like how everything in a computer is either a 1 or 0, either YES or NO, either ON or OFF, either HIGH or LOW, etc. This is entirely true for digital computers (yes, there are [analog computers](https://en.wikipedia.org/wiki/Analog_computer)), and the basic unit of information that stores that 1 or 0 is called a [bit](https://en.wikipedia.org/wiki/Bit). All information must somehow be represented by using one or more of these bits.

If we try to use just 1 bit, we don't get very far. It only allows us to represent two values.

![](https://cdn-learn.adafruit.com/assets/assets/000/039/008/medium800/circuit_playground_1bit.jpg?1486688540)

By simply adding a second bit, we can now represent up to 4 different values.

![](https://cdn-learn.adafruit.com/assets/assets/000/038/950/medium800/circuit_playground_2bit.jpg?1486516987)

The more bits we add, the more values we can represent. What about 10 bits?

![](https://cdn-learn.adafruit.com/assets/assets/000/038/951/medium800/circuit_playground_10bit.jpg?1486516994)

Here's a summary of how many values can be represented by using 1 to 10 bits. Note that the variation is not linear.

![](https://cdn-learn.adafruit.com/assets/assets/000/038/952/medium800/circuit_playground_bits_vs_values.jpg?1486517268)

# Analog World to Digital World

So how do we get those nice analog values into our digital computer so we can do cool things with the information? Simple - just use an Analog to Digital Converter (ADC). There's all kinds of technical mumbo jumbo for [how an ADC works](https://en.wikipedia.org/wiki/Analog-to-digital_converter), but for this guide it's good enough to just think of an ADC as a device where an analog signal goes in and a digital (1s and 0s) signal comes out.

To illustrate this, let's start by considering the gray scale range shown below - this is our analog signal. It is **continuously variable** (remember that from above?) from some MIN value up to some MAX value.

![](https://cdn-learn.adafruit.com/assets/assets/000/038/970/medium800/circuit_playground_digital_scale1.jpg?1486669607)

An ADC will take that and chop it up into discrete (digital) segments. ADCs come in different resolutions - the total number of bits they use. That's why you here people say things like "it has a 10bit ADC". The more bits, the more segments, and the better the ADC can represent (resolve) the analog values.

A 1 bit ADC would be pretty crude. All that beautiful grayness would just become black or white.

![](https://cdn-learn.adafruit.com/assets/assets/000/038/971/medium800/circuit_playground_digital_scale2.jpg?1486669947)

Let's add one more bit and see what happens. Here's what a 2 bit ADC would do.

![](https://cdn-learn.adafruit.com/assets/assets/000/038/972/medium800/circuit_playground_digital_scale3.jpg?1486670028)

That's much better. We can start to see some grayness. But it's still pretty crude. Let's add one more bit. Here's what a 3 bit ADC would do.

![](https://cdn-learn.adafruit.com/assets/assets/000/038/973/medium800/circuit_playground_digital_scale4.jpg?1486670073)

And that's even better.

You can see the trend here. The more bits we use, the closer we get to the original analog signal. If we could use an infinite number of bits, we'd actually get the original signal. But that's just not practical. No one's come up with a way store an infinite number of 1s and 0s on a digital computer.

If we were to feed the signal we introduced at the beginning into a 3 bit ADC, it would get turned into something like shown below. The orange (analog) signal becomes the black (digital) signal.

![](https://cdn-learn.adafruit.com/assets/assets/000/039/006/medium800/circuit_playground_signal3.jpg?1486688468)

# How Many Bits?

So why not just use the biggest most bit-est ADC there is for everything? The answer is cost. The ADC hardware will generally cost more, but there is also the consideration of storage cost. Even if money didn't matter and you could buy a 900000000bit ADC, do you have a place to store all those 1 and 0s? The speed of the ADC conversion is another consideration. Hey. Trade offs.

So you wave your engineering magic wand and pick the ADC that is best suited for your application. Or, you just work with what you've been given. In the case of the Circuit Playground, that's a 10bit ADC.

# Circuit Playground Analog Input

## Circuit Hookup

Alright. Fun time.

In order to create an analog signal we can control, we will connect a potentiometer to the Circuit Playground. By connecting the ends of the potentiometer to the 3.3V and GND there will be a variable voltage on the middle (sweep) pin.

Info: 

# Using Panel Mount Potentiometer
This is the overall view of the hookup.

- RED to 3.3V
- YELLOW to #10
- BLACK to GND

![circuit_playground_hookup_alligator.jpg](https://cdn-learn.adafruit.com/assets/assets/000/038/955/medium640/circuit_playground_hookup_alligator.jpg?1486591778)

Simply connect the alligator clips to the gold pads on the edge of the Circuit Playground as shown.

![circuit_playground_hookup_alligator3.jpg](https://cdn-learn.adafruit.com/assets/assets/000/038/956/medium640/circuit_playground_hookup_alligator3.jpg?1486591852)

The alligator clips should clamp nicely onto the leads of the potentiometer as shown.

![circuit_playground_hookup_alligator2.jpg](https://cdn-learn.adafruit.com/assets/assets/000/038/957/medium640/circuit_playground_hookup_alligator2.jpg?1486591918)

With this hookup, the voltage on the yellow (OUT) will vary with knob position as shown.

![circuit_playground_pot_knob.jpg](https://cdn-learn.adafruit.com/assets/assets/000/038/960/medium640/circuit_playground_pot_knob.jpg?1486593391)

# Using Breadboard Trim Pot

The test circuit can also be setup on a breadboard as shown below. You can go this route if you want, like if you already have some of these parts, but the knob on the panel mount potentiometer is bigger and easier to work with.

This is the overall view of the hookup.

- RED to 3.3V
- YELLOW to #10
- BLACK to GND

![circuit_playground_hookup_breadboard.jpg](https://cdn-learn.adafruit.com/assets/assets/000/038/958/medium640/circuit_playground_hookup_breadboard.jpg?1486592894)

# Circuit Playground Analog Input

## Hello Analog

To read the value from the analog input, we will use the good 'ole Arduino library function [analogRead()](https://www.arduino.cc/en/Reference/analogRead). You can use the sketch below to read the value from the potentiometer (attached to #10) and print it on the serial output.

```auto
///////////////////////////////////////////////////////////////////////////////
// Circuit Playground Analog In - Serial Print
//
// Author: Carter Nelson
// MIT License (https://opensource.org/licenses/MIT)

#include <Adafruit_CircuitPlayground.h>

uint16_t value;

///////////////////////////////////////////////////////////////////////////////
void setup() {
  Serial.begin(9600);
  CircuitPlayground.begin();
}

///////////////////////////////////////////////////////////////////////////////
void loop() {
  value = analogRead(10);
  Serial.println(value);
  delay(100);
}
```

With this sketch running on the Circuit Playground, open the Serial Monitor:

**Tools -\> Serial Monitor**

You should see the value being printed out. Now try spinning the knob and you should see the value change.

![](https://cdn-learn.adafruit.com/assets/assets/000/038/961/medium800/circuit_playground_analogin_mon.png?1486593971)

If you turn the knob all the way in one direction, you should get a value of 0. If you turn the knob all the way in the other direction, you should get a value of 1023. This is the range of values provided by the 10 bit analog to digital converter.

You can also use the Serial Plotter to watch the value change.

**Tools -\> Serial Plotter**

Now try turning the knob and watch the line go up and down. Wheeeee!!!!

![](https://cdn-learn.adafruit.com/assets/assets/000/038/962/medium800/circuit_playground_analogin_plot.png?1486594127)

# Circuit Playground Analog Input

## NeoPixel Fun

Now let's take the analog value and do something fun with it. How about turning on the NeoPixels to represent knob position? Like all NeoPixels off is one direction, and all NeoPixels on is the other direction.

We can do this pretty easy using the [map](https://www.arduino.cc/en/Reference/Map) function. We know the range of values we expect from the analog input, 0-1023, and we just want to map that to how many NeoPixels to light up, 0-10. So, it's just a matter of coding it up.

And here it is.

```auto
///////////////////////////////////////////////////////////////////////////////
// Circuit Playground Analog In - NeoPixel Fun
//
// Author: Carter Nelson
// MIT License (https://opensource.org/licenses/MIT)

#include <Adafruit_CircuitPlayground.h>

uint16_t value;
uint8_t pixels;

///////////////////////////////////////////////////////////////////////////////
void setup() {
  Serial.begin(9600);
  CircuitPlayground.begin();
}

///////////////////////////////////////////////////////////////////////////////
void loop() {
  value = analogRead(10);
  pixels = map(value, 0, 1023, 0, 10);

  CircuitPlayground.clearPixels();
  for (int p=0; p<pixels; p++) {
    CircuitPlayground.setPixelColor(p, 0xFF00FF);
  }
  delay(100);
}
```

With this code loaded and running on the Circuit Playground, try turning the knob. You should see the NeoPixels light up in accordance with the knob position.

![](https://cdn-learn.adafruit.com/assets/assets/000/038/963/medium800thumb/circuit_playground_pot_demo_small.jpg?1486594941)

# Circuit Playground Analog Input

## Speaker Fun

Oooooo. What about having the knob change the tone of the speaker? No prob. Same idea as the previous sketch. Just map the knob values to tone frequencies.

The Circuit Playground speaker is really only usable over a range of about 100Hz to 10000Hz, which leads us to the following code.

```auto
///////////////////////////////////////////////////////////////////////////////
// Circuit Playground Analog In - Speaker Fun ?
//
// Author: Carter Nelson
// MIT License (https://opensource.org/licenses/MIT)

#include <Adafruit_CircuitPlayground.h>

uint16_t value;
uint16_t freq;

///////////////////////////////////////////////////////////////////////////////
void setup() {
  Serial.begin(9600);
  CircuitPlayground.begin();
}

///////////////////////////////////////////////////////////////////////////////
void loop() {
  value = analogRead(10);
  freq = map(value, 0, 1023, 100, 10000);

  CircuitPlayground.playTone(freq, 100, false);
}
```

Load that code up and run it. Spin the knob. Make some weird outer space [theremin](https://en.wikipedia.org/wiki/Theremin) like music sounds. Hopefully the dogs don't start howling.

# Circuit Playground Analog Input

## Under The Hood

So you might have noticed we didn't use one of those `CircuitPlayground.something()` functions to get our analog value. Why not, and how did it work without doing so?

The answer to the second question is pretty easy - we just use the Arduino library. The Circuit Playground library is built (largely) on top of the Arduino library and 'hides' the details of using its various bells and whistles. However, you can still use the Arduino library if you want (and know what you're doing).

No real good answer to the other question. This could've been done, say with something like `CircuitPlayground.analogRead()` or `CircuitPlayground.giveMeTheAnalogPlease()` or whatever. But this would have likely done nothing more than just call the same Arduino function. So, guess it just never made the cut.

However, there are a few of the Circuit Playground sensors that are essentially the same as the potentiometer we used in this guide, namely the [light sensor](../../../../circuit-playground-lesson-number-0/light-sensor) , [sound sensor](../../../../circuit-playground-lesson-number-0/sound-sensor) , and [temperature sensor](../../../../circuit-playground-lesson-number-0/temperature-sensor). They all output a voltage that varies with the physical quantity they sense. In order for this to be used in the Circuit Playground, those signals are fed into ADCs, just like we did with the potentiometer. And that's pretty much all there is to it.

Don't believe me? Well, take a look...

# Light Sensor

Here's the circuit path for the output of the light sensor. The big red square that says 'THERMAL' is the [brain of the Circuit Playground](../../../../circuit-playground-lesson-number-0/microcontroller).

![](https://cdn-learn.adafruit.com/assets/assets/000/038/965/medium800/circuit_playground_lightsensor_out.jpg?1486598198)

And here's the code for the `CircuitPlayground.lightSensor()` function. ([direct link to repo](https://github.com/adafruit/Adafruit_CircuitPlayground/blob/master/Adafruit_CircuitPlayground.cpp#L88))

```auto
uint16_t Adafruit_CircuitPlayground::lightSensor(void) {
  return analogRead(CPLAY_LIGHTSENSOR);
}
```

# Sound Sensor

Here's the circuit path for the output of the soud sensor (microphone off screen).

![](https://cdn-learn.adafruit.com/assets/assets/000/038/966/medium800/circuit_playground_mic_out.jpg?1486598476)

And here's the code for the `CircuitPlayground.soundSensor()` function. ([direct link to repo](https://github.com/adafruit/Adafruit_CircuitPlayground/blob/master/Adafruit_CircuitPlayground.cpp#L92))

```auto
uint16_t Adafruit_CircuitPlayground::soundSensor(void) {
  return analogRead(CPLAY_SOUNDSENSOR);
}
```

Pretty boring. Nothing fancy or magical. Both the light sensor and sound sensor just call `analogRead()` for the pins they are attached to and return the value. And now you know why those functions return values from 0 to 1023. It's just the raw 10 bit analog to digital conversion.

# Temperature Sensor

Here's the circuit path for the output of the temperature sensor.

![](https://cdn-learn.adafruit.com/assets/assets/000/038/969/medium800/circuit_playground_temp_out.png?1486668053)

And here's the code for the `CircuitPlayground.temperature()` function. ([direct link to repo](https://github.com/adafruit/Adafruit_CircuitPlayground/blob/master/Adafruit_CircuitPlayground.cpp#L115))

```auto
float Adafruit_CircuitPlayground::temperature(void) {
   // Thermistor test
  float reading;

  reading = analogRead(CPLAY_THERMISTORPIN);

  //Serial.print("Thermistor reading: "); Serial.println(reading);

  // convert the value to resistance
  reading = ((1023.0 * SERIESRESISTOR) / reading);
  reading -= SERIESRESISTOR;

  //Serial.print("Thermistor resistance: "); Serial.println(reading);

  float steinhart;
  steinhart = reading / THERMISTORNOMINAL;     // (R/Ro)
  steinhart = log(steinhart);                  // ln(R/Ro)
  steinhart /= BCOEFFICIENT;                   // 1/B * ln(R/Ro)
  steinhart += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
  steinhart = 1.0 / steinhart;                 // Invert
  steinhart -= 273.15;                         // convert to C

  return steinhart;
}
```

OK, a bit more going on. But not really. Note that pretty much the first thing that happens is to read the analog value. The rest is just math to turn that into a temperature. In this case, that's more useful than the raw analog value. For the light and sound sensors, there's no really useful thing to convert to, so those just return the raw values.

# Circuit Playground Analog Input

## Questions and Code Challenges

The following are some questions related to this project along with some suggested code challenges. The idea is to provoke thought, test your understanding, and get you coding!

While the sketches provided in this guide work, there is room for improvement and additional features. Have fun playing with the provided code to see what you can do with it. Or do something new and exciting!

# Questions

- A 10 bit ADC has 1024 values, but why is the highest one 1023?
- Can you think of any natural phenomenon that behaves digitally?

# Code Challenges

- Make the knob change the color of the NeoPixels.
- Make the NeoPixels blink at different rates set by the knob.
- Change the range of the tones generated in Speaker Fun.
- Try using something other than `map()`. Something non-linear maybe?
- Combine the NeoPixel and Speaker Fun code into one - lights and sounds!


## Featured Products

### Circuit Playground Express

[Circuit Playground Express](https://www.adafruit.com/product/3333)
 **Circuit Playground Express** is the next step towards a perfect introduction to electronics and programming. We've taken the original Circuit Playground Classic and made it even better! Not only did we pack even more sensors in, we also made it even easier to...

In Stock
[Buy Now](https://www.adafruit.com/product/3333)
[Related Guides to the Product](https://learn.adafruit.com/products/3333/guides)
### Circuit Playground Classic

[Circuit Playground Classic](https://www.adafruit.com/product/3000)
Would you like to learn electronics, with an all-in-one board that has sensors and LEDs built in? **Circuit Playground** is here - and it's the best way to practice programming on real hardware with no soldering or sewing required!

This is the **Classic**...

In Stock
[Buy Now](https://www.adafruit.com/product/3000)
[Related Guides to the Product](https://learn.adafruit.com/products/3000/guides)
### Small Alligator Clip Test Lead (set of 12)

[Small Alligator Clip Test Lead (set of 12)](https://www.adafruit.com/product/1008)
Connect this to that without soldering using these handy mini alligator clip test leads. 15" cables with alligator clip on each end, color coded. You get 12 pieces in 6 colors. Strong and grippy, these always come in handy! We often use these in conjunction with a multimeter so we...

Out of Stock
[Buy Now](https://www.adafruit.com/product/1008)
[Related Guides to the Product](https://learn.adafruit.com/products/1008/guides)
### Panel Mount 10K potentiometer (Breadboard Friendly)

[Panel Mount 10K potentiometer (Breadboard Friendly)](https://www.adafruit.com/product/562)
This potentiometer is a two-in-one, good in a breadboard or with a panel. It's a fairly standard linear taper 10K ohm potentiometer, with a grippy shaft. It's smooth and easy to turn, but not so loose that it will shift on its own. We like this one because the legs are 0.2" apart...

In Stock
[Buy Now](https://www.adafruit.com/product/562)
[Related Guides to the Product](https://learn.adafruit.com/products/562/guides)
### Slim Metal Potentiometer Knob - 10mm Diameter x 15mm - T18

[Slim Metal Potentiometer Knob - 10mm Diameter x 15mm - T18](https://www.adafruit.com/product/2057)
This slim and modern potentiometer is small and compact, yet looks good and has a nice feel. We hand-selected the best knobs for your project in different sizes and varieties. &nbsp;This one is the...

In Stock
[Buy Now](https://www.adafruit.com/product/2057)
[Related Guides to the Product](https://learn.adafruit.com/products/2057/guides)
### Small Alligator Clip to Male Jumper Wire Bundle - 12 Pieces

[Small Alligator Clip to Male Jumper Wire Bundle - 12 Pieces](https://www.adafruit.com/product/3255)
For bread-boarding with unusual non-header-friendly surfaces, these cables will be your best friends! No longer will you have long strands of alligator clips that are grabbing little wires. These compact jumper cables have a premium male header on one end, and a grippy mini alligator clip on...

In Stock
[Buy Now](https://www.adafruit.com/product/3255)
[Related Guides to the Product](https://learn.adafruit.com/products/3255/guides)
### Breadboard trim potentiometer

[Breadboard trim potentiometer](https://www.adafruit.com/product/356)
These are our favorite trim pots, perfect for breadboarding and prototyping. They have a long grippy adjustment knob and with 0.1" spacing, they plug into breadboards or perfboards with ease.

This is the same pot that comes with our character LCDs and tutorial...

In Stock
[Buy Now](https://www.adafruit.com/product/356)
[Related Guides to the Product](https://learn.adafruit.com/products/356/guides)
### Half Sized Premium Breadboard - 400 Tie Points

[Half Sized Premium Breadboard - 400 Tie Points](https://www.adafruit.com/product/64)
This is a cute, half-size breadboard with&nbsp;400 tie points, good for small projects. It's 3.25" x 2.2" / 8.3cm&nbsp;x 5.5cm&nbsp;with a standard double-strip in the middle and two power rails on both sides.&nbsp;You can pull the power rails off easily to make the breadboard as...

In Stock
[Buy Now](https://www.adafruit.com/product/64)
[Related Guides to the Product](https://learn.adafruit.com/products/64/guides)

## Related Guides

- [Introducing Circuit Playground](https://learn.adafruit.com/introducing-circuit-playground.md)
- [Adafruit Circuit Playground Express](https://learn.adafruit.com/adafruit-circuit-playground-express.md)
- [Machine Learning with Marshmallows and Tiny Sorter](https://learn.adafruit.com/machine-learning-with-marshmallows-and-tiny-sorter.md)
- [Crickit Powered Holiday Diorama](https://learn.adafruit.com/crickit-powered-holiday-diorama.md)
- [Jack-o-Theremin](https://learn.adafruit.com/jack-o-theremin.md)
- [Living Starry Night Painting](https://learn.adafruit.com/animated-starry-night-circuit-playground-express.md)
- [AdaBox 008](https://learn.adafruit.com/adabox008.md)
- [Circuit Playground Hot Potato](https://learn.adafruit.com/circuit-playground-hot-potato.md)
- [Rose's Shield - Steven Universe](https://learn.adafruit.com/rose-s-shield-steven-universe.md)
- [Lucky Cat with Circuit Playground Express](https://learn.adafruit.com/lucky-cat-with-circuit-playground-express.md)
- [Cartoon Network and Make Code - Rose Quartz Shield Umbrella](https://learn.adafruit.com/cartoon-network-and-make-code-rose-quartz-shield-umbrella.md)
- [Case for Circuit Playground](https://learn.adafruit.com/case-for-circuit-playground.md)
- [MakeCode for Circuit Playground Express](https://learn.adafruit.com/makecode.md)
- [Adalight for Circuit Playground](https://learn.adafruit.com/adalight-for-circuit-playground.md)
- [Epoxy Resin Torch Lamp with Touch Control & 3D Printed Base](https://learn.adafruit.com/epoxy-resin-torch-lamp-with-touch-control-3d-printed-base.md)
- [DIY Circuit Playground Shields](https://learn.adafruit.com/circuit-playground-shields.md)
- [Ever-Burning Flame Painting](https://learn.adafruit.com/ever-burning-flame-painting.md)
