Arduino can do so many things; one of the most basic tutorials that many follow – myself included – when first getting into Arduino is to make an LED flash. Today, we’ll be doing the same thing, but in a much cooler way. We’ll be replicating lightning strikes with a few white LED’s and a diffusing cube, all hooked up to an Arduino generating random flashes of light for differing intervals. Join us over the break to follow the first part of this tutorial.
Step 1. The plan
The most important part to any Arduino project, big or small, is to have a solid plan that you can work off. After you’ve established exactly what you want to achieve, break it down in to the smallest parts possible. So what are we going to achieve?
Let’s look at a lightning strike, what does it consist of? Well for starters,
- The initial flash of lightning. This isn’t always the same brightness, but it is reasonably bright, so we will introduce some randomness in that.
- The intial flash stays for a certain period of time, which we will randomly select.
- Some lightning strikes ‘fade’ out while others disappear near instantly.
- The time between strikes is also very random, with some strikes occurring within seconds of each other.
- And of course, this repeats itself for the life of the lightning storm.
Step 2. Setting up
I’ll be using a breadboard, you could use some veroboard or directly solder it – however you do it, set it up similarly the following diagram. Note that we for Part 1 of this tutorial, we’ll be using only a single LED. This allows us to avoid a decent chunk of programming with for loops, and also running the LED’s in parallel because each digital out pin on the Arduino can only supply 40mA, which is easy to exceed running several LED’s very brightly. Later on, we’ll look at setting up an array of LED’s to allow us to do some really cool things with varying the brightness between strikes.
The value of your resistor required for your resistor will vary depending on which LED you’re using; a good calculator can be found here. An optional piece of kit that stops you from getting blinded by the LED is a simple cube cut from some copy paper, you can find some nets for this here (courtesy of Gijs Korthals Altes), but remember to either cut a hole for your LED or leave off the bottom! Now that we’re done with the hardware side of things, lets move right along to first part of coding; blinking!
Step 3. Initial Coding
Load up the Arduino IDE – if you don’t have it, get on it! Grab it here off the arduino.cc website, and if this is your first time, don’t forget to install the drivers (as the author of this post may or may not have done..).
I’ll be assuming that you’re entirely new to the Arduino platform; if not, you should be okay just skimming most of this tutorial to get the gist of it.
First step, we need the bare minimum functions that all Arduino sketches require.
void setup() {
//setup code goes here
}
void loop() {
//repeating code goes here
}
So what do we know? If you’ve followed the diagram above exactly, your LED is connected on pin 11; therefore we can define as a global variable (i.e. before the setup function runs),
int led = 11; //set the variable 'led' to hold the pin of the LED
Well, that pretty much covers it. I’ll see you in the next tutorial.
Only joking! We need to tell the Arduino that the pin an output, so in the setup loop, type in
pinMode(led, OUTPUT); //sets pin number 'led' as output
We need one more piece in our setup loop before we continue. Because of the ‘fake’ or pseudo-random nature of the way that Arduino generates random numbers, we need to give it a truly random number to base itself off. We can use the randomSeed() function to do this. One easy way to produce a truly random number to be used in that function is to read an unused analog pin, because they’re always floating around the place. If we didn’t do this, then we’d always get an identical series of flashes! (Thanks to Pigboy501 for pointing this out!) Below your pinMode code, add in,
randomSeed(analogRead(0)); //creates random seed
Again, this is only short because this is a pretty basic tutorial! We’re done with the setup, so scroll on down to the meat of the code, the loop function.
In here, we need to do several things that we identified earlier, in this order.
- Light up the LED to a random level of brightness.
- Wait a random amount of time with LED at this brightness.
- Fade the LED out over a short, but random amount of time.
- Wait a random amount of time with LED off until the next strike.
void strike(int x) {
analogWrite(led, x);
}
int brightness = random(200,255); //generate random intensity
Now remember our strike function took a brightness value in its arguments? Well that’s exactly what we need to do! Add below the above code, in the main loop:
strike(brightness); //flash at random brightness
Now if you run this code on your Arduino, you’d probably just get an LED flickering slightly, and that OK, we not done quite yet! Our next task is to wait a certain amount of time while the LED is at that brightness. We’ll use another variable that holds a delay (we’ll set up three here to save time later, don’t panic!).
int delayPulse = random(1,65); //how long to keep LED bright int delayBetween = random(25,6500); //duration between strikes int fadeLength = random(1,12); //duration of fade out
Add these in where you added in the brightness (for simplicity sake, keep all your variables for the moment up the top of the loop function). We’re getting somewhere! Now we need to make a way in which to fade the LED out after a strike. Again, we’ll use a separate function, although you don’t have to. This function is going to use a for loop, which essentially says for every time this variable is within a certain range, to this to it. For more documentation on fading, click here, and for more on the for loops, click here. Type in this code at the bottom, below where you had your strike function (make sure you don’t put it inside the strike function!)
void fade(int y) {
for(int fadeValue = 225 ; fadeValue >= 0; fadeValue -=5) {
analogWrite(led, fadeValue);
delay(y);
}
}
Almost there, don’t worry
Now we can assemble it all! At the moment, all we have is the strike function set up, but we still need those delays. It’s simply a matter of setting up two delay() function in the main loop using those delayPulse and delayBetween variables, with the fade function in between.
//this goes directly below strike(brightness); delay(delayPulse); fade(fadeLength); delay(delayBetween);
And we’re done! You should now be able to upload your code to your Arduino and stand in awe of the amazing effects that rival Tesla’s! The entire code is available at end of the post if you’re having trouble.
In Part 2 of this tutorial, we’ll be advancing upon what we’ve established here. We’ll be using Processing (don’t worry, it’ll be very familiar if you know about Arduino, it’s based upon it!) to make a lightning strike sound syncronised with the flashing of the light! Stay tuned and thanks for reading. If you need help, don’t hesitate to ask in the comments.
Reference Code
int led = 10; //set the variable 'led' to hold the pin of the LED
void setup() {
//setup code goes here
pinMode(led, OUTPUT); //sets pin number 'led' as output
randomSeed(analogRead(0));
}
void loop() {
int brightness = random(200,255); //generate random intensity
int delayPulse = random(1,65); //how long to keep LED bright
int delayBetween = random(25,6500); //duration between strikes
int fadeLength = random(1,12); //duration of fade out
strike(brightness); //flash at random brightness
delay(delayPulse);
fade(fadeLength, brightness);
delay(delayBetween);
//repeating code goes here
}
void strike(int x) {
analogWrite(led, x);
}
void fade(int y, int z) {
for(int fadeValue = z; fadeValue >= 0; fadeValue -=5) {
analogWrite(led, fadeValue);
delay(y);
}
}


Man, this is so lame. Only a single LED. How can you simulate an electrical storm with only a single LED? You can’t.
‘Part 1′
Not much more to lightning than a light. There’s much more to come.