We will connect all three contacts of the POTENTIOMETER to the Arduino. The first connection goes from one of the outer pins of the POTENTIOMETER to GROUND. The second connection goes from the other outer pin of the potentiometer to +5VDC. The final connection is made from the centre pin of the potentiometer to the ANALOG 0 Pin on the Arduino. We also connect an LED with a current limiting resistor to DIGITAL PIN 13 (see LED Blink for reference).
In this example, we've chosen to use a Potentiometer with a male header soldered to the tabs. This creates a very stable and easy to use attachment for the Potentiometer to the breadboard and is recommended, though any Potentiometer chosen will work fine (as long as it is the correct value - 10K OHMS). In the photo below, we see examples of a few different types of potentiometers - the first is a thumbwheel type and will fit directly into the breadboard. The second is the one we are using with the header pins soldered to the tabs. The third is another panel mount Potentiometer with leads soldered to the tabs, which would be plugged into the breadboard. In this example, we are using the Modern Devices Arduino, which has male headers for the 6 Analogue In channels. We are using a lead with one female plug attached to one end to fit into the Analogue 0 pin on the board.
Code: Select all
// ANALOG INPUT - POTENTIOMETER
// This example flashes an LED on and off at a rate determined by the
// analog input value read on ANALOG IN 0 based on the position of the
// potentiometer. When the potentiometer is turned all the way clockwise, the
// analogRead function will return a value of 0. When the potentiometer is turned
// all the way counterclockwise, the analogRead function will return a value of
// 1023. This value will be used to determine one cycle of delay time for the LED
// to be on and then off. The value of potPIN is printed into the Arduino Serial
// Window so its value change can be monitored along with the change in flash
// rate of the LED
int potPIN = 0; // select the input pin for the potentiometer
int ledPIN = 13; // select the output pin for the LED
int potVAL = 0; // variable to store the value coming from the potentiometer
void setup() {
Serial.begin(9600); // set the baud rate for the serial window
pinMode(ledPIN, OUTPUT); // declare the ledPin as an OUTPUT, ANALOG IN is an
// input by default
}
void loop() {
potVAL = analogRead(potPIN); // read the value from the potentiometer
potVAL = (potVAL/2 + 16); // scale and shift the potVAL to be a useful
// flashrate (barely visible to 511ms)
digitalWrite(ledPIN, HIGH); // set the ledPIN HIGH, which turns on the LED
delay(potVAL); // do nothing for (potVAL) milliseconds
digitalWrite(ledPIN, LOW); // set the ledPIN LOW, which turns off the LED
delay(potVAL); // do nothing for (potVAL) milliseconds
Serial.println(potVAL); // print potVAL in the arduino serial window
}