Learn to code

ARDUINO & CODES

Previous Post
Adjusting the color of a RGB LED
Next Post
Controlling and powering NeoPixels with Arduino

Control the sequence LED with a potentiometer and Arduino

in Arduino Visual Output

In this tutorial, we will control the sequence of LED with a potentiometer. We will use 3 LEDs for this. Individually, the LEDs will be turned on depending on the input from the potentiometer. Besides that, we ware using a potentiometer with a map function that returns 3 values. Furthermore, we are using arrays and a for loop to achieve the desired result.

Parts you will need

Arduino Uno Rev3 Arduino Uno Rev3 × 1
Breadboard 400 point Breadboard 400 point × 1
Dupont Wires Dupont Wires × 11
220 ohm resistor 220 ohm resistor × 3
10K Potentiometer 10K Potentiometer × 1

Arduinoplatform is a participant in several affiliate programs. This means that I will earn a commision if you buy a product from the affiliated websites through clicking on the links provided above.

BreadBoard Layout

As you can see in the diagram below we will use a potentiometer with three LEDs connected to the Arduino Uno Board.

The Code

//Sketch: LED sequence with potentiometer
//Turns on a series of LEDs proportional to a value of an analog sensor.
const int nrLeds = 3; // total number of LEDs
const int ledPins[] = { 2, 3, 4}; // array of LEDs pins
const int analogInPin = 0; // Analog input pin for potentiometer
const int wait = 30; // delay
const boolean LED_ON = LOW; // to turn the LED on
const boolean LED_OFF = HIGH; // to turn the LED off
int sensorValue = 0; // value read from the sensor
int ledLevel = 0; // sensor value converted into LED sequence
void setup() {
  for (int led = 0; led < nrLeds; led++)
  {
    pinMode(ledPins[led], OUTPUT); // make all the LED pins outputs
  }
}
void loop() {
  sensorValue = analogRead(analogInPin); // read the analog in value
  ledLevel = map(sensorValue, 0, 1023, 0, nrLeds); // map to the number of LEDs
  for (int led = 0; led < nrLeds; led++)
  {
    if (led < ledLevel ) {
      digitalWrite(ledPins[led], LED_ON); // turn on pins less than the level
    }
    else {
      digitalWrite(ledPins[led], LED_OFF); // turn off pins higher than the
    }
  }
}

Code Explanation

This code is a bit complicated and you will need some knowledge about the different commands and functions if you want to understand it.

Firstly, we are declaring a couple of variables. The first is the number of LEDs that we are using. We name this variable nrLeds. Second, we declare an array to hold the pins connected to the LEDs. To change the number of LEDs, you can add or remove the pin numbers in this array. Remember to change the variable nrLeds as well. We will need a variable to hold to sensor value of the potentiometer as well as a variable to convert the value of the potentiometer into levels that we can use for turning the LEDs on or off. The last variables that we declare are boolean variables. We will use these variables to turn the LED on or off.

const int nrLeds = 3; // total number of LEDs
const int ledPins[] = { 2, 3, 4}; // array of LEDs pins
const int analogInPin = 0; // Analog input pin for potentiometer
const int wait = 30; // delay 
const boolean LED_ON = LOW; // to turn the LED on
const boolean LED_OFF = HIGH; // to turn the LED off
int sensorValue = 0; // value read from the sensor
int ledLevel = 0; // sensor value converted into LED sequence

 

In the void setup() part of our sketch, we will set the pins that are connected to the LEDs to an output mode. We will use a for loop for this.

for (int led = 0; led < nrLeds; led++)
  {
    pinMode(ledPins[led], OUTPUT); // make all the LED pins outputs
  }

 

We finally arrived at our void loop() part. Firstly, we need to read the value of the potentiometer and use the map function to rearrange the value 0 – 1023 to and value between 0 – nrLEDs, in our case 3.  The Arduino map function is used to calculate the number of LEDs that should be lit as
a proportion of the sensor value. The code loops through each LED, turning it on if the proportional value of the sensor is greater than the LED number. For example, if the sensor value is 0, no pins are on. When the sensor is at maximum value, all the LEDs will be lit.

sensorValue = analogRead(analogInPin); // read the analog in value
  ledLevel = map(sensorValue, 0, 1023, 0, nrLeds); // map to the number of LEDs

 

After the reading and mapping are completed we will use a for loop to control the LEDs.

for (int led = 0; led < nrLeds; led++)
  {
    if (led < ledLevel ) {
      digitalWrite(ledPins[led], LED_ON); // turn on pins less than the level
    }
    else {
      digitalWrite(ledPins[led], LED_OFF); // turn off pins higher than the
    }
  }

 

You are now able to control the three LEDs by turning the shaft of the potentiometer.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu