How to connect a potentiometer to a microcontroller

Post Reply
User avatar
Shane
Captain
Captain
Posts: 226
Joined: Sun Jul 19, 2009 9:59 pm
Location: Jönköping, Sweden

How to connect a potentiometer to a microcontroller

Post by Shane » Thu Jun 23, 2011 11:37 pm

A POTENTIOMETER is a three-terminal resistor with a sliding contact that forms an adjustable voltage divider. If only two terminals are used (one side and the wiper), it functions as a variable resistor or Rheostat. Potentiometers are commonly used to control electrical devices, such as the volume or tone control of an amplifier. Potentiometers are rarely used to directly control significant power, but rather are primarily used to adjust the level of small analogue signals or a control input for electronic circuits. We'll use a potentiometer in this way, as an analogue input to control the flash rate of an LED connected to a DIGITAL OUT pin on the Arduino.

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.
1.jpg
1.jpg (141.27 KiB) Viewed 3741 times

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
 }
2.jpg
2.jpg (218.96 KiB) Viewed 3741 times
Courtecy of UMASS AMHERST
Post Reply

Return to “Arduino”