Digital Sandbox Arduino Companion

Pages
Contributors: jimblom
Favorited Favorite 3

10. Do the Analog Slide

Digital inputs, like the button, only allow for two input values: HIGH or LOW. But what about the in-betweens? When you turn the volume up on your stereo, you're not forced to pick between mute and "OMG MY EARS." For volume control and other "finely-tuned" settings, we need analog inputs.

Background Information

Analog inputs are components that put data into a system with a range of more than two values. On the Digital Sandbox, analog inputs can produce a value anywhere between zero and 1023 (1024 total values). The value produced by an analog input is proportional to the voltage it produces. If an analog component reads a value of zero, the voltage is 0V. If the output value is 1023, then the voltage is 5V. An analog reading of 512 is about 2.5V, and so on.

A special component inside the Sandbox's microcontroller called an analog-to-digital converter (ADC) is able to convert that range of input voltages to a discrete number. This is a special circuit that most pins on the Sandbox don't have. It's so special that the ADC pins are labeled with a preceding 'A'. The analog sensors on the board are labeled as "A0", "A1", "A2" and "A3."

Many electronic components produce analog output, the most common of which is a potentiometer. "Pots" come in a variety of shapes and sizes. Rotary pots are very commonly used to adjust the volume on a stereo. Slide potentiometers, like that on the bottom of the Sandbox, are often seen adjusting sound levels on mixing boards.

Active Parts

alt text

Code Components

First there was digitalWrite() then analogWrite(). Then there was digitalRead()...can you guess what's next?

Analog Input: analogRead( [analog pin] )

To read the value of an analog input we use the analogRead( [analog pin] ) function. Like digitalRead(), this function requires just a single parameter -- a pin to read.

That's where the similarities stop though. Unlike digitalRead() the only pins you can analogRead() on are those with a preceding 'A': A0, A1, A2, and A3.

Also, instead of simply returning HIGH or LOW, analogRead() returns a number between 0 and 1023 -- 1024 possible analog values! An output of 0 equates to 0V, and 1023 means the pin reads 5V. If you get 512, you're somewhere in the middle ~ 2.5V.

You can store the return value of an analogRead() in a variable, and use it from there. For example, here's how we store the analog input of A0 into a variable:

language:c
example_variable = analogRead(A0);

Or you can use it directly, like in a comparison for example:

language:c
if (analogRead(A0) > 512)
{
    Serial.println("The analog voltage is above 2.5V");
}

Don't forget to set your analog pin as an INPUT some time before you read it!

Sketch and Experiment

Here's the Sandbox_10_Analog_Slide.ino sketch. Upload away!

language:c
    // Sandbox 10: Do the Analog Slide

/* Digital inputs, like the button, only allow for two input values: HIGH or 
   LOW. But what about the in-betweens? When you turn the volume up on your 
   stereo, you’re not forced to pick between mute and “OMG MY EARS.” For volume 
   control and other “finely-tuned” settings, we need analog inputs.

   This experiment introduces analog inputs using the analogRead([analog pin]) 
   function. You can perform an analogRead on any pin that has an 'A' in front
   of it.

   analogRead() returns a value between 0 and 1023. 0 is equivalent to 0V, 1023
   is 5V, 512 is 2.5V, and so on.

   After uploading this sketch, open the serial monitor and check the output
   of the slider potentiometer.
*/

// 
void setup()
{
    // Set up the slide potentiometer as an INPUT:
    pinMode(A3, INPUT);

    // Set up serial, set the baud rate to 9600:
    Serial.begin(9600);
}

void loop()
{
    // Print the value of analog pin 3:
    Serial.println(analogRead(analogRead(A3)));

    delay(100); // Delay for visibility.
}

After uploading the sketch, open up the Serial Monitor. Numbers should begin to stream by. Try sliding the potentiometer, do the values change?

Can you make the output 0? 1023? 512? Take note of which slide pot position relates to which value.

Your Turn!

  • Can you make the slider control the LEDs? You could slide the white LEDs back and forth, or try controlling the brightness of the RGB LEDs with the slider.
  • Why are there only 1024 output values? Why not an even 1000? (Hint: 210.)