Potentiometer

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.

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):

Code for Potentiometer

Library

The potentiometer does not require any special code library.

Global Variables

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).

// if necessary, change data pin number to match your wiring
int dialPin = A0;

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).

// global variable
int dialReading;

setup( ) function

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.

Measuring Dial Position

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.

dialReading = analogRead(dialPin);

// add code to do something based on dialReading

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.

Mapping Dial Position to Custom Range of Values

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.

// global variables

int dialPin = A0;

// dialReading will be actual measurement from dial
// dialValue will be reading mapped to your custom range
int dialReading;
int dialValue;

// change min and max to match what you want for your custom range
int minValue = 0;
int maxValue = 11;

Here's the code to take a dial measurement and then map it to your custom range.

// actual measurement
dialReading = analogRead(dialPin);

// reading mapped to custom range
dialValue = round(map(dialReading, 0, 4095, minValue, maxValue + 1));

// add code to do something with dialValue

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.

Last updated