24,625

27

20

## Introduction: Random Arduino LED Fader.

Randomness. It is so easy in the real world. In the electronics world, well...it is really hard. Sure it is possible to create "pseudo-random" effects but not truly random. The only way to actually create randomness is to use programming. Even then, randomness will eventually repeat itself.

Fading. It is also hard.Without programming, it is possible but it is not efficient. The circuits aren't small. In fact, most fading circuits use large capacitors that take up space. Not pleasing.

Put these two together and it is extremely tough to make.

In the end, I must subject to programming.

To be specific in this case,

Arduino programming.

This project involves Pulse Width Modulation and is achieved through the use of an Arduino, a computer and a breadboard with LEDs. Inverting a signal will be achieved through the use of sinking and sourcing.

Update: I changed the code a bit. It now fades a bit faster and a bit better and smoother to view. If you downloaded the code look again at the code.

### Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

## Step 1: What You Need.

You will need:
An Arduino
6 LEDs (possibly more.)
Jumper Wires

## Step 2: Programming.

Program the Arduino with the code below. It will give you a slow fade from each LED.

```//feel free to make changes
//do not connect led's in sequential order
//make sure all led's are connected to a resistor if applicable
//this project was made and tested using only one wire and arduino's pin 13 resistor and LED
//note: pin 5 and 6 act weird in the beginning - an arduino bug
byte led1 = 3;
byte led2 = 5;
byte led3 = 6;
byte led4 = 9;
byte led5 = 10;
byte led6 = 11;
int x = 0;
int y = 0;
int steps = 1; //change if needed, defines the steps between 0 and 255, a lower number is smoother
//make sure the variable "steps" is a factor of 255; any of the below numbers
//factors of 255 are : 1,3,5,15,17,51,85,255
//sorry for a lot of notes, but remember to change variable "delaytime" according to variable "steps"

//delay is in milliseconds for below
int delaytime = 10; //change if needed, delay between increments of PWM
//850 milliseconds is on-off/off-on time, in 17 step increments of brightness
int delaytime2 = 1000; //change if needed, delay between switching of leds
void setup (){
pinMode (led1, OUTPUT);
pinMode (led2, OUTPUT);
pinMode (led3, OUTPUT);
pinMode (led4, OUTPUT);
pinMode (led5, OUTPUT);
pinMode (led6, OUTPUT);
do {
x = x + steps;
analogWrite (led1, x);
analogWrite (led2, x);
delay (delaytime);
}
while (x != 255);
}
void loop (){
y = 0;
x = 255;
delay (delaytime);
do{
y = y + steps;
x = x - steps;
analogWrite (led3, y);
analogWrite (led1, x);
delay (delaytime);
}
while (y != 255 && x != 0);
delay (delaytime2);
y = 0;
x = 255;
do{
y = y + steps;
x = x - steps;
analogWrite (led4, y);
analogWrite (led2, x);
delay (delaytime);
}
while (y != 255 && x != 0);
delay (delaytime2);
y = 0;
x = 255;
do{
y = y + steps;
x = x - steps;
analogWrite (led5, y);
analogWrite (led3, x);
delay (delaytime);
}
while (y != 255 && x != 0);
delay (delaytime2);
y = 0;
x = 255;
do{
y = y + steps;
x = x - steps;
analogWrite (led6, y);
analogWrite (led4, x);
delay (delaytime);
}
while (y != 255 && x != 0);
delay (delaytime2);
y = 0;
x = 255;
do{
y = y + steps;
x = x - steps;
analogWrite (led1, y);
analogWrite (led5, x);
delay (delaytime);
}
while (y != 255 && x != 0);
delay (delaytime2);
y = 0;
x = 255;
do{
y = y + steps;
x = x - steps;
analogWrite (led2, y);
analogWrite (led6, x);
delay (delaytime);
}
while (y != 255 && x != 0);
delay (delaytime2);
}
```

## Step 3: Circuitry.

Take the ground of each LED and connect it to the ground rail of the breadboard. Connect the positive leads to pin 3, 5, 6, 9, 10, 11 on the Arduino.

Now, the LEDs should fade in a pseudo-random way. You are done but, for less impressed people, click "Next Step."

## Step 5: Sinking and Sourcing.

Here's how I teach sinking and sourcing.

By sinking, I mean from output pin to ground. By sourcing, I mean from positive to output pin.

Think of it this way. Power can flow from the LED to the pin in sourcing. In sinking, power comes from the pin. Power is always coming into the pin so, when the pin goes high, the current "passes" through the LED to the pin thus, turning it off.

To most people, they should say, "What difference does it make?"

To moi, a random person, it means, "I can invert signals thus create more randomness!"

