Sunday, September 22, 2013

Analog Inputs


Discussion:

We've investigated using the Arduino to read in a digital value. In this case, digital means that a quantity can only have one of two values, namely HIGH (synonymous with "1" or +5 V) or LOW (synonymous with "0" or ground).

Click through the break to begin this activity.


For some applications, we require the ability to measure an input more precisely than to call it simply HIGH or LOW. We want to be able to measure an analog input. To be correct, an analog measurement implies the ability to use a truly continuous spectrum of values. For example, the pitch of a guitar is an analog quantity, because it could be 440 Hz, 450 Hz, or anywhere in between (like 442.23523119874... HZ). What we're really doing is using the Arduino's built-in analog-to-digital converter. An analog-to-digital converter takes an analog signal (or input) is makes at a discrete value. For example, a 3-bit analog-to-digital converter maps a continuous spectrum of possible values to 2^3 (or 8) possible input values. The chart below shows how a digital input behaves differently than a 3-bit analog input.


Notice how the digital input treats any input value below about 2.5 V as LOW and any input value higher than about 2.5 V as HIGH. The analog input, on the other hand, quantizes the input voltages into 8 distinct values that do a better job of accurately measuring them.

The actual analog-to-digital converter built in to the Arduino (available on pins A0 through A5) is 10-bit. That means it quantizes input voltages into 2^10 (or 1024) discrete values. A value of 0 corresponds to an input voltage of 0 V, and a value of 1023 corresponds to an input voltage of 5 V. Simple arithmetic allows us to accurately measure in between voltages as well, e.g., an input value of 950 is approximately 4.64 V because 950 * 5 / 1023 = 4.64.

Procedure:

  1. Open the example sketch called "AnalogInput" (found under File-->Examples-->.03Analog)
  2. Connect the circuit as described in the sketch's comments
  3. Depending on how many potentiometers are available, you might have to sustitute a different component to use as a varibale input. Consider using a photoresistor or a force-dependant resistor instead.
  4. Upload the code and explore how everything works

Written Problems:
  1. Suppose you're using the Arduino to measured an analog voltage somewhere between 0 and 5 volts. If the analogRead() function gives a result of 125, what is the actual voltage?
  2. Suppose you're using the Arduino to measured an analog voltage somewhere between 0 and 5 volts. If the analogRead() function gives a result of 768, what is the actual voltage?
  3. Suppose you are using a photoresistor connected on pin A5 to detect a laser trip wire. Experimentally, you find that the voltage across the photoresistor is 4.0 volts (or less) when the laser is blocked. As part of your sketch, you use the statement if(analogRead(A5) < XXX) to trigger the part of the sketch that should be executed when the laser is blocked. What is the value of XXX?

Going Further:

Instead of outputting "information" as LED flashes, let's use sound.
  1. Open the example sketch called tonePitchFollower (found under File-->Examples-->.02Digital)
  2. Connect the circuit as described in the sketch's comments
  3. You might have to change the sketch slightly depending on if you are using a photoresistor, potentiometer, or something else.
  4. Upload the code and explore how everything works

No comments:

Post a Comment