Arduino Starter’s Guide  (2/7): Controlling Multiple LEDs
Arduino Starter's Guide (2/7): Controlling Multiple LEDs

Arduino Starter's Guide (2/7): Controlling Multiple LEDs

01 Overview

After flashing a single LED, this next circuit steps up to controlling multiple LEDs with different light sequences. It's a good setup for experimenting with your own programs and getting a feel for how Arduino works, while also introducing a few simple programming methods that help keep your code small, like arrays, loops, and functions.

Project Use Case

This is the second entry in the Arduino Starter's Guide series, aimed at beginners who've already tried blinking a single LED and are ready to control several outputs at once. It's a solid introduction to writing reusable functions and using loops instead of repeating the same lines of code over and over.

02 Hardware and Software Components

Gather everything below before you start. This is your checklist — names, models, and versions only.

Hardware Components

Component Description
Arduino Uno Runs the sketch and drives each LED through its digital pins.
LED, 5mm (x5) The five lights being controlled by the animations in this sketch.
100 Ohm Resistor (x5) One per LED, limits current to protect each LED from burning out.
Breadboard Used to prototype the wiring between the Arduino and the five LEDs.

Software Tools

Software Details
Arduino IDE Used to write and upload the sketch to the Arduino Uno.

03 Application Discussion

Here is what each component does and why it is part of this project.

LEDs and Resistors

Each LED is paired with its own 100 ohm resistor to limit the current flowing through it, protecting the LED from drawing more current than it can safely handle. Wiring five LED-resistor pairs to five separate digital pins lets the Arduino switch each one independently, which is what makes the different light sequences in this sketch possible.

Arduino Uno

The Arduino Uno runs the sketch that decides which LEDs are on or off at any given moment. Rather than wiring separate logic for each LED, this project stores all five pin numbers in a single array and uses loops and functions to control them, which keeps the code shorter and easier to extend.

04 Hardware Setup

Wire the components to the board using the table below.

LEDs to Arduino

LED Arduino Pin Description
LED #0 Pin 2 Through a 100 ohm resistor
LED #1 Pin 3 Through a 100 ohm resistor
LED #2 Pin 4 Through a 100 ohm resistor
LED #3 Pin 5 Through a 100 ohm resistor
LED #4 Pin 6 Through a 100 ohm resistor

Connect each LED's cathode (short leg) to the breadboard's ground rail, and that rail to the Arduino's GND pin.

05 Software Setup

Follow these steps in order. Do not skip any step.

Step 1 — Install the Arduino IDE

  1. Download and install the Arduino IDE.

Step 2 — Upload the Code

  1. Open the sketch from Section 06 in the Arduino IDE.
  2. Select the correct board and port for your Arduino Uno.
  3. Click Upload.

06 Code

Copy the file below into a new Arduino sketch as described in the Software Setup section. Read the Code Breakdown section to understand what each part does.

multiple_leds.ino

Arduino / C++
/*     ---------------------------------------------------------
 *     |  Arduino Experimentation Kit Example Code             |
 *     |  CIRC-02 .: 5 LED Fun :. (Multiple LEDs)   |
 *     ---------------------------------------------------------
 *
 *  A few Simple LED animations
 *
 * For more information on this circuit http://tinyurl.com/d2hrud
 *
 */

//LED Pin Variables
int ledPins[] = {2,3,4,5,6}; //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


/*
 * 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 < 8; i++){         //this is a loop and will repeat eight 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
  pinMode(ledPins[0],OUTPUT);
  pinMode(ledPins[1],OUTPUT);
  pinMode(ledPins[2],OUTPUT);
  pinMode(ledPins[3],OUTPUT);
  pinMode(ledPins[4],OUTPUT);

}


/*
 * loop() - this function will start after setup finishes and then repeat
 * we call a function called oneAfterAnother(). if you would like a different behaviour
 * uncomment (delete the two slashes) one of the other lines
 */
void loop()                     // run over and over again
{
  oneAfterAnotherNoLoop();   //this will turn on each LED one by one then turn each off
  //oneAfterAnotherLoop();   //does the same as oneAfterAnotherNoLoop but with
                             //much less typing
  //oneOnAtATime();          //this will turn one LED on then turn the next one
                             //on turning the
                             //former off (one LED will look like it is scrolling
                             //along the line
  //pingPong();
  //randomLED();
}

/*
 * oneAfterAnotherNoLoop() - Will light one LED then delay for delayTime then light
 * the next LED until all LEDs are on it will then turn them off one after another
 *
 * this does it without using a loop which makes for a lot of typing.
 * oneOnAtATimeLoop() does exactly the same thing with less typing
 */
void oneAfterAnotherNoLoop(){
  int delayTime = 100; //the time (in milliseconds) to pause between LEDs
                       //make smaller for quicker switching and larger for slower
  digitalWrite(ledPins[0], HIGH);  //Turns on LED #0 (connected to pin 2 )
  delay(delayTime);                //waits delayTime milliseconds
  digitalWrite(ledPins[1], HIGH);  //Turns on LED #1 (connected to pin 3 )
  delay(delayTime);                //waits delayTime milliseconds
  digitalWrite(ledPins[2], HIGH);  //Turns on LED #2 (connected to pin 4 )
  delay(delayTime);                //waits delayTime milliseconds
  digitalWrite(ledPins[3], HIGH);  //Turns on LED #3 (connected to pin 5 )
  delay(delayTime);                //waits delayTime milliseconds
  digitalWrite(ledPins[4], HIGH);  //Turns on LED #4 (connected to pin 6 )
  delay(delayTime);                //waits delayTime milliseconds


//Turns Each LED Off

  digitalWrite(ledPins[4], LOW);  //Turns on LED #3 (connected to pin 5 )
  delay(delayTime);                //waits delayTime milliseconds
  digitalWrite(ledPins[3], LOW);  //Turns on LED #4 (connected to pin 6 )
  delay(delayTime);                //waits delayTime milliseconds
  digitalWrite(ledPins[2], LOW);  //Turns on LED #5 (connected to pin 7 )
  delay(delayTime);                //waits delayTime milliseconds
  digitalWrite(ledPins[1], LOW);  //Turns on LED #6 (connected to pin 8 )
  delay(delayTime);                //waits delayTime milliseconds
  digitalWrite(ledPins[0], LOW);  //Turns on LED #7 (connected to pin 9 )
  delay(delayTime);                //waits delayTime milliseconds
}

/*
 * oneAfterAnotherLoop() - Will light one LED then delay for delayTime then light
 * the next LED until all LEDs are on it will then turn them off one after another
 *
 * this does it using a loop which makes for a lot less typing.
 * than oneOnAtATimeNoLoop() does exactly the same thing with less typing
 */
void oneAfterAnotherLoop(){
  int delayTime = 100; //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 <= 5; 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 = 5; 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


}

/*
 * oneOnAtATime() - Will light one LED then the next turning off all the others
 */
void oneOnAtATime(){
  int delayTime = 100; //the time (in milliseconds) to pause between LEDs
                       //make smaller for quicker switching and larger for slower

  for(int i = 0; i <= 5; i++){
    int offLED = i - 1;  //Calculate which LED was turned on last time through
    if(i == 0) {         //for i = 1 to 7 this is i minus 1 (i.e. if i = 2 we will
      offLED = 5;        //turn on LED 2 and off LED 1)
    }                    //however if i = 0 we don't want to turn of led -1 (doesn't exist)
                         //instead we turn off LED 7, (looping around)
    digitalWrite(ledPins[i], HIGH);     //turn on LED #i
    digitalWrite(ledPins[offLED], LOW); //turn off the LED we turned on last time
    delay(delayTime);
  }
}

/*
 * inAndOut() - This will turn on the two middle LEDs then the next two out
 * making an in and out look
 */
void pingPong()
{
  int index;
  int delayTime = 100; // milliseconds to pause between LEDs
                       // make this smaller for faster switching

  // step through the LEDs, from 0 to 7

  for(index = 0; index <= 5; index++)
  {
    digitalWrite(ledPins[index], HIGH);  // turn LED on
    delay(delayTime);                    // pause to slow down
    digitalWrite(ledPins[index], LOW);   // turn LED off
  }

  // step through the LEDs, from 7 to 0

  for(index = 5; index >= 0; index--)
  {
    digitalWrite(ledPins[index], HIGH);  // turn LED on
    delay(delayTime);                    // pause to slow down
    digitalWrite(ledPins[index], LOW);   // turn LED off
  }
}


