Lab-Analog Input

Parts

  • breadboard
  • hook up wire
  • Arduino
  • LEDs
  • potentiometer
  • 330 Ohm resistors (or similar)
  • 10K Ohm resistors (or similar)
  • 1K Ohm resistors (or similar)
  • variable resistor(s) of  your own choosing
  • wire stripper
  • multimeter

Connect Power from Arduino to Breadboard

  1. Hook up a red wire from the 5V output of the Arduino to one of the power rails on the breadboard
  2. Hook up a black wire from either ground (GND) connection on the Arduino to a ground rail on the breadboard
  3. Hook up power and ground across the breadboard so that the rails on both sides are live.

While you can choose any color of wires, and don’t always need to have the rails on both sides live, you’ll find that doing so by default will prevent confusion later on and end up saving you debugging time.

connect_power

Potentiometer Input

  1. Attach your potentiometer’s outer red and black wires to power and ground respectively. Then connect the middle wire from the potentiometer to the Analog input pin 3 on your Arduino board. Check to make sure you’ve connected to the Analog 3 input and not the Digital one that’s on the other side of the board.

Your potentiometer is a self-contained voltage-divider circuit, essentially acting like two resistors each with a value that changes exactly in proportion to the other as the internal wiper sweeps across a strip of partially conductive material.

potentiometer_diagram

potentiometer

Add an LED for PWM Output

  1. Place an LED on your breadboard with the short, cathode leg connected to ground and the longer positive leg connected to one side of a 330 Ohm resistor.
  2. Attach a hookup wire from the other side of the 330 Ohm resistor to Digital pin 9 on your Arduino. Note that this is one of the pins labeled with “PWM”.

PWM or pulse-width modulation is the process by which a digital pin can be turned on and off rapidly to simulate a lower voltage. The amount of time that the digital pin is on is known as the pulse width. By varying (modulating) the length of time that the pin is turned on, the apparent brightness of the LED can be controlled.

addLED
Schematic01

Load a Program to Control the Arduino

  1. Paste the following code into your Arduino program, then select the appropriate serial port and board on the Tools menu and upload the program to your Arduino board.
int ledPin = 9;      // LED connected to digital pin 9
int analogPin = 3;   // potentiometer connected to analog pin 3
int val = 0;         // variable to store the read value

void setup()
{
  pinMode(ledPin, OUTPUT);   // sets the pin as output
}

void loop()
{
  val = analogRead(analogPin);   // read the input pin
  analogWrite(ledPin, val / 4);  // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
}

Setting up a serial output is useful to monitor the varying results of your potentiometer. If you would like to see serial output, add the following lines to your code. In the setup function, add the command to initialize serial output at 9600 baud:

Serial.begin(9600);

Also add the following command to the end of the loop function. This will print each value in decimal, on its own line, as serial output:

Serial.println(val, DEC);

Upload the code to your Arduino board, then click the “Serial Monitor” icon on the Arduino toolbar. You should see numbers outputting between 0 to 1023. Adjust your potentiometer and notice the behavior of the numbers and the LED.

Try Other Variable Resistors

Potentiometers intrinsically contain an internal voltage divider. For most other types of variable resistors, you’ll have to make your own voltage divider circuit. Luckily, this is very easy to do.

  1. Obtain a variable resistor, perhaps a photoresistor, thermistor, flex sensor or force-sensing resistor. Your variable resistor should have only two leads. If it has more, check the data sheet to find out what they are.
  2. Attach one leg of the variable resistor to 5V power.
  3. Attach the other leg of the variable resistor to one side of a fixed resistor. Start with a 10K Ohm resistor, but remember that a different value might be needed for the very best results.
  4. Attach the other side of the fixed 10K resistor to ground.
  5. Finally, hook up a wire from one of your Arduino’s Analog inputs (such as Analog pin 3) to the same point where the variable resistor meets the fixed resistor. This is where you will sense changes in the voltage divider circuit, between the two types of resistor.

VoltageDividerSchematic

Try varying the value of the fixed resistor to see what results you get, both by measuring at various points in the circuit with a multimeter, and by viewing the digital results using serial output. What changes does it make if you use a 1K fixed resistor rather than a 10K?

Invent Something

  1. Create a device that uses analog input in an engaging way. Consider designing your own “love meter” like the ones found in old-time arcades, where squeezing a handle resulted in a purported readout of your romantic abilities. Pick an input of your own choosing, and an output (PWM, varied number of lights, speed of blinking, buzzer or perhaps a servo motor if you’d like to skip ahead slightly) that creates some kind of compelling interactive experience.
  2. You can find a few variable resistors in your kit, but this is a good opportunity to begin shopping for yourself. Sparkfun Electronics is an excellent resource for beginners to get their hands on well-documented electronic parts. Check their sensors category for interesting inputs that might be useful for this project and future ones.

Any creative project that demonstrates basic ability with analog inputs is fine. You don’t have to build a love meter although there seems to be no limit to the amount of creative, funny and often downright weird takes on the love meter that students are able to envision and create. Have fun!