Lightning & thunder: Arduino Halloween DIY project
|
For this year’s Halloween, I wanted to do some fun DIY projects with stuff I already had laying around. This project is about simulating the lightning and thunder of a thunderstorm with an Arduino, LEDs and a DFPlayer Mini MP3 player module (see the documentation). It is easy to build and it worked out really well. So here it is, for anyone who also wants to surprise (or scare) his/her guests next year.
Note that this is not a “sound to light” system. The sketch (randomly) flashes the LEDs first and then plays the sound file after a short (random) delay. I think that’s more realistic than flashing the LEDs in reaction to the volume of the sound like other projects/systems do. Light travels much faster than sound. Therefore, during a real-life thunderstorm, you will usually observe the flashes first. The sound will be delayed, depending on how far away the lightning occurred.
Lightning simulator project: parts list
The parts used for the project:
- Arduino Nano
- DFPlayer Mini MP3 player module
- Micro SD card (8 GB)
- Breadboard and jumper wires
- MOSFET (1x IRFZ44N)
- Resistors (2x 1K)
- LED strip (5m, 12V, cool white)
- Test leads with alligator clips
- Power supply (12V, 2A)
Of course, you can use some other 12V LED lights. The MP3 files (see below) are only about 6MB so any SD card will do.


Wiring the Arduino lightning simulator
The wiring is pretty straightforward, see the images below:
- The 12V power supply is connected to the red breadboard power rail on the breadboard.
The LEDs get 12V from the power rail (red wire).
The Arduino Nano gets 12V power from the power rail to its Vin pin (red wire) and the Nano GND pin is connected to the common ground rail (blue wire). - The DFPlayer Mini gets 5V on its VCC pin from the Nano (red wire).
The RX pin from the DFPlayer goes to pin 10 on the Nano (black wire) via a 1K resistor.
The TX pin from the DFPlayer goes to pin 11 on the Nano (purple wire) via a 1K resistor.
The BUSY pin from the DFPlayer goes to pin 12 on the Nano (white wire).
The DAC-L (green wire) and DAC-R (yellow wire) go to the stereo audio amplifier.
The GND pin from the DFPlayer goes to the common ground rail (blue wire). - The MOSFET GATE pin (left) goes to the Nano pin 9 (orange wire).
The MOSFET DRAIN pin (center) goes to the LED’s ground wire (black wire).
The MOSFET SOURCE pin (right) goes to the common ground rail (black wire). - The amplifier’s ground is connected to the common ground rail.


Make sure that the power supply, Arduino, MOSFET, DFplayer and audio connection share the same common ground. Also, make sure the 12V is connected to the right pin (Vin) on the Nano.
Make sure you have the polarization of 12V and ground of the power supply and the LEDs right.
I used some wires with alligator clips to easily connect the LEDs and audio to the breadboard.


