10K Breadboard Potentiometer

From LinkSprite Playgound
Jump to: navigation, search

Overview

A potentiometer is a simple knob that provides a variable resistance, which we can read into the Arduino board as an analog value. Shown in the Figure below is a simple 10K breadboard potentiometer.

Potentiometer.jpg

This board works with 5V DC power supply. It has three pins: +5V, GND, and Out, where the Pin Out is the voltage output that varies with resistance.

Example Project

In this example, that value controls the rate at which an LED blinks. We connect three wires to the Arduino board. The first goes to Ground from one of the outer pins of the potentiometer. The second goes from +5V to the other outer pin of the potentiometer. The third goes from analog input 2 to the middle pin of the potentiometer.

By turning the shaft of the potentiometer, we change the amount of resistance on either side of the wiper which is connected to the center pin of the potentiometer. This changes the relative "closeness" of that pin to +5V and Ground, giving us a different analog input. When the shaft is turned all the way in one direction, there are 0V going to the pin, and we read 0. When the shaft is turned all the way in the other direction, there are 5V going to the pin and we read 1023. In between, the function analogRead( ) returns a number between 0 and 1023 that is proportional to the amount of voltage being applied to the pin.

The source code is attached below.

<syntaxhighlight lang="c">

/* Analog Read to LED

* ------------------ 
*
* 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.
*
* Created 1 December 2005
* copyleft 2005 DojoDave <http://www.0j0.org>
* http://arduino.berlios.de
*
*/

int potPin = 2; // select the input pin for the potentiometer int ledPin = 13; // select the pin for the LED int val = 0; // variable to store the value coming from the sensor

void 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

}

</syntaxhighlight>