Arduino Tutorial Bundle .:Arduino Experimentation Kit:. (ARDX)

PrintPrint PDF FacebookFacebook TwitterTwitter Send this to a friendE-mail
by oomlout
 

step 10.:Twisting:. (Potentiometers) - CIRC08


What We're Doing:
Along with the digital pins the Arduino has it also has 6 pins which can be used for analog input. These inputs take a voltage (from 0 to 5 volts) and convert it to a digital number between 0 (0 volts) and 1024 (5 volts) (10 bits of resolution). A very useful device that exploits these inputs is a potentiometer (also called a variable resistor). When it is connected with 5 volts across its outer pins the middle pin will read some value between 0 and 5 volts dependent on the angle to which it is turned (ie. 2.5 volts in the middle). We can then use the returned values as a variable in our program.

(you can also download the breadboard layout sheet from the bottom of this step)


The Parts:
  • CIRC-08 Breadboard Sheet
  • 2 Pin Header (x4)
  • Potentiometer (10k ohm) (x1)
  • 5mm Green LED (x1)
  • 560 ohm Resistor (green-blue-brown) (x1)
  • Wire


The Circuit and Plugging Everything In:
A Small Video of Everything Being Plugged in


The Code: - File > Sketchbook > Examples > Analog > AnalogInput
/* * AnalogInput * by DojoDave <http://www.0j0.org> * * Turns on and off a light emitting diode(LED) connected to digital   * pin 13. The amount of time the LED will be on and off depends on * the value obtained by analogRead(). In the easiest case we connect * a potentiometer to analog pin 2. * * http://www.arduino.cc/en/Tutorial/AnalogInput */int potPin = 2;    // select the input pin for the potentiometerint ledPin = 13;   // select the pin for the LEDint val = 0;       // variable to store the value coming from the sensorvoid setup() {  pinMode(ledPin, OUTPUT);  // declare the ledPin as an OUTPUT}void loop() {  val = analogRead(potPin);    // read the value from the sensor  digitalWrite(ledPin, HIGH);  // turn the ledPin on  delay(val);                  // stop the program for some time  digitalWrite(ledPin, LOW);   // turn the ledPin off  delay(val);                  // stop the program for some time}



Not Working?
  • Sporadically Working -This is most likely due to a slightly dodgy connection with the potentiometer's pins. This can usually be conquered by taping the potentiometer down.
  • The Control is Backward - There are two ways to fix this, either switch the red and black wires connected to the potentiometer, or turn the potentiometer around. (sorry sometimes the factory ships us a backwards potentiometer)
  • Still Backward - You can try operating the circuit upside down. Sometimes this helps.


Making it Better:
Threshold Switching:
Sometimes you will want to switch an output when a value exceeds a certain threshold. To do this with a potentiometer change the loop() code to.
void loop() {  int threshold = 512;  if(analogRead(potPin) > threshold){ digitalWrite(ledPin, HIGH);}  else{ digitalWrite(ledPin, LOW);}}
This will cause the LED to turn on when the value is above 512 (about halfway), you can adjust the sensitivity by changing the threshold value.

Fading:
Lets control the brightness of an LED directly from the potentiometer. To do this we need to first change the pin the LED is connected to. Move the wire from pin 13 to pin 9 and change one line in the code.
int ledPin = 13; ----> int ledPin = 9;
Then change the loop code to.
  void loop() {   int value = analogRead(potPin) / 4;   analogWrite(ledPin, value);  }
Upload the code and watch as your LED fades in relation to your potentiometer spinning. (Note: the reason we divide the value by 4 is the analogRead() function returns a value from 0 to 1024 (10 bits), and analogWrite() takes a value from 0 to 255 (8 bits) )

Controlling a Servo:
This is a really neat example and brings a couple of circuits together. Wire up the servo like you did in CIRC-04, then open the example program Knob (File > Sketchbook > Examples > Library-Servo > Knob ), then change one line of code.
int potpin = 0; ----> int potpin = 2;
Upload to your Arduino and then watch as the servo shaft turns as you turn the potentiometer.

.:Twisting:. (Potentiometers) - CIRC08
CIRC08-Square.JPGCIRC08-assembled.jpgCIRC08-pieces.jpgCIRC08-3d.pngCIRC08-3dexploded.pngCIRC08-scem.pngCIRC08-sheet.png
CIRC08-sheet.pdf(630x810) 47 KB
 
Remove these adsRemove these ads by Signing Up.
To Download the PDF or View All Steps,   
Become a Pro Member »