[tutorial] How to use a Potentiometer



For those beginning to learn about robotics, particularly in the area of building circuits, you may have come across the question of how to change the brightness of a LED, without having to keep switching parts. Quite simply, the solution to this issue is a potentiometer.

Potentiometers are variable resistors and they function to alter their resistance via a knob or dial. You have probably used one before by adjusting the volume on your stereo or using a light dimmer.

Potentiometers have a range of resistance. They can be attuned from zero ohms to whatever maximum resistance that is specific to it. For example, a potentiometer of 10 kΩ can be adjusted from 0 Ω to its maximum of 10 kΩ.

In this tutorial you will learn how to use a potentiometer with and without Arduino board to fade an LED.

You will also learn how to use analogRead() and map() functions.

How to use potentiometer

variable resistor

All potentiometers have three pins. The outer pins are used for connecting power source (Vref and gnd). The middle pin (output) give us the variable of resistance value.

Let's see it in practice, you will need:

  • potentiometer
  • led
  • battery AA 1.5 (or another but no more than 5V)

Connect battery to outer pins of potentiometer and the positive end of led (larger pin) to middle pin. Now turn the knob (or dial) left and right.

It changes the brightness of the led!

Now let's see how we can connect the potentiometer with the arduino uno

The Circuit


You will need:

  • Arduino uno
  • Breadboard
  • LED
  • 220 Ohm resistor
  • Potentiometer (e.g. 4.7k)

The Code

Here's the 'Fade an LED with potentiometer' code, embedded using codebender!

By turning the shaft of the potentiometer, we change the amount of resistence on either side of the wiper which is connected to the center pin of the potentiometer. This changes the relative "closeness" of that pin to 5 volts and ground, giving us a different analog input. When the shaft is turned all the way in one direction, there are 0 volts going to the pin, and we read 0. When the shaft is turned all the way in the other direction, there are 5 volts going to the pin and we read 1023. In between, analogRead() returns a number between 0 and 1023 that is proportional to the amount of voltage being applied to the pin.

Ηow it works:

  • Read analog value from potentiometer middle pin
    *-> value=analogRead(potPin)
  • Map analog values 0-1024 to pwm values 0-255
    *-> value = map(value, 0, 1023, 0, 255);
  • Send pwm value to led
    -> analogWrite(ledPin, value);

Tip: You can make the same example by connecting potentiometer middle pin to a digital PWM pin and avoid to use map() function. Try it dy clicking 'Edit' button.

Well done!

You have successfully completed our third Arduino "How to" tutorial and you learned so far how to use:

  • LEDs
  • potentiometers
  • pinMode(), delay(), map(), digitalWrite(), analogWrite() and analogRead() functions
  • variables and constants
  • if statement