.st0{fill:#FFFFFF;}

Arduino

Arduino basics – How to use the potentiometer 

 March 22, 2018

By  Peter

Join Our Mailing List

We publish fresh content each week. Read how-to's on Arduino, ESP32, KiCad, Node-RED, drones and more. Listen to interviews. Learn about new tech with our comprehensive reviews. Get discount offers for our courses and books. Interact with our community.

One email per week, no spam, unsubscribe at any time.

In this post I’ll show you how to use a potentiometer. A potentiometer is a device that allows you to change the value of a resistance by turning a knob.

Let’s put this circuit together, and then we’ll discuss how it works. In particular, we’ll see what is happening inside the potentiometer.

Assembly

Let’s assemble the circuit. You will need:

  • The Arduino
  • A 10 kΩ potentiometer
  • An LED
  • A resistor to protect the LED (I used a 1kΩ resistor)

As you turn the knob of the potentiometer, the resistance connected to its output pin changes. This in turn changes the voltage on that pin. This output voltage is read by the Arduino at analog pin 0 (A0).

The Arduino will then use digital pin 11 to drive the LED. Although pin 11 is digital, we are using its Pulse Width Modulation (PWD) feature, like we did back in Lecture 5. We just glossed over this feature back then: we used the analogWrite instruction which makes use of this feature. Later in this lecture I will explain how PWD works.

Here’s a photo of the assembled circuit:

How it works

In the blog post titled “Arduino basics: The voltage divider and the light sensor“, you learned about the voltage or impedance (resistance) divider. Back then, the divider was fixed as we used two fixed resistors.

In a potentiometer, we still have these two resistors inside the package, but at least one of them is variable, that is, the resistor changes as we turn the knob.

In the diagram on the right, the pin in the middle is connected to a dial that is made of conductive material, like copper. They circle represents a resistor, with its two ends connected to Vcc and GND. As the dial comes in contact with different parts of the resistor, it samples a voltage between V+ and ground, and we can read this voltage from the middle pin, labeled “Out” in the diagram.

A potentiometer is just a variable voltage divider. That takes care of the input.

But how to we get the LED to light up in proportion to the position of the potentiometer?

This is where Pulse Width Modulation, or PWM, comes in.

Arduino’s digital pins can output HIGH and LOW, as we have already seen. Some of these digital pins, however, are special: although they can still only output HIGH and LOW, their output can be modulated is a way that this output can behave as if it was analog.

Have a look at this diagram, taken from the Arduino web site. A “normal” digital pin outputs 0V or 5V, like in the top and bottom waveform examples. But, in a pin that supports PWM, we can output 5V for only part of the period. In the second waveform, for example, we output 5V for 25% of the period, and 0V for the rest. In the third example, its 50% and 50% respectively.

On the LED, this kind of modulation on the output signal has an effect similar to using an analog pin and outputting voltage in the range of 0V to 5V. In effect, we simulate analog output using a digital pin!

Sketch

Here’s the sketch that drives this circuit

int potentiometerPin = 0;
int ledPin = 11;
int potentiometerVal = 0;
void setup()
{
	Serial.begin(9600); // setup serial
}
void loop()
{
	potentiometerVal = analogRead(potentiometerPin);
//I use the map function because PWM pins can only accept
//values from 0 to 255. Analog pins can output values from
//0 to 1023. With the map function, the range 0-1023 is
//converted to appropriate values from 0 to 255.
	int mappedVal = map(potentiometerVal,0,1023,0,255);
	Serial.print(potentiometerVal);
	Serial.print(" - ");
	Serial.println(mappedVal);
	analogWrite(ledPin,mappedVal);
	delay(10);
}

There is nothing new here. We have seen the “map” function in an earlier lecture, so you know that this function is used when we want to make a range of values fit within another range of values. In this case, we read values in the range of 0 to 1023 from analog pin 0, and we want to make this fit in the range 0 to 255, which is what the pulse width modulation-capable pin 11 can output.

Other than that, we simply use the “analogRead” function to read a value from analog pin 0, and the “analogWrite” function to create a PWM pulse on digital pin 11.

Simple!

Here’s an experiment: Take a small speaker and connect it to the breadboard so that it replaces the LED. Fire up the circuit. Can you hear the sound coming out of it? Can you notice how its pitch changes as you turn the knob?


Tags

Arduino, Basics, Potentiometer


You may also like

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Understanding the power requirements is crucial for any enthusiast using the Arduino Uno in their projects. How you manage voltage, amperage, and power sources impacts the success or failure of your endeavours. In this guide,

Read More
The Ultimate Guide to Powering Your Arduino Uno Board

If you are curious about how electronic devices work, understanding the fundamentals of the operation of their basic components is essential. Every electronic device—from the smartphones in our pockets to the satellites orbiting our planet—contains

Read More
A Guide to Essential Electronic Components in Circuits