Lightning and thunder sound files
I have collected 17 different lightning and thunder sound samples in a .zip file. They can be downloaded using the link below. The files (0001.mp3 to 0017.mp3) should be put in a folder called ‘mp3’ on the SD card. The card may be formatted FAT16 or FAT32.
Programming the Arduino
You can upload the sketch to the Arduino the usual way, using the USB cable. The Nano gets its power from the 12V power supply, not from USB. So after uploading you can disconnect the USB cable.
The sketch uses the DFRobotDFPlayerMini library, check out the GitHub page and documentation for details.
#include "Arduino.h" #include "SoftwareSerial.h" #include "DFRobotDFPlayerMini.h" int ledPin = 9; // LEDs (via MOSFET) connected to pin 9 int rxPin = 10; // DFplayer RX to Arduino pin 10 int txPin = 11; // DFplayer TX toArduinopin 11 int busyPin = 12; // DFplayer BUSY connected to pin 12 SoftwareSerial mySoftwareSerial(rxPin, txPin); DFRobotDFPlayerMini myDFPlayer; void setup() { pinMode(ledPin, OUTPUT); pinMode(busyPin, INPUT); mySoftwareSerial.begin(9600); Serial.begin(115200); Serial.println(F("Initializing DFPlayer...")); if (!myDFPlayer.begin(mySoftwareSerial)) { //Use softwareSerial to communicate with mp3. Serial.println(F("Unable to begin. Check connection and SD card, or reset the Arduino.")); while (true); } Serial.println(F("DFPlayer Mini online.")); myDFPlayer.setTimeOut(500); // Set serial communictaion time out 500ms myDFPlayer.volume(30); // Set volume value (0~30). myDFPlayer.EQ(DFPLAYER_EQ_BASS); // Set EQ to BASS (normal/pop/rock/jazz/classic/bass) myDFPlayer.outputDevice(DFPLAYER_DEVICE_SD); // Set device we use SD as default myDFPlayer.enableDAC(); // Enable On-chip DAC } void loop() { int flashCount = random (3, 15); // Min. and max. number of flashes each loop int flashBrightnessMin = 10; // LED flash min. brightness (0-255) int flashBrightnessMax = 255; // LED flash max. brightness (0-255) int flashDurationMin = 1; // Min. duration of each seperate flash int flashDurationMax = 50; // Max. duration of each seperate flash int nextFlashDelayMin = 1; // Min, delay between each flash and the next int nextFlashDelayMax = 150; // Max, delay between each flash and the next int thunderDelay = random (500, 3000); // Min. and max. delay between flashing and playing sound int thunderFile = random (1, 17); // There are 17 soundfiles: 0001.mp3 ... 0017.mp3 int loopDelay = random (5000, 30000); // Min. and max. delay between each loop Serial.println(); Serial.print(F("Flashing, count: ")); Serial.println( flashCount ); for (int flash = 0 ; flash <= flashCount; flash += 1) { // Flashing LED strip in a loop, random count analogWrite(ledPin, random (flashBrightnessMin, flashBrightnessMax)); // Turn LED strip on, random brightness delay(random(flashDurationMin, flashDurationMax)); // Keep it tured on, random duration analogWrite(ledPin, 0); // Turn the LED strip off delay(random(nextFlashDelayMin, nextFlashDelayMax)); // Random delay before next flash } Serial.print(F("Pausing before playing thunder sound, milliseconds: ")); Serial.println(thunderDelay); delay(thunderDelay); Serial.print(F("Playing thunder sound, file number: ")); Serial.println(thunderFile); myDFPlayer.playMp3Folder(thunderFile); delay(1000); // Give the DFPlayer some time while (digitalRead(busyPin) == LOW) { // Wait for the DFPlayer to finish playing the MP3 file } Serial.print(F("Pausing before next loop, milliseconds: ")); Serial.println(loopDelay); delay(loopDelay); }
i think this is amazing , gonna try it out for my fish tank and see if i can get it going.
nice post!
Did it work for your tank? I am going to do this too for my fish tank.
please sir this is fantastic I want to make a thunder cloud all battery ran and self contained. if you could help I would be grateful !!! I have a strand of low voltage leds from a remote control flashing led unit, could these be hooked direct to the Arduino with same flashing and dimming effects or would I still need the mosfet do that. I would like to use 3 volt (two aa batt) if possible, also what changes if any would need to be done to the code thank you Sir !
For something running off 5 Volts without a mosfet, you maybe can get some inspiration from this project: IR REMOTE CONTROLLED COLOR CHANGING CLOUD (ARDUINO)
Good morning
but I could use WTV 020 – MP3 MODULO?
remain as an CODING?
From now and on thank you very much!
This is such an awesome idea! I used it as the foundation of an animation I just added to my HO scale model train layout. (https://youtu.be/P2Po3XqT-Y4) Thanks for including the sound files – they were invaluable.
Awesome project. As a newbie to arduino it is just what I am looking for.
Could you recommend speakers that will work with the DF player?
This is almost exactly what I am looking for for my cemetery scene. Is there a way to substitute a four channel relay block for the led outputs. I need to use 110v spotlights for the effect.
Thank you for sharing this. I am going to try this for my first Aurduino project. I am not sure what to do with the DFRobotDFPlayerMini. Is it a file that must be embedded into the sketch? Or maybe just on the SD card. I loaded your sketch into the IDE and I get an error message that states : “Arduino: 1.8.7 (Windows Store 1.8.15.0) (Windows 10), Board: “Arduino/Genuino Uno”
Lightning_Storm:3:33: error: DFRobotDFPlayerMini.h: No such file or directory
compilation terminated.
exit status 1
DFRobotDFPlayerMini.h: No such file or directory
This report would have more information with
“Show verbose output during compilation”
option enabled in File -> Preferences.
Where does this file go?
Did you work it out in the end David?
Took me a little while to work it out but got there in the end.
Thanks very much for posting this, it was just what I was looking for. Have now managed to put it all together and get it working but one problem, after about 5 mins or so all of a sudden the led lights will flash on and off quickly for a few seconds then everything stops, have to switch off and back on to get it all working again, this keeps happening, any ideas?
I replaced the MOSFET and all ok now, all ready for my Halloween party
I spoke to soon 🙁
After a while of testing it stopped again with the L light blinking on the Arduino, switching off and back on it works again for a while?
Think I solved it again by using an Arduino Mega that luckily I found, seems to be running smoothly for the last hour on test :-))
The onboard grounding on the DF Player is terrible. Typically manifests as sound problems, but could be the cause here. Check this out:
https://arduino.stackexchange.com/questions/49807/dfplayer-noise-researched-tried-and-bep-bep-bep-bep-bep
Thank you for this. Could you add a motion sensor to this project as well that would trigger the lightning/thunder while still using the same nano or would you need to use something larger like the Uno? Thanks!
how who u go about chaning the led to neo pixels instead of 12v led i cant deam to figure it out
I’m getting beeping noise when the LED’s light up. I can’t figure out why, can any one help me?
Hi, thanks for the project…. I need to ask wich amplifyer you recommend? Thanks
I’m a complete amateur when it comes to arduino, so forgive me if this is really simple. I get the following error when I try to validate the code:
This report would have more information with
“Show verbose output during compilation”
enabled in File > Preferences.
Arduino: 1.0.6 (Mac OS X), Board: “Arduino Nano w/ ATmega328”
In file included from sketch_may08b.ino:3:
DFRobotDFPlayerMini.h:72: error: ISO C++ forbids initialization of member ‘_timeOutDuration’
DFRobotDFPlayerMini.h:72: error: making ‘_timeOutDuration’ static
DFRobotDFPlayerMini.h:72: error: ISO C++ forbids in-class initialization of non-const static member ‘_timeOutDuration’
DFRobotDFPlayerMini.h:75: error: a brace-enclosed initializer is not allowed here before ‘{‘ token
DFRobotDFPlayerMini.h:75: error: ISO C++ forbids initialization of member ‘_sending’
DFRobotDFPlayerMini.h:75: error: making ‘_sending’ static
DFRobotDFPlayerMini.h:75: error: invalid in-class initialization of static data member of non-integral type ‘uint8_t [10]’
DFRobotDFPlayerMini.h:77: error: ISO C++ forbids initialization of member ‘_receivedIndex’
DFRobotDFPlayerMini.h:77: error: making ‘_receivedIndex’ static
DFRobotDFPlayerMini.h:77: error: ISO C++ forbids in-class initialization of non-const static member ‘_receivedIndex’
DFRobotDFPlayerMini.h:98: error: ISO C++ forbids initialization of member ‘device’
DFRobotDFPlayerMini.h:98: error: making ‘device’ static
DFRobotDFPlayerMini.h:98: error: ISO C++ forbids in-class initialization of non-const static member ‘device’
DFRobotDFPlayerMini.h:105: error: ISO C++ forbids initialization of member ‘_isAvailable’
DFRobotDFPlayerMini.h:105: error: making ‘_isAvailable’ static
DFRobotDFPlayerMini.h:105: error: ISO C++ forbids in-class initialization of non-const static member ‘_isAvailable’
DFRobotDFPlayerMini.h:106: error: ISO C++ forbids initialization of member ‘_isSending’
DFRobotDFPlayerMini.h:106: error: making ‘_isSending’ static
DFRobotDFPlayerMini.h:106: error: ISO C++ forbids in-class initialization of non-const static member ‘_isSending’
Any help is greatly appreciated.
Thanks for this. I have not seen any replies but worth asking all the same.. I am planning to make this and add to a pinball topper (TFTC). My question is, can this be set to trigger by either motion, vibration or sound? Brand new at Arduino stuff and will probably be able to answer this question myself in time, but any advice is appreciated.
I use IR sensors to activate the sound in many projects on my model railroad.
Hi Tom, that sounds interesting. Do you use IR sensors to detect humans or model trains?
Late to the party I know… I have a question for you… First off, thank you for this! Its amazing and is going to my my daughter’s lightening/umbrella a huge hit for halloween!
I’m trying to get this to run off a portable battery and I am finding that when the program runs from the start of plugging in it runs 1 time thru and then the battery turns off. It would appear that the voltage draw on the battery drops so low it thinks the device has been disconnected and turns off…
Can you think of any way to keep something going on the system in between the flashes/thunder that would help make it present a constant load to the battery so it doesn’t shut off? And/Or a battery that just stays lit and doesn’t turn off when the load drops?
Thank you so much!
I would like to do this in Visuino, can you make a tutorial or explain how to do it?
No, I can’t, because I don’t have a Visuino license.
I want to make a floating cloud with thunderstorm and lightening effects…..not electronically minded at all…..do you know where I can buy a miniature/small box to make this effect please ?
Hi,
Thanks for the guide.
I think there is a mistake with the following lines:
“The RX pin from the DFPlayer goes to pin 10 on the Nano (black wire) via a 1K resistor.
The TX pin from the DFPlayer goes to pin 11 on the Nano (purple wire) via a 1K resistor.”
RX of DFPlayer should be connected to pin 11 and TX to 10.
Just put this together. I tried using a 12V5A power supply for this, but when the lights flash there wasn’t enough power for the Arduino as well, causing it to restart. So get more power or run it separately off of USB.
Also, the random (1,17) will only return 16 possible options as the second parameter is exclusive. So you’d want random(1,18) instead.
Thank you so much, this project is awesome and was fun to do….worked to the t, as instructed!
Has just made everything work but has a sound noise beep beep beep beep between each audio file, can it be solved in some way
Hi from the UK :o) … Awesome project, would a BUZ11a MOSFET work instead ? .. As that’s all we have “in stock” :o)
Arduino: 1.8.13 (Windows Store 1.8.42.0) (Windows 10), Board: “Arduino Nano, ATmega328P”
readme.txt:3:10: fatal error: DFRobotDFPlayerMini.h: No such file or directory
#include “DFRobotDFPlayerMini.h”
^~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
exit status 1
Hi getting this error messege when i try and load to the Andriuino DFRobotDFPlayerMini.h: No such file or directory
Invalid library found in C:\Users\johnc\Documents\Arduino\libraries\readme.txt: no headers files (.h) found in C:\Users\johnc\Documents\Arduino\libraries\readme.txt
What would cause this? tried a number of options, but still getting same error