Using pot to roll through multiple led's.

Hey all,
First time posting, awesome forum! Im just trying to get my head around all this programming, I basically have almost nill programming skills but am learning through all the tutorials and youtube videos.

What I am trying to do is setup multiple led’s on the breadboard which I have done, going from pins 2-13. I’m having a bit of trouble with the code trying to figure out how to make a single led manually roll through to the next one depending on pot position.

And also mod the code so instead of just one lighting up, the next one lights up, and the next one until all are lit up to pin 13 depending on pot position. I hope this makes sence.

The code I have already is:

int val = 0; // variable to store the read value
int potPin = 0; // potentiometer connected to analog pin 0


void setup()
for(int i = 2; i<13; i++)
pinMode(i, OUTPUT);


void loop()

val = analogRead(potPin);


I know its half missing but if theres any tutorials regarding how to do this I would greatly appreciate it. Its hard because there can be a dozen ways to do one thing and people have their own way of programming.

Thanks in advance.


Not totally sure how you want the LEDs to turn off again but take a look at this:

/*Alittle something for my dad's H0 modeltrain
*this is the light for a swing from the '50s
*based on a 8 LED kit from some web retailer.
*the code is simple, as were the Tivoli light in the '50s
//LED Pin Variables
int ledPins[] = {2,3,4,5,6,7,8,9,10,11}; //An array to hold the pin each LED is connected to
                                   //i.e. LED #0 is connected to pin 2, LED #1, 3 and so on
                                   //to address an array use ledPins[0] this would equal 2
                                   //and ledPins[9] would equal 11
 * setup() - this function runs once when you turn your Arduino on
 * We the three control pins to outputs
void setup()
  //Set each pin connected to an LED to output mode (pulling high (on) or low (off)
  for(int i = 0; i < 10; i++){         //this is a loop and will repeat ten times
      pinMode(ledPins[i],OUTPUT); //we use this to set each LED pin to output
  }                                   //the code this replaces is below
  /* (commented code will not run)
   * these are the lines replaced by the for loop above they do exactly the
   * same thing the one above just uses less typing
  (end of commented code)*/
 * loop() - this function will start after setup finishes and then repeat
 * we call a function called oneAfterAnother(). 
void loop()                     // run over and over again

void oneAfterAnotherLoop(){
  int delayTime = 350; //the time (in milliseconds) to pause between LEDs
                       //make smaller for quicker switching and larger for slower
//Turn Each LED on one after another
  for(int i = 0; i <= 9; i++){
    digitalWrite(ledPins[i], HIGH);  //Turns on LED #i each time this runs i
    delay(delayTime);                //gets one added to it so this will repeat 
  }                                  //8 times the first time i will = 0 the final
                                     //time i will equal 7;
//Turn Each LED off one after another
  for(int i = 9; i >= 0; i--){  //same as above but rather than starting at 0 and counting up
                                //we start at seven and count down
    digitalWrite(ledPins[i], LOW);  //Turns off LED #i each time this runs i
    delay(delayTime);                //gets one subtracted from it so this will repeat 
  }                                  //8 times the first time i will = 7 the final
                                     //time it will equal 0

Hope that it can help you a little…

Hi, thanks for your help.

That sketch is good, but is there a way to adjust lights through potentiometer? Like when pot is at 0, the first led lights up, and when you turn the pot a little, the second led lights up and so on. Then when you turn the pot back to 0, each led turns off in sequence from pins 13 to 2.

And also to make a minor change to the code so instead of them all lighting up in sequence, its just the one. i.e single led on pin 2 moves to pin 13 according to position of pot.


Is this homework?
We are getting a lot of very similar questions lately.

haha, nah, just trying to learn the basics that's all. I can run the leds in sequence on their own and do arrays and stuff. But not adjust it with the potentiometer.

I did try searching here and google for what I am after but they are all (knight rider and led chaser) just adjusting the speed at which the led travels. Like when the pot is at one end the leds go slow and when you turn up the pot they get faster.

So when you read a pot you get a value from 0 to 1023,
So for 11 LEDs that will be a range of 1023/11 for each LED.
Therefore simply step through values in steps of 11 with a 'for' loop and set or clear the LEDs according to if the pot reading is greater are less than the step you are currently evaluating.

I found kind of what I am looking for:,17696.0.html

I have modified it with the extra led’s and got:

byte ledPin = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; //pins used for LED’s
int potPin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin

void setup()


void loop()

val = analogRead(potPin); // reads the value of the potentiometer (value between 0 and 1023)
for (int i = 0; i < 13; i++) {
if( (i * 93) > val) { digitalWrite(ledPin*, HIGH);}*

  • else*
    _ { digitalWrite(ledPin*, LOW);}_
    It works well although the led’s are all quite dim? Once I have a basic code on how this works then I will break it down and try to experiment and understand how to use the ‘for’ and ‘if else’ statements for potentiometers to move the led’s in sequence.
    I have worked out how to use a pot to turn a little dc motor forward and reverse and forward only.

Please post code correctly, read the how to use the forum sticky post.

Define pins as outputs in the setup function. That will make the LEDs brighter. Hope you have resistors on them.

No probs Mike. Yep I have 10k resisters on all of them.
The final code I have is

byte ledPin[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; //pins used for LED's
int potPin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin

void setup()
  for(int i = 0; i < 13; i++){   
void loop()
 val = analogRead(potPin);            // reads the value of the potentiometer (value between 0 and 1023)
  for (int i = 0; i < 13; i++) {
    if( (i * 102) > val) { digitalWrite(ledPin[i], HIGH);}
     { digitalWrite(ledPin[i], LOW);}

I set the pins as output and they are brighter now.
If say instead of a single LED moving, how would I change the code so the previous led’s stay on, kind of like a led bar.

Thanks for your help!

Write the code so that you keep turning on LEDs until
val > (i * 102)
then turn them off.

Now i understand, then my reply was a little off.
But this is very useful, i'll might lend this some time :slight_smile: