Thunder And Lightning Effect Using Pixel Strip, LEDS Or Light Bulb

Thunder And Lightning Effect Using Arduino  nano,  DF Player, and a relay to control an LED pixel strip, standard LEDS or an AC light bulb or LED spotlights.


Here we define the initial output state to “LOW” when no sound is detected. When a small amount of sound will detect, the current will flow through the input pin of the relay module. In this situation, the circuit will go to the “HIGH” state. Note that this condition is working only when we connect the relay module to the output of the Arduino.

Otherwise, if we want to run a single LED or LED stripe, we need to change the code. In this situation, it should be set to “HIGH” while it is in an initial position. And when a sound will be detected, it will go to “LOW” condition.
Arduino Code

int soundSensor = 8; //define sound sensor pin
int LED = 9; //define relay input pin
void setup()
pinMode (soundSensor, INPUT); //define Sound Sensor as input
pinMode (LED, OUTPUT); //define LED Strip as output
void loop()
int statusSensor = digitalRead (soundSensor); //define variable of the sound sensor status
//and read value of the sensor’s

if (statusSensor == 1) //When the Sensor detects a signal
digitalWrite(LED, LOW);

else //If no signal is detected
digitalWrite(LED, HIGH);


DFPlayer Arduino Library

To compile code to the Arduino we need a DFPlayer Mini Mp3 library file. You can just download this from the link.
Arduino Code

#include “Arduino.h”
#include “SoftwareSerial.h”
#include “DFRobotDFPlayerMini.h”
int ledPin = 9; // Bulb Output
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);
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.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.print(F(“Playing thunder sound, file number: “));
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: “));



We hope that you found the information in this page useful. The internet is all about getting and giving. And giving back is the best. Please donate $1 or more to aid us in our continuing quest to add to and polish the information in our site.

Scroll to top