Thunder & Lightning Animation




Children's Hospital of Pittsburgh has a wonderful O-scale train layout that represents scenes from the Harry Potter movies. There is a large castle at one end of the layout and I wanted to add an animation to the castle that would flash bright LEDs in concert with an audio track of claps of thunder.

I have a good deal of experience with micro-controller controlled MP3 players including the VMUSIC2 and the DFPlayer. I opted to use the DFPlayer for this project as it works very well with an Arduino and is quite inexpensive. More importantly it is easy to control and can power a speaker without an additional amplifier.

This project can be built on a piece of prototype board with point to point wiring. Add a bright LED bulb or three and a speaker to the Arduino and DFPlayer and you are ready to go!

Step 1: The Circuit

In addition to the DFPlayer and an Arduino I needed a Mosfet (an IRL520) to drive a group of LED lights that operate from 12 volts and can draw a good bit of current. I have tested the unit with as many as 4 six LED spotlight bulbs wired in parallel.

The DFPlayer's RX pin is connected to the Arduino's TX pin. Be sure to include the 1000 ohm current limiting resistor. Pin 16 on the DFPlayer (busy) goes to pin 10 on the Arduino - that allows the Arduino to know if the MP3 file is playing or not.

The bright LED spotlight bulbs are connected to the Arduino through a Mosfet, Q1, which turns the lights on and off. This behavior is made possible by the connection between speaker pin 8 on the DFPlayer and analog pin 0 (zero) on the Arduino. The sound is sampled continuously and, when a loud sound is detected, the bulbs light brightly. When the sound subsides the LEDs go off.

Step 2: Parts

This group of parts is placed near their location on the schematic. While the Arduino and DFPlayer could be soldered directly to the circuit board I like to put such devices in sockets. The black socket pins are cut from a longer strip of contacts.


  • Arduino Pro Mini - the Arduino Uno will work, too.
  • DF-Player (must be labeled DF Player - there are many similar units that will not work)
  • micro SD card - 1 gig is plenty
  • 7805 voltage regulator
  • heat sink for regulator
  • IRL520 Mosfet
  • 10 uf / 25 volt tantalum capacitor (anything from 1 to 10 uf will work)
  • 470 uf / 25 volt electrolytic capacitor (anything from 220 - 1000 uf will work)
  • 8 ohm speaker (junk box or old computer speakers -If you use a 4 ohm speaker R5 is needed - see schematic)
  • 1 or more LED spotlight bulbs - the ones I used have six LEDs in each bulb and are VERY bright!
  • sockets for the LED bulbs
  • pushbutton "start" switch (any momentary SPST switch will work)
  • female 2.54mm header (to cut up for the Arduino and DFPlayer sockets)
  • miscellaneous connectors, wire, etc
  • 10 K resistor (1)
  • 1 K resistor (3)
  • 12 volt 2 amp power supply
  • Circuit board for prototype construction (approx 2" x3")

Visit my web page ( ) for links to the places I ordered parts.

Step 3: Construction

The unit was built on a small piece of prototype board. The solder pad side of the circuit board faces down and the components are inserted from the other side.Step 1 - solder the Arduino and DFPlayer to the board using female header sections.

Be careful that there are no solder bridges connecting adjoining pins.

Step 4: Power Supply

The 7805 voltage regulator and the two capacitors are added next - the tantalum capacitor cannot be seen in this photo - it is on the other side of the 7805.

Step 5: Power Supply 2

Next add the wires that supply +5 volts (the red wires) and ground (black wires) - the two wires that are not connected will be used for the "start" switch.

Step 6: Add the Start Switch

To accommodate the switch that I used two 1/8" holes were drilled in the board. The 10 K resistor holds the Arduino pin that connects to the switch low until it is pressed.

Step 7: Add the Light Control Mosfet

There is a two pin female header next to it that the LEDs plug into - if you prefer they can be wired directly.

The two heavier red and black wires deliver power directly to the Mosfet and the plug for the LED bulbs. Using thinner wire would limit the amount of current available to the LEDs.

Step 8: Final Connections

Don't forget the jumper between Arduino pin 10 & DFPlayer pin 16 and the wire (blue in the photos) that connects speaker pin 8 on the DFPlayer and pin A0 on the Arduino. The 5 ohm resistor between the speaker and pin 6 on the DFPlayer is only needed if you use a 4 ohm speaker - if you don't use the resistor with a 4 ohm speaker the voltage regulator will get quite hot. An 8 ohm speaker works without the resistor.

The wires on the top of the board were placed that way to more clearly illustrate the circuit wiring. If you prefer you could put those wires on the bottom of the board.

Step 9: Sound Files

You can download the thunder sound files by clicking here. On your Micro SD card place them in a folder called mp3 The sound files themselves are numbered 0001.mp3, 0002.mp3 and so on. I only use 0001.mp3 in the video.

Step 10: Software

