# HW5: Potentiometer

In this article we will see two ways to use a potentiometer.

The previous article was a few more than a review on how to use LEDs. Because of that, this article will be more dense of contents, we will see two examples of use of the potentiometer.

The first example doesn’t involve any code. With this example we will only learn what a potentiometer is and what it does.

What we need (for both the examples):

• 1x 10KΩ potentiometer
• 1x 220Ω resistor
• 1x 5mm LED

This is how a potentiometer looks like.

The potentiometer is usually described as a variable resistor, infact turning the shaft its resitance changes from 0Ω to its max value.

As you can see the potentiometer has 3 contacts. The left and the right contacts must be connected one to the power source and one to the ground. It is not strictly important which one receives the power and which one goes to the ground. How we connect the side pins only affects where is the minimum resistance value and where is the maximum.

We have the minimum resistance when the shaft is turned to the power source side, the maximum when turned all the way to the ground side.

The middle contact gives the potentiometer’s output current. The current intensity depends on how we rotate the shaft. Minimum resistance obviously means maximum current intensity and conversly.

In the first article about the LED I said that once we know the minumum value of the resistor to put in series into the circuit, we can use a resistor with any higher value, but the higher is the value, the dimmer will be the light emitted.

Let’s see this using a potentiometer.

This is the circuit that we have to build (click to zoom)

One side contact of the potentiometer goes to the 5V pin, the other to the ground and the middle one to the LED’s positive lead. Then we connect the negative LED’s lead to the ground through a 220Ω resistor.

You may wander why we use a resistor if we already have a potentiometer in the circuit and as I said before it behaves as a resistor. Well, I also said that the potentiometer’s minimum resistance is 0Ω. We must be protected when the potentiometer resistance is under the safe value for the LED. Because of the resistor’s presence in the circuit, the actual resistance’s values vary from 220Ω to 10220Ω. Resistors in series sum their resistance’s values.

For this example we don’t need to upload any code, so just give power to Arduino and rotate the potentiometer’s shaft around. You should see the LED’s light to change its intensity.

The second example

This time we will connect the middle contact of the potentiometer to an analog pin on Arduino. In this way Arduino will be able to read the current flowing out from the potentiometer and it will be able to take decisions according to it.

We need the same components as above, we only have to connect them in a different way. Let’s see an image.

The + and – column of the breadboard are connected the first to the 5V and the other to GND pin.

The potentiometer has the side connectors connected one to the + column and one to the – column. The middle conntact is connected to the analog pin A0.

The LED has the positive lead connected to the digital PWM pin 11 and the negative lead to the – column through a 220Ω resistor.

The circuit is complete, now the code.

You can find the same code already on the Arduino IDE. To load it go to File > Examples > 03.Analog > Calibration.
The official tutorial.

Now just change the ledPin variable to 11. I used the pin 11 only to keep the image above ordered.

You can read the code below, just in case it will be changed or removed in newer versions of the IDE.

Line 2 and 3: naming the pins’ for better readability and maintenance of the code.

Lines 6 to 8: declaration of 3 variables.

setup(): it is not necessary to declare the use of the analog pins as input, that’s the default. First the digital pin 13 is declared as output and the L LED is lit. Then we find a while loop which last 5 seconds. millis() says the milliseconds passed since Arduino was powered up. Anytime we give power to Arduino, the counter starts again from 0. Inside the loop we read the current’s intensity arriving from the potentiometer to the pin A0. The reading is done by the analogRead() function, which only requires the pin from which to read. The next lines into the while are used to assign the actual minimum and maximum read from the A0 pin.

Given the code written in the setup() for the first 5 seconds we have to turn the potentiometer’s shaft all the way to the left and to the right a few times to let Arduino know which is the range of values in input. They reasonably will be between 0 and 1023.

To know which values Arduino reads we should use the Serial Monitor. More about how to use the Serial Monitor.

Line 37: we read the current value arriving to the pin A0

Line 40map() is a function which maps a value (1st parameter) within a range (2nd and 3rd parameters) to a value belonging to another range (4th and 5th parameters). Let’s explain with an example. Assume to have a starting range which goes from 1 to 1000 and the target range is 1 to 10. Suppose to receive a value equals to 237, this will be mapped to 3 on the target range. Official documentation.
We need this function because the input range (0 to 1023) and the output range (0 to 255) are different. The PWM pins can’t manage values over 255.

Line 46: we send an output signal from the PWM pin to the LED equals to the mapped imput value.

Rotating the potentiometer’s shaft, we will have the same result as the first example, but this time it is Arduino to manage the LED’s brightness depending on the readings coming from the potentiometer.

I’ve tested the circuits and the code posted in this article on my equipment and they work properly, anyway I do not assume any responsibility for any damage which your components could suffer.