# Arduino Potentiometer with Multiple LEDs [Tutorial]

In this Arduino tutorial you will learn how to control multiple LEDs with a potentiometer.

I will not explain everything from scratch, so it’s better if you already know a bit about LEDs and potentiometer before reading the following. If you have some doubts, check out how to control one LED with a potentiometer.

We will first see how you can select a different LED (which means: power on a different LED) when you turn the potentiometer knob. Then, we will dynamically modify the LEDs’ brightness.

This tutorial works for any Arduino board, I will use Arduino Uno for the example.

Let’s get started!

## Arduino circuit with potentiometer and multiple LEDs

Let’s build this circuit.

You are learning how to use Arduino to build your own projects?

Check out and learn step by step.

Steps to build the circuit:

• For each LED, plug the shorter leg to the ground. You can do so directly by plugging the leg into the ground line of the breadboard – which is then connected to a GND pin on the Arduino.
• Connect the longer leg to a digital pin, with a 220 Ohm resistor in between. Make sure you connect each LED and each leg separately to avoid short circuits.
• For the potentiometer, connect one of the leg on the side (for example the left one here) to the ground. The opposite leg goes to the 5V pin of the Arduino, and finally the middle pin goes to an analog pin.

Once everything is correctly plugged, check again, and then let’s start with the code.

## Select which LED to power on with the potentiometer

Here is what we want to do here: depending on the knob position, we will power on a different LED. To make it simple, with 3 LEDs, we select the first LED when the knob is in the first 1/3th, the second LED between 1/3th and 2/3th, and the third LED if more than 2/3th.

Here is the Arduino code to do that.

```#define LED_1_PIN 11
#define LED_2_PIN 10
#define LED_3_PIN 9
#define POTENTIOMETER_PIN A0

#define LED_NUMBER 3

void setup()
{
pinMode(LED_1_PIN, OUTPUT);
pinMode(LED_2_PIN, OUTPUT);
pinMode(LED_3_PIN, OUTPUT);
}

void loop()
{
int ledChoice = potentiometerValue / (1024 / LED_NUMBER);

if (ledChoice > LED_NUMBER - 1) {
ledChoice = LED_NUMBER - 1;
}

if (ledChoice == 0) {
digitalWrite(LED_1_PIN, HIGH);
digitalWrite(LED_2_PIN, LOW);
digitalWrite(LED_3_PIN, LOW);
}
else if (ledChoice == 1) {
digitalWrite(LED_1_PIN, LOW);
digitalWrite(LED_2_PIN, HIGH);
digitalWrite(LED_3_PIN, LOW);
}
else {
digitalWrite(LED_1_PIN, LOW);
digitalWrite(LED_2_PIN, LOW);
digitalWrite(LED_3_PIN, HIGH);
}
}```

Let’s now analyze this code.

### Create some defines

```#define LED_1_PIN 11
#define LED_2_PIN 10
#define LED_3_PIN 9
#define POTENTIOMETER_PIN A0

#define LED_NUMBER 3```

First we create a define for each pin we are going to use, so it will be more practical. We also use a define which contains the number of LEDs we have in the circuit.

### Setup the pins

```void setup()
{
pinMode(LED_1_PIN, OUTPUT);
pinMode(LED_2_PIN, OUTPUT);
pinMode(LED_3_PIN, OUTPUT);
}```

In the void setup(), we set the mode for each LED to OUTPUT. Nothing to do for the potentiometer.

### Read the potentiometer and choose which LED to power on

Now is the most important part of the program.

```void loop()
{
int ledChoice = potentiometerValue / (1024 / LED_NUMBER);

if (ledChoice > LED_NUMBER - 1) {
ledChoice = LED_NUMBER - 1;
}```

We do that in the void loop(), so we can repeat this action as fast as possible.

So, first, we read the value from the potentiometer. This will give us an integer number between 0 and 1023.

Then, we transform this number into a number that is either:

• 0 (for LED 1)
• 1 (for LED 2)
• 2 (for LED 3)

In programming we almost always start to count from zero, so let’s keep this convention here.

When we compute (1024 / 3), we get about 1/3th of the potentiometer range. Then we just need to divide the value we read by this number, and we’ll get the LED index we want.

Note: everything will be rounded here, so it’s possible that the max value (1023) gives you something bigger than 2. As we want a number between 0 and 2 (for 3 LEDs), we add a check with the if structure. This check will make sure the max number will be 2. This solution is probably better than trying to compute an exact number. In the end, precision is not really an important factor here.

### Power on the corresponding LED with the computed index

```  if (ledChoice == 0) {
digitalWrite(LED_1_PIN, HIGH);
digitalWrite(LED_2_PIN, LOW);
digitalWrite(LED_3_PIN, LOW);
}
else if (ledChoice == 1) {
digitalWrite(LED_1_PIN, LOW);
digitalWrite(LED_2_PIN, HIGH);
digitalWrite(LED_3_PIN, LOW);
}
else {
digitalWrite(LED_1_PIN, LOW);
digitalWrite(LED_2_PIN, LOW);
digitalWrite(LED_3_PIN, HIGH);
}
}```

Now that we have the index, we just need to check it and power on only one LED accordingly. For example, if we have 0, then we power on LED 1 and we power off the 2 other ones, etc.

Note: we could also have used a switch structure here, which would have looked like this.

```  switch(ledChoice) {
case 0:
digitalWrite(LED_1_PIN, HIGH);
digitalWrite(LED_2_PIN, LOW);
digitalWrite(LED_3_PIN, LOW);
break;
case 1:
digitalWrite(LED_1_PIN, LOW);
digitalWrite(LED_2_PIN, HIGH);
digitalWrite(LED_3_PIN, LOW);
break;
case 2:
digitalWrite(LED_1_PIN, LOW);
digitalWrite(LED_2_PIN, LOW);
digitalWrite(LED_3_PIN, HIGH);
break;
default:
// nothing
break;
}
}```

## Select LED with potentiometer: Same, but with arrays and functions

Using this code with 3 LEDs is fine, but we start to see a few repetitions. Now imagine working with 8 LEDs, you’d have to duplicate a lot of code, which is not a great thing to do.

So, let’s see how to control multiple LEDs with the potentiometer – using arrays and functions.

For more details on the LED part, check out: how to handle multiple LEDs with arrays and functions.

```#define LED_1_PIN 11
#define LED_2_PIN 10
#define LED_3_PIN 9
#define POTENTIOMETER_PIN A0

#define LED_NUMBER 3

byte LEDPinArray[LED_NUMBER] = { LED_1_PIN,
LED_2_PIN,
LED_3_PIN };

void initAllLEDs()
{
for (int i = 0; i < LED_NUMBER; i++) {
pinMode(LEDPinArray[i], OUTPUT);
}
}

void powerOnSelectedLEDOnly(int index)
{
for (int i = 0; i < LED_NUMBER; i++) {
if (i == index) {
digitalWrite(LEDPinArray[i], HIGH);
}
else {
digitalWrite(LEDPinArray[i], LOW);
}
}
}

void setup()
{
initAllLEDs();
}

void loop()
{
int ledChoice = potentiometerValue / (1024 / LED_NUMBER);

if (ledChoice > LED_NUMBER - 1) {
ledChoice = LED_NUMBER - 1;
}

powerOnSelectedLEDOnly(ledChoice);
}```

And again, let’s break down this code, this time to highlight the differences with the previous one.

### Defines and global variables

```#define LED_1_PIN 11
#define LED_2_PIN 10
#define LED_3_PIN 9
#define POTENTIOMETER_PIN A0

#define LED_NUMBER 3

byte LEDPinArray[LED_NUMBER] = { LED_1_PIN,
LED_2_PIN,
LED_3_PIN };```

We create the same defines as before, and we add a global variable, which is actually an array containing all the LED pins we are going to use.

This way, for example you can access LED 2 by writing “LEDPinArray[1]”.

### Setup the pins

```void initAllLEDs()
{
for (int i = 0; i < LED_NUMBER; i++) {
pinMode(LEDPinArray[i], OUTPUT);
}
}```

and

```void setup()
{
initAllLEDs();
}```

With a for loop, we go through each element of the LEDPinArray. And we use pinMode() on each element to set the pin as OUTPUT.

We put this for loop inside a function named initAllLEDs(), and we call this function in the void setup(). As you can see, now the void setup() is much cleaner, and this code won’t change whether you have 3 or 30 LEDs.

### Read the potentiometer and choose which LED to power on

```void loop()
{
int ledChoice = potentiometerValue / (1024 / LED_NUMBER);

if (ledChoice > LED_NUMBER - 1) {
ledChoice = LED_NUMBER - 1;
}```

This code is actually the same as before. This is an action we do once, which is related to the potentiometer. So, no need for arrays here.

We just compute the LED index as previously done.

