Twinkle LEDs

Like many of my posts, this isn’t the most practical project. However, the end result is a pretty cool twinkling row of LED lights. I got the idea for the project from this tutorial on Sparkfun ( While the tutorial is for the LilyPad, a sewable micro controller for fabrics, it works fine with an Arduino Uno.

Here’s what you’ll need:

  • 9 LEDs
  • Lots of jumper wires
  • Arduino Uno or similar board
  • Solderless breadboard

And here’s what you’ll do:

You can lay out your breadboard however you choose, but an easy way to do so is to line up the LEDs in a row across the breadboard. For the sake of keeping things organized and tidy, have all the the positive pins of the LEDs (the longer pin) pointing towards the left and the negative towards the right (the shorter pin). Each positive pin on the LEDs will be connected by a jumper wire to a digital input on the Arduino. I’m using digital pins 2 through 10. Each negative pin is connected to the negative rail of the breadboard with one wire on that rail connected to ground on the Arduino. Here a couple of photos to get you started:

It’s a semi-organized mess of wires. There certainly could be a better way of wiring it that would use fewer jumpers, but this gets the job done. Also, I’m completely ignoring the fact that a resistor should be used in each LED circuit. I’m not too concerned about damaging my Arduino. I only plan on running these twinkling lights for a few minutes at a time. If you want LEDs that you could leave twinkling for hours, you’ll have to redesign the circuit to use something like a 330K ohm resistor on the negative end of each LED. So, just be aware that I’m encouraging bad behavior.

On to the software. This I took almost line by line from the SparkFun tutorial. Upload the following code to your Arduino:

// Twinkle code for 9 LEDS
#define LED1 2
#define LED2 3
#define LED3 4
#define LED4 5
#define LED5 6
#define LED6 7
#define LED7 8
#define LED8 9
#define LED9 10
#define interval 11000
#define dead_time 1000
unsigned long PWM_counter = 0;
int offset = 0;
int step_size = 200;
unsigned long on_time = 0;
unsigned long cycle_start = 0;
char dir = 1;
int offset2 = 0;
int step_size2 = 200;
unsigned long on_time2 = 0;
unsigned long cycle_start2 = 0;
char dir2 = 1;
byte LED_tracker1 = 1;
byte LED_tracker2 = 1;
void setup() {
 pinMode(LED1, OUTPUT); 
 pinMode(LED2, OUTPUT); 
 pinMode(LED3, OUTPUT); 
 pinMode(LED4, OUTPUT); 
 pinMode(LED5, OUTPUT); 
 pinMode(LED6, OUTPUT); 
 pinMode(LED7, OUTPUT); 
 pinMode(LED8, OUTPUT); 
 pinMode(LED9, OUTPUT); 
 offset = random(2500,7500);
 offset2 = random(2500,7500);
void loop()
 PWM_counter = micros();

 if( (PWM_counter + offset - cycle_start) >= interval) { // completed cycle, start over
 on_time += step_size*dir;
 cycle_start = PWM_counter;
 if (on_time >= (interval - dead_time) ) {
 dir *= -1;
 on_time = interval - dead_time;
 else if (on_time <= 0) {
 on_time = 0;
 dir *= -1;
 LED_tracker1 = LED_tracker2;
 while (LED_tracker1 == LED_tracker2) LED_tracker1 = random(1,10);
 offset2 = random(0,4000);
 else if( (PWM_counter + offset - cycle_start) >= (dead_time + on_time) ) { // time to switch LED off
 //digitalWrite(LED1, LOW);
 else if( (PWM_counter + offset - cycle_start) >= dead_time) { // time to switch LED on
 //digitalWrite(LED1, HIGH);

 if( (PWM_counter + offset2 - cycle_start2) >= interval) { // completed cycle, start over
 on_time2 += step_size2*dir2;
 cycle_start2 = PWM_counter;
 if (on_time2 >= (interval - dead_time) ) {
 dir2 *= -1;
 on_time2 = interval - dead_time;
 else if (on_time2 <= 0) {
 on_time2 = 0;
 dir2 *= -1;
 LED_tracker2 = LED_tracker1;
 while (LED_tracker2 == LED_tracker1) LED_tracker2 = random(1,10);
 offset2 = random(0,4000);
 else if( (PWM_counter + offset2 - cycle_start2) >= (dead_time + on_time2) ) { // time to switch LED off
 //digitalWrite(LED1, LOW);
 else if( (PWM_counter + offset2 - cycle_start2) >= dead_time) { // time to switch LED on
 //digitalWrite(LED1, HIGH);
void LED_on(byte LED)
 if (LED == 1) digitalWrite(LED1, HIGH);
 if (LED == 2) digitalWrite(LED2, HIGH);
 if (LED == 3) digitalWrite(LED3, HIGH);
 if (LED == 4) digitalWrite(LED4, HIGH);
 if (LED == 5) digitalWrite(LED5, HIGH);
 if (LED == 6) digitalWrite(LED6, HIGH);
 if (LED == 7) digitalWrite(LED7, HIGH);
 if (LED == 8) digitalWrite(LED8, HIGH);
 if (LED == 9) digitalWrite(LED9, HIGH);
void LED_off(byte LED)
 if (LED == 1) digitalWrite(LED1, LOW);
 if (LED == 2) digitalWrite(LED2, LOW);
 if (LED == 3) digitalWrite(LED3, LOW);
 if (LED == 4) digitalWrite(LED4, LOW);
 if (LED == 5) digitalWrite(LED5, LOW);
 if (LED == 6) digitalWrite(LED6, LOW);
 if (LED == 7) digitalWrite(LED7, LOW);
 if (LED == 8) digitalWrite(LED8, LOW);
 if (LED == 9) digitalWrite(LED9, LOW);

That’s it. Afterward you should see all nine LEDs turning off and on randomly for a twinkle effect. Once again, thanks to Dia on SparkFun tutorials for the code and inspiration. Please let me know in the comments if you could think of a practical project that could incorporate these lights, or if you built it and simply enjoy staring into them. Actually, just tell me anything in the comments. I’d like to know that someone else is reading this and may actually find it useful.

Here’s a photo of the LEDs twinkling:

About these ads
Tagged ,

One thought on “Twinkle LEDs

  1. […] the previous post, I set up nine LEDs to randomly twinkle. While watching my LEDs twinkle, I noticed that one of them […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

Join 266 other followers

%d bloggers like this: