Potentiometer
Last updated
Last updated
A potentiometer is a variable resistor that can be adjusted by sliding, rotating, or another type of physical interaction.
Potentiometers are used in various electrical devices such as: joysticks and other game controllers, control knobs and sliders, dimmer switches for lights, etc.
The potentiometer included in your Photon kit is a "trimpot" that can be rotated and used as a dial. The dial can be rotated approximately 270° (it cannot be rotated all the way around). The position of the dial can be measured and used as an input to change something (such as: brightness of light, volume of sound, etc.) – or to make a selection from a range of values.
Your potentiometer has 3 metal legs. Line up the legs with different numbered rows on the breadboard, and push down to insert the potentiometer into the breadboard. Then use jumper wires to connect the 3 leg rows to the Photon.
Potentiometer
Photon Pin
Outer leg (either one)
3.3V
Middle leg
any analog I/O pin
Outer leg (other one)
GND
IMPORTANT: The middle leg of the potentiometer must connect to an analog I/O pin, such as: A0, A1, A2, A3, A4, A5.
Experiment 4 of the online SparkFun Photon Experiment Guide shows how to connect the potentiometer. Here is the connection diagram for Experiment 4 (ignore the wiring for the 3 push buttons):
The potentiometer does not require any special code library.
In the global variables, you should declare which pin is being used as the potentiometer input pin (middle leg). The example below declares a variable called "dialPin" (but you could use a different variable name).
You will probably also want to declare an integer variable to store the reading from the dial. This will make it easier to do something based on the measurement. The example below declares a variable called "dialReading" (but you could use a different variable name).
There isn't any code that you need to include in the setup()
function for the potentiometer.
IMPORTANT: Do not set a pin mode for the potentiometer.
You can measure the position of the potentiometer dial using an analogRead()
statement. This code would be most likely placed within the loop()
function or within a custom function.
The measurement will be a value ranging from 0-4095.
When the dial is rotated all the way to the left (counterclockwise), the reading will be 0. When the dial is rotated all the way to the right (clockwise), the reading will be 4095.
IMPORTANT: Remember that the dial can only be rotated approximately 270° – it cannot be rotated all the way around.
In many cases, you don't want your dial to give you a number between 0-4095. Instead, you may want a dial that can be used to a select a value between a smaller custom range.
For example, if you were using the dial as a volume control knob, you may want the dial to give you a value between 0-10 (with 0 representing volume off and 10 representing maximum volume). Even better, you may want a volume knob that goes to 11.
To do this, you can use a map()
function that converts a measurement from its original range (such as 0-4095) into a new range of your choice. You decide the minimum and maximum values for this new range.
To map the dial reading to your own custom range of values, it will help to declare global variables for the minimum value of your custom range, the maximum value of your custom range, as well as a variable to store your new dial value mapped within this custom range.
Here's the code to take a dial measurement and then map it to your custom range.
So now you can use your dial to turn it up to 11.
NOTE: The code rounds the mapped value to the nearest integer because the map()
function always returns a decimal value. Also, the code adds 1 to the maxValue because otherwise it is very difficult to select the maximum value even if the dial is turned all the way to the right.