### Power on the corresponding LED with the computed index

```void powerOnSelectedLEDOnly(int index)
{
for (int i = 0; i < LED_NUMBER; i++) {
if (i == index) {
digitalWrite(LEDPinArray[i], HIGH);
}
else {
digitalWrite(LEDPinArray[i], LOW);
}
}
}```

and

```   powerOnSelectedLEDOnly(ledChoice);
}```

Again, and this is similar to what we did to initialize the LEDs, we create a for loop to go through the LED pin array. We put the for loop inside a function which receives one parameter: the index for the LED.

In the for loop, we simply power off any LED that does not correspond to the given index, and we only power on the LED for which the pin number index matches.

And to run this code, we just call the function from the void loop(). We pass the LED index we just computed before to the function.

## Change LED brightness for multiple LEDs using the potentiometer

Let’s now do something a bit different. Instead of powering on a different LED depending on the knob position, we will adjust the brightness for all LEDs.

Basically, if you know how to set the brightness for one LED using a potentiometer, this won’t be much harder to do.

### Circuit

One very important thing to check on the Arduino circuit: all LEDs must be connected to pins that are PWM compatible. As you can see on the circuit I’ve made, pins 9, 10, and 11 on Arduino all have a “~” near the number. This means we can use PWM on those pins.

The number of PWM compatible digital pins can vary depending on the Arduino board. This will be your main limitation here. On Arduino Uno you have 6 of those, so you can do this application for max 6 LEDs.

### Code

```#define LED_1_PIN 11
#define LED_2_PIN 10
#define LED_3_PIN 9
#define POTENTIOMETER_PIN A0

#define LED_NUMBER 3

byte LEDPinArray[LED_NUMBER] = { LED_1_PIN,
LED_2_PIN,
LED_3_PIN };

void initAllLEDs()
{
for (int i = 0; i < LED_NUMBER; i++) {
pinMode(LEDPinArray[i], OUTPUT);
}
}

void setBrightnessForAllLEDs(int brightness)
{
for (int i = 0; i < LED_NUMBER; i++) {
analogWrite(LEDPinArray[i], brightness);
}
}

void setup()
{
initAllLEDs();
}

void loop()
{
int brightness = map(potentiometerValue, 0, 1023, 0, 255);

setBrightnessForAllLEDs(brightness);
}```

This code contains some parts that are identical to the previous code we did:

• Create some defines, and a global array for the LED pins.
• Init all LEDs from the setup(), by calling a function containing an array.

Now, here is the code specific to this brightness application.

### Compute brightness to apply to LEDs

```void loop()
{
int brightness = map(potentiometerValue, 0, 1023, 0, 255);```

We first read the value from the potentiometer.

Then, we transform this value from the range 0-1023 to the range 0-255, using the Arduino map() function. This way, the higher the potentiometer value, the higher the brightness.

### Apply the brightness to all LEDs

```void setBrightnessForAllLEDs(int brightness)
{
for (int i = 0; i < LED_NUMBER; i++) {
analogWrite(LEDPinArray[i], brightness);
}
}```

and

```  setBrightnessForAllLEDs(brightness);
}```

We use the powerful combination of arrays + functions to create a clean and scalable piece of code.

In the for loop where we go through each LED pin, we use analogWrite() to apply the computed brightness to each LED.

Also, don’t forget to call the function directly after computing the brightness in the void loop(), passing the brightness as a parameter.

## Conclusion – Control multiple LEDs with a potentiometer

You can now control multiple LEDs with a potentiometer, whether it’s to select one LED, or to apply a dynamic brightness to all/some LEDs.

Here are a few improvements you could work on, to get more practice:

• To select the LED, you could set a different rule, which is not directly proportional to the potentiometer value.
• Also, instead of just powering on one LED, you could power on more LEDs when you increase the potentiometer value. For example, at first you power on LED 1, then LED 1 and LED 2, and then all LEDs.
• You can also combine the 2 applications – select an LED and apply brightness. To do that, you can either use 2 potentiometers – 1 for selecting an LED, 1 for setting the brightness, or a combination of a push button to select the LED, and potentiometer to set the brightness.

Did you find this tutorial useful?

Do you want to learn Arduino from scratch?

If yes, this course is for you:

LEARN HOW TO PROGRAM ROBOTS

Did you find this tutorial useful?

Do you want to become better at programming robots, with Arduino, Raspberry Pi, or ROS2?

If yes, subscribe to receive exclusive content and special offers!