The Arduino code is shown below . There are two variables ( if (sensorValue >= 750) { and if (sensorValue <= 666) { ) that can be adjusted based on your sound files - 750 is the analog reading that triggers the LED strip light on and 666 is the level where the LEDs are turned off. Since this happens at a very fast rate it does a remarkable job of simulating lightning flashes that are synchronized with the claps of thunder. Watch the video to see how the program works.

The only library ( #include ) that is used can be downloaded here:

d. bodnar - 9-06-2016

Lightning & thunder for CHP castle



const int buttonPin = 3; // the number of the pushbutton pin

int buttonState = 0; // variable for reading the pushbutton status

int buusyPin = 10;// buusyPin = 10; // sound player busy

int bsy = 0;

int LEDstripLight = 12; // IRL520 to LED strip light

int sensorPin = A0; // Audio level samples

int sensorValue = 0; // variable to store the value coming from the sensor

int buttn = 0;


void setup () {

pinMode(buttonPin, INPUT);

pinMode(LEDstripLight, OUTPUT);

pinMode(buusyPin, INPUT);

pinMode(buttonPin, INPUT);

Serial.begin (9600);

mp3_set_serial (Serial); //set Serial for DFPlayer-mini mp3 module

mp3_set_volume (30); // must remove mp3_reset(); to get this to work



void loop () {


Serial.println("Waiting for Button Push");

do {

buttn = digitalRead(buttonPin); // pins closest to power pins

} while (buttn == 0);

Serial.println("Button Hit");



do {

sensorValue = analogRead(sensorPin);


Serial.print(" ");

if (sensorValue >= 750) {

Serial.println("large number! ");

digitalWrite(LEDstripLight, HIGH);


if (sensorValue <= 666) {

digitalWrite(LEDstripLight, LOW);


bsy = digitalRead(buusyPin);

} while (bsy == 0); // zero when sound active


//...................................... END LOOP ........................................

Step 11: Conclusion

The videos show quite clearly how well the sound and light flashes are synchronized. This same circuit could easily be adapted for a mine explosion (see the third video for an example of this), a fireworks display and many other lighting effects.
Please let me know how you use it!



    • Beauty Tips Contest

      Beauty Tips Contest
    • Pets Challenge

      Pets Challenge
    • Frozen Treats Challenge

      Frozen Treats Challenge

    21 Discussions


    Question 12 days ago

    I would like to use the full code of the page, but with
    a slight adjustment.

    For a cauldron on coal (have a fixed lighting).
    Am an amateur of 75 years young, and do my best
    to understand English. 'Ve only 1 ½ years in electronics.

    Now comes my problem, programming.
    With good examples and setups I start to understand
    it, just to keep things to make it work.

    Instead of push buttons, I would like to work
    with HU-SRF05 ultrasonic sensor (HC-SR04). Both the DFPlayer and the sensor are
    totally new to me, I do not know how to make the combination.

    The idea:
    2 to 15 cm violent storm file 0001
    15 to 40 cm violent bubbles file 0002
    41 to 80 cm bubbles file 0003 //max distance I want to use
    81 cm en Out of range bubbles file 0004 //more than 81 and Out of range

    The file 0001 can only replay after 5 to 10 seconds
    The files 0002 to 0004 should continuous to we get another distance.

    This is at the pin numbers
    int trig = 8; int
    echo = 9;

    In the void loop () I do not know how to solve it, go somewhere with "if and else if"
    I think, but can't find any adequate example.

    2nd question:
    The files 0002 to 0004 I would also save as but
    with a blank stereo left channel. No lights come here, this are bubbles from
    the couldron, is this correct?

    My idea is good, but I can't get it to a good end.
    If you want to help me I would be very grateful.
    If you prefer to reply by mail:

    1 answer

    Answer 12 days ago

    Good day - I believe that you could interface the ultrasonic sensor as you describe. There are sure to be samples on the Internet that can help.
    I will leave the actual coding and hardware changes to you as I do not have time to devote to your ideas right now.
    Good luck and have fun!



    Question 15 days ago

    Am a bit confused.
    At the Mosfet we have the G D and S. The G to the Arduino, the S to the negative of LEDs and the D to GND, or am I wrong?

    2 answers

    Answer 15 days ago

    I believe you have it right - the source goes to ground, the drain to the LED and the gate to the Arduino


    Question 2 months ago on Step 11

    I'm just learning Arduino, made a few sketches and would like to try your sketch, but, I get a error message on the line that reads "mp3_set_serial(Serial)" error message reads "not declared in this scope"
    Tried a few things without success. Can you be of assistance

    1 answer

    Reply 2 months ago

    Joe - the only thing that I can think of is that you have not installed the library -

    The only library ( #include ) that is used can be downloaded here:

    If you have not give that a try.

    If you are not familar with adding libraries just Google arduino library install for help

    Good luck!



    Question 1 year ago on Step 10

    I'm new to this so can you please tell me how to download the library module. If I click on your link it takes me to a page showing the code but I can't see a "download" feature anywhere.


    1 year ago

    amazing it works great, under ARDUINO topic named THUNDER


    2 years ago

    #include <DFPlayer_Mini_Mp3.h>

    #include "SoftwareSerial.h"

    these compiled


    2 years ago

    #include DFPlayer_Mini_Mp3 does not compile. getting an error message .

    error: #include expects "FILENAME" or <FILENAME>


    2 years ago

    Nice work. Being Arduino controlled, I would change to lightning part to flash only at the beginning of the sound effect, the sound the thunder like a real storm does. All in all great work to help out the hospital.


    2 years ago

    Truly amazing work, not only on the thunder and lightning, but the models as well! Thank you so much for sharing!

    1 reply

    Reply 2 years ago

    Thanks for the comment! Please note that I did not build this layout - that was done by a professional organization and took many moons to complete!



    2 years ago

    Freaking awesome!!!!


    2 years ago

    Nice system and great idea, I think that you mean that the arduino controls the mp3 player and tells it when to play music. Is that right? The speaker output linked to the analog input is always positive?

    1 reply

    Reply 2 years ago

    The speaker output varies depending on the volume of the of the MP3 player output - that is how the Arduino knows when it is time to turn on or turn off the LED light.



    2 years ago

    Wow very nice instructable.
    Keep up the good work!