/*
randomLED()

This function will turn on random LEDs. Can you modify it so it
also lights them for random times?
*/

void randomLED()
{
  int index;
  int delayTime;

  // The random() function will return a semi-random number each
  // time it is called. See http://arduino.cc/en/Reference/Random
  // for tips on how to make random() even more random.

  index = random(5);    // pick a random number between 0 and 7
  delayTime = 100;

  digitalWrite(ledPins[index], HIGH);  // turn LED on
  delay(delayTime);                    // pause to slow down
  digitalWrite(ledPins[index], LOW);   // turn LED off
}

07 Code Breakdown

Here is what each part of the code does. Read this after uploading.

Key Variables

int ledPins[] = {2,3,4,5,6} is an array holding the pin number each LED is connected to. Instead of five separate variables, this lets the code refer to any LED by its index, for example ledPins[0] equals 2.

Key Functions

setup()

Sets each LED pin to output mode using a for loop instead of writing five separate pinMode() calls, which is shown commented out below the loop for comparison.

loop()

Calls whichever animation function isn't commented out. By default, oneAfterAnotherNoLoop() runs; commenting that line out and removing the // from a different one switches to a different animation.

oneAfterAnotherNoLoop()

Turns each LED on one at a time with a short delay between them, then turns them off the same way, all written out explicitly without a loop.

oneAfterAnotherLoop()

Does the same thing as oneAfterAnotherNoLoop(), but uses a for loop to turn the LEDs on in sequence and a second for loop counting backward to turn them off, with far less repeated code.

oneOnAtATime()

Turns one LED on while turning off whichever LED was on the previous step, creating a single light that appears to scroll along the row.

pingPong()

Steps through the LEDs from first to last, then from last to first, turning each on and back off in sequence to create a back-and-forth effect.

randomLED()

Picks a random LED using random(), turns it on briefly, then off, so each call lights up a different, unpredictable LED.

Learning More About Functions

Each animation above is written as its own function so the loop() function can stay short and simply call whichever one is active. For a deeper look at how functions work in Arduino, see the Starting Electronics tutorial linked in the References section.

08 Testing and Calibration

After uploading, verify each of the following to confirm the system is working correctly.

Default Animation

Confirm the five LEDs light up one after another from pin 2 through pin 6, then turn off in the same one-after-another order. This is the default oneAfterAnotherNoLoop() animation.

Switching Animations

To try a different animation, comment out the oneAfterAnotherNoLoop(); line in loop() by adding // in front of it, then remove the // from the animation you want to try instead. Only one animation line should be active (uncommented) at a time, then re-upload the sketch.

Common Issue: If nothing lights up, or the pattern looks broken, double check only one animation call in loop() is uncommented, and see the note at the top of this post about the array bounds in the loop functions.

09 System Demonstration

Three of the alternate animations available in this sketch:

  • Random — lights a randomly chosen LED briefly, then off, over and over.
  • One At A Time — a single lit LED appears to scroll along the row as each step turns one on and the previous one off.
  • Ping Pong — LEDs light in sequence from one end to the other, then reverse direction, back and forth.

10 Conclusion

This project builds on single-LED blinking by introducing arrays, loops, and functions, the building blocks that make it practical to control several outputs without repeating the same code over and over. With five simple animations to try, it's a solid foundation for writing more advanced Arduino sketches later in the series.

Possible Improvements and Future Enhancements

  • Add a potentiometer to control the animation speed in real time instead of a fixed delay.
  • Add a push button to cycle between animations without re-uploading the sketch.
  • Extend the array to control more than five LEDs.

11 References

  • startingelectronics.org — Learn to Program Course: Functions
  • tinyurl.com/d2hrud — Original circuit reference (CIRC-02, Arduino Experimentation Kit)

12 Project Authors

  • Kevin Montenegro

Quality checked by

  • Alexander Maiso
  • Jhon Ronan Limbadan
Arduino Starter's Guide (2/7): Controlling Multiple LEDs – CreateLabz
Arduino, Blink, Function, Knowledgebase, Led, Multiple leds, Oneatatime, Pingpong, Starter kit

Leave a comment

All comments are moderated before being published