Random LED Patterns with Arduino – LED Light Show

Back to Blog
Random LED Patterns with Arduino - LED Light Show - Random LED Patterns with Arduino - LED Light Show - Random LED Patterns with Arduino - LED Light Show - Random LED Patterns with Arduino - LED Light Show

Random LED Patterns with Arduino – LED Light Show

Most of us tried Blinking LED using Arduino as a Beginner. Let’s do something cool using LEDs with Arduino. In this project, we will make LEDs blink in different patterns in a random manner. The program has totally 6 different patterns and the Arduino chooses the patterns randomly by itself. You can include more patterns if you like.

Components Required:

Arduino Pro Mini (or) Arduino Uno/Nano/Micro – Any One

Breadboard – 1

LEDs – 18 Nos (Blue)

100 Ohm Resistor – 18

Jumper Wires

Note: For other LEDs than Blue use 220 Ohm Resistors

Circuit diagram:

Circuit DiagramThere are totally 18 LEDs, so we need 18 Pins of the Arduino to control the LEDs. We will be using Digital pins 2 to 13 and Analog Pins from A0 (14) to A5 (19). In Arduino Uno/Pro Mini/Nano boards the Analog pins can also be mentioned in numbers instead of A0-A5. It will be mapped automatically by Arduino IDE.

So you can directly mention the Pin numbers from 2 to 19 (A5) instead of mentioning A0, A1, A2, A3, A4 and A5 separately.

Analog pins and its digital pin mapping for Arduino Uno/Nano/Pro Mini/Micro

A0 is equivalent to 14

A1 is equivalent to 15

A2 is equivalent to 16

A3 is equivalent to 17

A4 is equivalent to 18

A5 is equivalent to 19

Circuit Connections:

First, place all the LEDs on the breadboard. The Cathode terminal (Short Leg) should be connected to Ground (GND) via a Resistor and the Anode terminal (Long Leg) of all LED will be connected to a sequence of Arduino Pins as shown in the Table below.

Arduino LED Pin connections
Now connect the Power Supply +V and Ground. The Pro Mini has an inbuilt voltage regulator so the RAW pin can accept from 5V to 12V DC Power supply. Make sure the Positive and Negative terminals are connected correctly.

Now Power ON and Upload the code using FDTI Programmer in case of Pro Mini Boards. Otherwise, use the supported cables for the boards.

Code:

/* Arduino LED Patterns/Show Light with 18 LED */
//Setting Up the Pins for LEDs

void setup()

{

for (int pin = 2; pin <= 19; pin++)

  {

    pinMode(pin,OUTPUT);

  }

}

//Main Loop - Switches different LED Patterns

void loop()

{
int pickme = random(1,20); // picks a random pattern of LED patterns

switch(pickme)

  {

  case 1:

    onrun(random(20,50));

  break;

  case 2:

    alternate(random(80,100));

  break;

  case 3:

    offrun(random(20,50));

  break;

  case 4:

    stack(random(30,50));

  break;

  case 5:

    chaser(random(80,100));

  break;

  case 6:

    fadealter(random(80,100));

  break;

  }

}

void clearall()

{

  for (int pin = 2; pin <= 19; pin++)

  {

  digitalWrite(pin,LOW);

  }

}


void fillall()

{

  for (int pin = 2; pin <= 19; pin++)

  {

  digitalWrite(pin, HIGH);

  }

}


//One ON LED Run and all other OFF

void onrun(int delaytime)

{

  for(int pin = 2; pin <= 19; pin++)

  {

  clearall();

  digitalWrite(pin, HIGH);

  delay(delaytime);

  }

  for(int pin = 18; pin >= 2; pin--)

  {

  clearall();

  digitalWrite(pin, HIGH);

  delay(delaytime);

  }

}

//One OFF LED Run and all other OFF

void offrun(int delaytime)

{

  for(int pin = 2; pin <= 19; pin++)

  {

  fillall();

  digitalWrite(pin, LOW);

  delay(delaytime);

  }

  for(int pin = 18; pin >= 2; pin--)

  {

  fillall();

  digitalWrite(pin, LOW);

  delay(delaytime);

  }

}


//Flashing all LEDs ON and OFF

void flash(int delaytime)

{

  for(int i = 1; i <=20; i++)

  {

  clearall();

  delay(delaytime);

  fillall();

  delay(delaytime);

  }

}


//Flashing LED in Fade manner

void fadeflash(int delaytime)

{

clearall();

int newdelay = delaytime / 5;

  for(int fade = 0; fade <= 255; fade += 5)

  {

    for(int pin = 2; pin <= 19; pin++)

    {

    analogWrite(pin, fade);

    }

  delay(newdelay);

  }



  for(int fade = 255; fade >= 0; fade -= 5)

  {

    for(int pin = 2; pin <= 19; pin++)

    {

    analogWrite(pin, fade);

    }

  delay(newdelay);

  }

}

//Alternatively Fade & Brightens

void fadealter(int delaytime)

{

clearall();

int newdelay = delaytime / 5;

  for(int fade = 0; fade <= 255; fade += 5)

  {

    for(int i = 2; i <= 18; i+=2)

    {

    analogWrite(i, fade);

    }

    for (int j = 3; j <= 19; j += 2)

    {

    analogWrite(j, 255-fade);

    }

  delay(newdelay);

  }



  for(int fade = 255; fade >= 0; fade -= 5)

  {

    for(int i = 2; i <= 18; i+=2)

    {

    analogWrite(i, fade);

    }



  for (int j = 3; j <= 19; j += 2)

  {

  analogWrite(j, 255-fade);

  }

  delay(newdelay);

  }

}

//Alternate Flash - Similar to Flash but alternate LEDs

void alternate(int delaytime)

{

  for (int n = 1; n <= 5; n++)

  {

  clearall();

  for (int i = 2; i <= 18; i += 2)

    {

    digitalWrite(i, HIGH);

    }

  delay(delaytime);

  clearall();

  for (int j = 3; j <= 19; j += 2)

    {

    digitalWrite(j, HIGH);

    }

  delay(delaytime);

  }

}

//Putting all LEDs one by one in a stack

void stack(int delaytime)

{

int stack = 0;

while(stack < 18)

  {

  for(int pos = 2; pos <= (19 - stack); pos++)

    {

    clearall();

    digitalWrite(pos, HIGH);

    drawstack(stack);

    delay(delaytime);

    }

  stack++;

  }

}

//Subfunction of the stack function

void drawstack(int stack)

{

  for(int n = 19; n > (19 - stack); n--)

    {

    if(n >= 2)

      {

      digitalWrite(n, HIGH);

      }

    }

}


//One LED chases another LED front and back

void chaser(int delaytime)

{

int div = 40;

int flashtime = delaytime / div;

int A = random(2,7);

int B = random(7,12);

int Av = 1;

int Bv = 1;

if(random(0,2))

{

Av *= -1;

}

if(random(0,2))

{

Bv *= -1;

}

for(int time = 1; time < 100; time++)

{

if(abs(A-B) == 1 && (Av*Bv) == -1)

{

for(int f = 1; f < round(div/4); f++)

{

clearall();

delay(flashtime);

digitalWrite(A, HIGH);

digitalWrite(B, HIGH);

delay(flashtime);

}

Av *= -1;

Bv *= -1;

A += Av;

B += Bv;

}

else

{

clearall();

digitalWrite(A, HIGH);

digitalWrite(B, HIGH);

A += Av;

B += Bv;

delay(delaytime);

}

if(A < 2)

{

A = 3;

Av *= -1;

}

if(B > 19)

{

B = 18;

Bv *= -1;

}

if(A >= B)

{

A = B-1;

}

}

}

 

After programming, remove the programmer and keep the power supply itself. It will work standalone. You can keep it on Shelves or Showcase in your Home.

Impress your friends and Enjoy the Light Show!

Share this post

Comments (25)

  • Jayne Jones

    Works great! I would like to put a PIR motion detector in this. Any help would be appreciated. Not having any luck coding it on my own.

    November 10, 2018 at 2:31 AM
  • Surmit Chauhan

    How can i change the blink time between each led

    December 29, 2018 at 7:52 PM
  • lukas

    when i try to upload to a arduino pro micro it says Error: stray ‘\240’ in program

    March 2, 2019 at 7:54 PM
    • Sharath

      It is because of the text copied from the copy option in the editor. It adds a tab kind of thing added in every line. So you have to copy the code directly without using the ‘copy option’. Or you have to delete it manually but all over the code. We will try to fix it soon.

      March 4, 2019 at 10:28 AM
      • lukas

        thx 😀 awesome led show btw

        March 6, 2019 at 11:19 PM
  • Jayantha Munasinghe

    stray ‘\240’ in program. how to fix

    March 8, 2019 at 4:18 PM
    • Sharath

      It is because of the text copied from the copy option in the editor. It adds a tab kind of thing added in every line. So you have to copy the code directly without using the ‘copy option’. Or you have to delete it manually but all over the code. We will try to fix it soon.

      March 9, 2019 at 11:33 AM
  • Steven Mooney

    Instead of LED’s will it open and close relays the same way. Of course I would have to remove the fade code.

    March 18, 2019 at 1:33 AM
    • Sharath

      Yes you can, But due to faster switching ur relay will fail quickly. Also the switching speed of electromagnetic relay is slower than LEDs, otherwise u need to use a Solid State Relay.

      March 18, 2019 at 11:33 AM
  • Attila Hrotko

    Fantastické mne to bez problémov funguje. Ide to aj na Mega. Gratulujem za skvelí projekt.

    May 6, 2019 at 12:12 AM
  • DA

    Much appreciated!

    June 30, 2019 at 4:35 AM
  • pravin

    how to add more effects in this program. plz. suggest. also suggest such a wonderfull effects for only 4 leds

    July 2, 2019 at 9:48 PM
  • pravin

    this is really very good project. but instead of 18 LEDS i want to use 4 leds and need program for multiple effects, so plz provide the codes

    July 2, 2019 at 9:50 PM
    • Sharath

      void fillall()
      void onrun()
      void fadealter() all are custom functions.

      Inside the loop we called the functions using a random function (an inbuilt arduino function) to pick numbers randomly between the range.

      int pickme = random(1,20); // picks a random pattern functions of LED patterns

      So to add a pattern. create your pattern in a custom function and put it inside the new case.

      fadealter(random(80,100)); //80 and 100 is just the delaytime that our custom function takes as input. You can create without that also by manually enter delay inside the function.

      July 3, 2019 at 10:27 AM
  • Mischa

    Hi,
    This may be a little much to ask so, please forgive me if it is.
    Is it possible to have 2 separate chaser LEDs(each with 4 LEDs), 4 Blinking LEDs and 2 static LEDs all run from a single Arduino Nano board, respectfully, without multiplexing?
    They would all run at the same time from the same program.

    July 14, 2019 at 7:21 AM
  • sevarin

    how do i make this work on an Arduino uno i am sure i have it connected correctly but its not lighting up and where do the positives go? this doesnt seem to be working. if you can help that would be great https://cdn.discordapp.com/attachments/488884857448038400/638909258062233600/DSC_0482.JPG

    October 30, 2019 at 6:49 AM
    • Sharath

      The positive of LEDs (Longer Leg) Goes to GPIO Pins.
      Most probably, you didn’t bridge the full rails in breadboard.
      The breadboard that you are using has 4 separate power rails (Marked as Red(+) and Blue(-). You can able to see the lines will break at half of the breadboard. It means the supply goes through until the marked lines only. If you need to extend it, use a small wire to bridge those half.

      October 30, 2019 at 10:22 AM
      • sevarin

        bridging it was not a problem I got that much to work but now the system dies after a few seconds of being on and then it crashes the ardiuno 🙁 so it ends up not working after that. Is there a way to make it consistant?

        October 31, 2019 at 7:35 AM
        • Sharath

          Then either there is a short circuit (or) the LEDs are drawing more current from the Arduino. Because Arduino GPIO Pins draw upto 300mA totally together(20mA on each GPIO Pin). So you can use an external power and invert the terminals to make the LED Glow on LOW Signals instead of HIGH.

          October 31, 2019 at 3:59 PM
  • Woz

    Does the fade control using “analogWrite” only work only on PWM pins or am I missing something ? 🙂

    January 2, 2020 at 11:20 AM
    • Sharath

      Technically You are correct. If we use analogWrite on a non-PWM Pin, the values less than a certain number takes as LOW and Greater will take as HIGH. But we can’t achieve it properly in Arduino UNO, Promini,Nano, we don’t have enough PWM pins in it. Only the PWM LEDs will Fade, rest will Blink. In Mega We have a series of PWM Pins from D2 to D12.

      January 2, 2020 at 1:20 PM
      • Woz

        Thank you for clarifying. I will look into using software PWM libraries that are available.

        January 5, 2020 at 1:34 PM
  • rhys1101

    hi I’m a beginner when it comes to arduino code, how difficult would it be to add a sound module to this code to activate the light patterns on sound
    thanks

    January 13, 2021 at 5:51 PM

Leave a Reply

Back to Blog