Remember, to avoid using a resistor (living dangerously) power it from 3.3 volts.

## Step 6: Doing So...

Now take pin 3 and source it.

Take pin 6 and source it.

Take pin 9 and source it.

Then, take pin 11 and source it.

Leave the rest where they are.

## Step 7: Really Done.

Now, a random effect can be achieved (time for me to evily cackle) (excusez-moi...BWA-HA-HAHA-HA!)

Done.

Participated in the

Participated in the
Arduino Challenge

Participated in the
ShopBot Challenge

Participated in the
Make It Glow Challenge

Participated in the
The Teacher Contest

42 3.9K
28 3.2K
164 9.9K

## 20 Discussions

I want to add more number of leds to tihs code how do i do that?

Thank you! Almost exactly what I was looking for. I played with the code because the original has an obvious pattern. so I added a few lines, and some integers to make the LED's dim faster or slower It still his a pattern, but it is harder to notice.

byte led1 = 3;

byte led2 = 5;

byte led3 = 6;

byte led4 = 9;

byte led5 = 10;

byte led6 = 11;

int x = 0;

int y = 0;

int steps1 = 1;

int steps2 = 5;//change if needed, defines the steps between 0 and 255, a lower number is smoother

//make sure the variable "steps" is a factor of 255; any of the below numbers

//factors of 255 are : 1,3,5,15,17,51,85,255

//sorry for a lot of notes, but remember to change variable "delaytime" according to variable "steps"

//delay is in milliseconds for below

int delaytime = 10; //change if needed, delay between increments of PWM

//850 milliseconds is on-off/off-on time, in 17 step increments of brightness

int delaytime2 = 1000; //change if needed, delay between switching of leds

int delaytime3 = 500;

void setup (){

pinMode (led1, OUTPUT);

pinMode (led2, OUTPUT);

pinMode (led3, OUTPUT);

pinMode (led4, OUTPUT);

pinMode (led5, OUTPUT);

pinMode (led6, OUTPUT);

do {

x = x + steps1;

analogWrite (led1, x);

analogWrite (led2, x);

delay (delaytime);

}

while (x != 255);

}

