Introduction: Thunder & Lightning Animation
Introduction
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.
Parts:
- 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 ( http://www.trainelectronics.com/Animation-thunder-lightning/ ) 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: https://github.com/DFRobot/DFPlayer-Mini-mp3/blob...
/*
d. bodnar - 9-06-2016
Lightning & thunder for CHP castle
*/
#include
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;
//***************************************SETUP*************************************
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
}
//.......................................LOOP................................................
void loop () {
Serial.println("");
Serial.println("Waiting for Button Push");
do {
buttn = digitalRead(buttonPin); // pins closest to power pins
} while (buttn == 0);
Serial.println("Button Hit");
mp3_play(1);
delay(100);
do {
sensorValue = analogRead(sensorPin);
Serial.print(sensorValue);
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!
47 Comments
Question 4 years 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
Joe
Reply 4 years 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: https://github.com/DFRobot/DFPlayer-Mini-mp3/blob...
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!
dave
Reply 24 days ago
DaveBodnar,
I also am attempting to use your code and getting the "mp3_set_serial", "mp3_set_volume", and "mp3_play" not declared errors.
Any advise? Ok, found I had incorrect DFPlayer library. That fixed the above.
Then the details. Went through all the code to find minor details that were not consistent with your code. That fixed a few more errors. Yet there are still
undefined reference to `mp3_set_serial(HardwareSerial&)'
undefined reference to `mp3_set_volume(unsigned int)'
undefined reference to `mp3_play(unsigned int)'
that are persisting... any suggestions?
Thank you,
Bill
Reply 24 days ago
Here is the error message:
C:\Users\billh\AppData\Local\Temp\ccvzGT1w.ltrans0.ltrans.o: In function `setup':
C:\Users\billh\OneDrive\Documents\Arduino\button-thunderandlightning_sketch_sep2a/button-thunderandlightning_sketch_sep2a.ino:40: undefined reference to `mp3_set_serial(HardwareSerial&)'
C:\Users\billh\OneDrive\Documents\Arduino\button-thunderandlightning_sketch_sep2a/button-thunderandlightning_sketch_sep2a.ino:42: undefined reference to `mp3_set_volume(unsigned int)'
C:\Users\billh\AppData\Local\Temp\ccvzGT1w.ltrans0.ltrans.o: In function `loop':
C:\Users\billh\OneDrive\Documents\Arduino\button-thunderandlightning_sketch_sep2a/button-thunderandlightning_sketch_sep2a.ino:62: undefined reference to `mp3_play(unsigned int)'
collect2.exe: error: ld returned 1 exit status
exit status 1
Compilation error: exit status 1
6 years ago
#include DFPlayer_Mini_Mp3 does not compile. getting an error message .
error: #include expects "FILENAME" or <FILENAME>
Reply 24 days ago
arduinomaster,
Had that error and fixed it with " <DFRobotDFPlayerMini.h> "
5 months ago
Hi, I may be blind or dumb, but I do not see the third 1000 resistor on the schematic. Parts list shows 3 needed for the project, but only 2 on the schematic. The pictures show 4 resistors on the board. I do not beleive one of them to be a 5 ohm as the parts picture indicate you are using an 8 ohm speaker. Schematic shows R1, R3, R4 - no R2. None of the comments are about the missing resistor. So - where does the third 1000 resistor go? What have I missed? Thanks.
Reply 5 months ago
I don't think you missed anything - there are two 1000 ohm resistors in the schematic - stick with that and you should be fine. Have fun!
Also, there is more info on my web page here:
http://www.trainelectronics.com/Animation-thunder-...
dave
Reply 5 months ago
Thanks for the response Dave.
Question 2 years ago on Step 11
Looking to build a thunder and lightning controller circuit for my layout (along with a spinning funnel cloud). Due to product availability, I've had to go the Arduino Uno route.Being a total Arduino novice, I'm wondering if anyone has built a solution with this product - looking for schematic and/or sketch. Thanks.
Answer 2 years ago
Jon - the UNO could
be used for the project but you would have to wire it using the
schematic that I drew for the smaller Arduino - see:
http://www.trainelectronics.com/Animation-thunder-lightning/index.htm
the pin numbers are the same.
Good luck & Enjoy!
dave
Reply 10 months ago
Greetings,
I am also trying to build this using the UNO (newbie). I am a bit confused about the power circuit using the 7805. Is that needed when using the UNO and powering it through the power port on the UNO, 5V and then a 12V for the lights correct, or am I missing something. Thanks in advance for your assistance!
Reply 10 months ago
The UNO has its own voltage regulation so the 7805 is not needed. Use the 12 volts that goes into the UNO for the light and the 5 volts provided by the UNO
Have fun!
dave
Reply 10 months ago
Thank you very much! Its built and working perfectly!
Reply 10 months ago
Hey Dave, one more question. I wanted to build a few of these and have central audio feeding the thunder sounds instead of playing from the DFPlayer. So basically the Uno would be receiving audio in and pulsing the light according to the thunder sound. Have you tried that or recommend how to do it?
Thanks.
Reply 10 months ago
I haven't tried what you describe but it should work. Good luck!
Question 1 year ago
Is it possible to make this, but without a button?
So as soon its get power, it will run "forever"?
What should i replace in the code? Thanks :)
Answer 1 year ago
Try changing the "0" to a "1" in the last line of this DO loop
change:
do {
buttn = digitalRead(buttonPin); // pins closest to power pins
} while (buttn == 0);
to:
do {
buttn = digitalRead(buttonPin); // pins closest to power pins
} while (buttn == 1);
Reply 1 year ago
Thanks for reply! Really like this project.
Will try your code.
3 years ago
Thank you for sharing this project. I built it and plan to modify it for my train set too. I appreciate your hard work and your skills. Thanks.