Adapted from Sparkfun Inventor Kit Experiential Guide
Introduction
In this circuit you’ll work with potentiometer.
A potentiometer is also known as a variable resistor. When powered with 5V, the middle pin outputs a voltage between 0V and 5V, depending on the position of the knob on the potentiometer. A potentiometer is a perfect demonstration of a variable voltage divider circuit. The voltage is divided proportionate to the resistance between the middle pin and the ground pin. In this circuit, you’ll learn how to use a potentiometer to control the brightness of an LED.
Parts Needed
You will need the following parts:
- Breadboard
- RedBoard or Arduino Uno
- LED
- 330Ω Resistor
- Jumper Wires
- Potentiometer
Hardware Hookup
Ready to start hooking everything up? Check out the Fritzing diagram and hookup table below, to see how everything is connected.
Polarized Components: Pay special attention to the component’s markings indicating how to place it on the breadboard. Polarized components can only be connected to a circuit in one direction. Polarized components are highlighted with a yellow warning triangle, in the table.
Fritzing Diagram for RedBoard
Hookup Table
Run the Sketch
/*
SparkFun Inventor's Kit
Example sketch 02
POTENTIOMETER
Measure the position of a potentiometer and use it to
control the blink rate of an LED. Turn the knob to make
it blink faster or slower!
What's a potentiometer?
A potentiometer, or "pot" for short, is a control knob.
It's the same type of control you'd use to change volume,
dim a lamp, etc. A potentiometer changes resistance as it
is turned. By using it as a "voltage divider", the Arduino
can sense the position of the knob, and use that value to
control whatever you wish (like the blink rate of an LED,
as we're doing here).
Hardware connections:
Potentiometer:
Potentiometers have three pins. When we're using it as a
voltage divider, we connect the outside pins to power and
ground. The middle pin will be the signal (a voltage which
varies from 0 Volts to 5 Volts depending on the position of
the knob).
Connect the middle pin to ANALOG IN pin 0 on the Arduino.
Connect one of the outside pins to 5V.
Connect the other outside pin to GND.
(TIP: if once your program is running, the knob feels
"backwards", you can swap the 5V and GND pins to reverse
the direction.)
LED:
Most Arduinos already have an LED and resistor connected to
pin 13, so you may not need any additional circuitry.
But if you'd like to connect a second LED to pin 13, or use
a different pin, follow these steps:
Connect the positive side of your LED (longer leg) to
Arduino digital pin 13 (or another digital pin, but don't
forget to change the code to match).
Connect the negative side of your LED (shorter leg) to a
330 Ohm resistor (orange-orange-brown).
Connect the other side of the resistor to ground.
This sketch was written by SparkFun Electronics,
with lots of help from the Arduino community.
This code is completely free for any use.
Visit http://learn.sparkfun.com/products/2 for SIK information.
Visit http://www.arduino.cc to learn about the Arduino.
Version 2.0 6/2012 MDG
*/
// Welcome back! In this sketch we'll start using "variables".
// A variable is a named number. We'll often use these to store
// numbers that change, such as measurements from the outside
// world, or to make a sketch easier to understand (sometimes a
// descriptive name makes more sense than looking at a number).
// Variables can be different "data types", which is the kind of
// number we're using (can it be negative? Have a decimal point?)
// We'll introduce more data types later, but for the moment we'll
// stick with good old "integers" (called "int" in your sketch).
// Integers are whole numbers (0, 3, 5643), can be negative, and
// for reasons we won't go into right now, can range from -32768
// to 32767. (Don't worry, if you need to work with larger numbers,
// there are other data types for that. See:
// http://arduino.cc/en/Reference/VariableDeclaration
// for a list of all the data types you can use).
// You must "declare" variables before you use them, so that the
// computer knows about them. Here we'll declare two integer
// variables, and at the same time, initialize them to specific
// values. We're doing this so that further down, we can refer to
// the pins by name rather than number.
// Note that variable names are case-sensitive! If you get an
// "(variable) was not declared in this scope" error, double-check
// that you typed the name correctly.
// Here we're creating a variable called "sensorPin" of type "int"
// and initializing it to have the value "0":
int sensorPin = 0; // The potentiometer is connected to
// analog pin 0
int ledPin = 13; // The LED is connected to digital pin 13
// One more thing. If you declare variables outside of a function,
// as we have here, they are called "global variables" and can be
// seen by all the functions. If you declare variables within a
// function, they can only be seen within that function. It's good
// practice to "limit the scope" of a variable whenever possible,
// but as we're getting started, global variables are just fine.
void setup() // this function runs once when the sketch starts up
{
// We'll be using pin 13 to light a LED, so we must configure it
// as an output.
// Because we already created a variable called ledPin, and
// set it equal to 13, we can use "ledPin" in place of "13".
// This makes the sketch easier to follow.
pinMode(ledPin, OUTPUT);
// The above line is the same as "pinMode(13, OUTPUT);"
// You might be wondering why we're not also configuring
// sensorPin as an input. The reason is that this is an
// "analog in" pin. These pins have the special ability to
// read varying voltages from sensors like the potentiometer.
// Since they're always used as inputs, there is no need to
// specifically configure them.
}
void loop() // this function runs repeatedly after setup() finishes
{
// First we'll declare another integer variable
// to store the value of the potentiometer:
int sensorValue;
// The potentiometer is set up as a voltage divider, so that
// when you turn it, the voltage on the center pin will vary
// from 0V to 5V. We've connected the center pin on the
// potentiometer to the Arduino's analog input 0.
// The Arduino can read external voltages on the analog input
// pins using a built-in function called analogRead(). This
// function takes one input value, the analog pin we're using
// (sensorPin, which we earlier set to 0). It returns an integer
// number that ranges from 0 (0 Volts) to 1023 (5 Volts).
// We're sticking this value into the sensorValue variable:
sensorValue = analogRead(sensorPin);
// Now we'll blink the LED like in the first example, but we'll
// use the sensorValue variable to change the blink speed
// (the smaller the number, the faster it will blink).
// Note that we're using the ledPin variable here as well:
digitalWrite(ledPin, HIGH); // Turn the LED on
delay(sensorValue); // Pause for sensorValue
// milliseconds
digitalWrite(ledPin, LOW); // Turn the LED off
delay(sensorValue); // Pause for sensorValue
// milliseconds
// Remember that loop() repeats forever, so we'll do all this
// again and again.
}
Code To Note
int sensorValue;
A “variable” is a placeholder for values that may change in your code. You must introduce, or “declare” variables before you use them; here we’re declaring a variable called sensorValue, of type “int” (integer). Don’t forget that variable names are case-sensitive!
sensorValue = analogRead(sensorPin)
We use the analogRead()
function to read the value on an analog pin. analogRead()
takes one parameter, the analog pin you want to use (“sensorPin”), and returns a number (“sensorValue”) between 0 (0 volts) and 1023 (5 volts).
delay(sensorValue)
Microcontrollers are very fast, capable of running thousands of lines of code each second. To slow it down so that we can see what it’s doing, we’ll often insert delays into the code. delay()
counts in milliseconds; there are 1000 ms in one second.
What You Should See
You should see the LED blink faster or slower in accordance with your potentiometer. If it isn’t working, make sure you have assembled the circuit correctly and verified and uploaded the code to your board, or see the troubleshooting section.
Real World Application
Most traditional volume knobs employ a potentiometer.
Questions
- What range of values can 'sensorValue' have?
Adapted from Sparkfun Inventor Kit Experiential Guide