void loop (){

y = 0;

x = 255;

delay (delaytime);

do{

y = y + steps1;

x = x - steps2;

analogWrite (led3, y);

analogWrite (led1, x);

delay (delaytime);

}

while (y != 255 && x != 0);

delay (delaytime2);

y = 0;

x = 255;

do{

y = y + steps2;

x = x - steps1;

analogWrite (led1, y);

analogWrite (led5, x);

delay (delaytime);

}

while (y != 255 && x != 0);

delay (delaytime3);

y = 0;

x = 255;

do{

y = y + steps1;

x = x - steps2;

analogWrite (led6, y);

analogWrite (led4, x);

delay (delaytime);

}

while (y != 255 && x != 0);

delay (delaytime2);

y = 0;

x = 255;

do{

y = y + steps2;

x = x - steps1;

analogWrite (led5, y);

analogWrite (led1, x);

delay (delaytime);

}

while (y != 255 && x != 0);

delay (delaytime2);

y = 0;

x = 255;

do{

y = y + steps1;

x = x - steps2;

analogWrite (led4, y);

analogWrite (led6, x);

delay (delaytime);

}

while (y != 255 && x != 0);

delay (delaytime3);

y = 0;

x = 255;

do{

y = y + steps1;

x = x - steps1;

analogWrite (led4, y);

analogWrite (led2, x);

delay (delaytime);

}

while (y != 255 && x != 0);

delay (delaytime3);

y = 0;

x = 255;

do{

y = y + steps2;

x = x - steps1;

analogWrite (led5, y);

analogWrite (led3, x);

delay (delaytime);

}

while (y != 255 && x != 0);

delay (delaytime2);

y = 0;

x = 255;

do{

y = y + steps2;

x = x - steps2;

analogWrite (led6, y);

analogWrite (led4, x);

delay (delaytime);

}

while (y != 255 && x != 0);

delay (delaytime2);

y = 0;

x = 255;

do{

y = y + steps1;

x = x - steps1;

analogWrite (led1, y);

analogWrite (led5, x);

delay (delaytime);

}

while (y != 255 && x != 0);

delay (delaytime3);

y = 0;

x = 255;

do{

y = y + steps1;

x = x - steps2;

analogWrite (led2, y);

analogWrite (led6, x);

delay (delaytime);

}

while (y != 255 && x != 0);

delay (delaytime3);

y = 0;

x = 255;

delay (delaytime);

do{

y = y + steps2;

x = x - steps1;

analogWrite (led3, y);

analogWrite (led1, x);

delay (delaytime);

}

while (y != 255 && x != 0);

delay (delaytime3);

y = 0;

x = 255;

do{

y = y + steps1;

x = x - steps2;

analogWrite (led4, y);

analogWrite (led2, x);

delay (delaytime);

}

while (y != 255 && x != 0);

delay (delaytime2);

y = 0;

x = 255;

do{

y = y + steps1;

x = x - steps2;

analogWrite (led5, y);

analogWrite (led3, x);

delay (delaytime);

}

while (y != 255 && x != 0);

delay (delaytime3);

y = 0;

x = 255;

do{

y = y + steps1;

x = x - steps1;

analogWrite (led6, y);

analogWrite (led4, x);

delay (delaytime);

}

while (y != 255 && x != 0);

delay (delaytime2);

y = 0;

x = 255;

do{

y = y + steps2;

x = x - steps2;

analogWrite (led1, y);

analogWrite (led5, x);

delay (delaytime);

}

while (y != 255 && x != 0);

delay (delaytime2);

y = 0;

x = 255;

do{

y = y + steps2;

x = x - steps1;

analogWrite (led2, y);

analogWrite (led6, x);

delay (delaytime);

}

while (y != 255 && x != 0);

delay (delaytime2);

y = 0;

x = 255;

delay (delaytime);

do{

y = y + steps1;

x = x - steps1;

analogWrite (led1, y);

analogWrite (led2, x);

delay (delaytime);

}

while (y != 255 && x != 0);

delay (delaytime2);

y = 0;

x = 255;

do{

y = y + steps1;

x = x - steps1;

analogWrite (led3, y);

analogWrite (led1, x);

delay (delaytime);

}

while (y != 255 && x != 0);

delay (delaytime2);

y = 0;

x = 255;

do{

y = y + steps1;

x = x - steps2;

analogWrite (led4, y);

analogWrite (led2, x);

delay (delaytime);

}

while (y != 255 && x != 0);

delay (delaytime3);

y = 0;

x = 255;

do{

y = y + steps1;

x = x - steps2;

analogWrite (led5, y);

analogWrite (led3, x);

delay (delaytime);

}

while (y != 255 && x != 0);

delay (delaytime3);

y = 0;

x = 255;

do{

y = y + steps2;

x = x - steps1;

analogWrite (led6, y);

analogWrite (led4, x);

delay (delaytime);

}

while (y != 255 && x != 0);

delay (delaytime2);

y = 0;

x = 255;

do{

y = y + steps1;

x = x - steps2;

analogWrite (led1, y);

analogWrite (led5, x);

delay (delaytime);

}

while (y != 255 && x != 0);

delay (delaytime2);

y = 0;

x = 255;

do{

y = y + steps1;

x = x - steps1;

analogWrite (led1, y);

analogWrite (led2, x);

delay (delaytime);

}

while (y != 255 && x != 0);

delay (delaytime2);

y = 0;

x = 255;

do{

y = y + steps1;

x = x - steps1;

analogWrite (led3, y);

analogWrite (led1, x);

delay (delaytime);

}

while (y != 255 && x != 0);

delay (delaytime2);

y = 0;

x = 255;

do{

y = y + steps2;

x = x - steps2;

analogWrite (led2, y);

analogWrite (led6, x);

delay (delaytime);

}

while (y != 255 && x != 0);

delay (delaytime2);

}

how to make the fade faster

You should use curent limiting resistors on your LEDs, baud rate didnt killed it!

Yeah. I did sink and source, and I used your code. It starts out seeming random, but after a while a pattern emerges.

Try rewiring the sink and source. Plus, don't stare at it too long otherwise the pattern will emerge.

I will now rework the code.

You've heard of the random function on the arduino right? have you tried using it in the code? I'm trying to use
int x = random(1, 7)
to get a random number, and list all of the combinations the lights could be, and use the number gotten from the result of the random function to randomly select one of the combinations (and yes I know that that would only be capable of choosing only 6 possible combination of something like 30 to 36.)

that's something I just learned today. I'm trying it now, so if I get it to work I'll post it here.

My Arduino has died!!! DO NOT TRY THE RANDOM CODE AT THE WRONG (TOO FAST) BAUD RATE!!! IT WILL KILL IT!

that's weird, what was the baud rate that you used?
are you going to order another atmega, or get a new arduino?

I have new ATMEGA328s. They just don't have the bootloader.

The EEPROM seems busted...the TX LED stays on all the time.

I will have to run the Command Prompt to see where it doesn't work.

Okay well I hope you get it working. I will continue experimenting with the code. I might try using radomSeed, and use an analog pin with nothing attached.

Yay I´m the first to write a cament.
Very cool I did it and it works!!!!
Thanks!