Dancing Lights

 

Dancing Lights

 

Materials:

Wiring:

The LED strip consists of 3 wires

LED Strip Wires

 

LED Strip Wires

 

The red wire corresponds to power supply (5V), the white wire corresponds to ground, and the green wire corresponds to data.


These three wires are connected to a JST connector which can be used to connect other LED strips in series.

 

JST Connector

 

JST Connector

 



Connect three male-to-male jumper wires to the JST connector as shown below:  

 

 

 

 

 

 

Male to Male Jumper Wires

 

Male to Male Jumper Wires

 

 

Now connect three female-to-male jumper wires to the electret microphone.

 

Female-to-Male Jumper Wires

 

Female-to-Male Jumper Wires

 

Connect the microphone and the LED strip as follows:

 

Electret Microphone

Arduino UNO

VCC

3.3V

GND

GND

AUD

Pin A0



LED Strip

Arduino UNO

+5V

5V

GND

GND

Din

Pin 6

 

In the end, your circuit should look something like this:

 

Final Wiring Setup

 

Final Wiring Setup

 

Code:

#include <Adafruit_NeoPixel.h>  //Library to control LED Strip 
#define led 6  //Data pin of LED Strip 
#define mic A0   //Mic pin 
#define pixels 30  //Number of pixels in the LLED Strip 
int sensorValue = 0;  //Initial value of mic   
int threshold = 105;  //Change this number to set the min value the mic has to read in order to turn the lights on 
long randNumber, randNumber1, randNumber2;  //Define random numbers to generate different colors  
Adafruit_NeoPixel strip = Adafruit_NeoPixel(pixels, led, NEO_GRB + NEO_KHZ800);  //Initialize library 

void setup() {
 strip.begin();  //Prepare data pin for output
 strip.show();  //Turn off LEDs
}

void loop() {
  randNumber = random(0,255);  //Generate first random number between 0 and 255
  randNumber1 = random(0,255);  //Generate second random number between 0 and 255
  randNumber2 = random(0,255);  //Generate third random number between 0 and 255
  
 int sensorValue = analogRead(mic);  //Read analog value from mic
 sensorValue = map(sensorValue, 0, 1023, 0, 255);  //Scale down analog readings to have a value between 0 - 255 
 
 if(sensorValue>threshold){  //Check if analog value is greater than threshold
   for(int i=0; i<pixels; i++){  //Start for loop 
     strip.setPixelColor(i, randNumber, randNumber1, randNumber2);  //i selects the LED in the strip. randNumber sets the red intensity. randNumber1 sets the green intensity. randNumber2 sets blue intensity. 
   }
   strip.setBrightness(sensorValue);  //Set brightness of strip to the analog value of the mic
   strip.show();  //Display colors 
   delay(sensorValue/2);  //Wait some time to be able to see the different colors generated. 
  }
  else{  //Check if value is below threshold 
    for(int i=0; i<pixels; i++){  //Start for loop 
     strip.setPixelColor(i, 0, 0, 0);  //Turn off all LEDs in the strip 
   }
   strip.show();  //Display LEDs turned off
  }
}

Code Explanation:

#include <Adafruit_NeoPixel.h>  //Library to control LED Strip 

This is the library required to drive the LEDs. You can download the library here:

https://github.com/adafruit/Adafruit_NeoPix

 

#define led 6  //Data pin of LED Strip 
#define mic A0   //Mic pin 
#define pixels 30  //Number of pixels in the LLED Strip

Here we define the Arduino pins used for the microphone and the LED Strip. We also define how many LEDs we are going to be controlling.

int sensorValue = 0;  //Initial value of mic   
int threshold = 105;  //Change this number to set the min value the mic has to read in order to turn the lights on 
long randNumber, randNumber1, randNumber2;  //Define random numbers to generate different colors 

Here we initialize the reading value of the microphone to zero and set its threshold value to 105. The maximum value the threshold can have is 255. However, at this value the microphone won’t pick up anything and thus, the lights won't turn on at all. This variable is used to set the microphone’s “sensitivity” to volume. Finally, we create three variables that will set the intensity of each RGB channel.

Adafruit_NeoPixel strip = Adafruit_NeoPixel(pixels, led, NEO_GRB + NEO_KHZ800);  //Initialize library

This command initializes the library with the number of pixels, the data pin, and the WS2812 driver.

void setup() {
 strip.begin();  //Prepare data pin for output
 strip.show();  //Turn off LEDs
}

In the setup section the begin function is used to prepare the the data pin for the display  output. The show function is used to clear all LEDs (Turn them off).

void loop() {
  randNumber = random(0,255);  //Generate first random number between 0 and 255
  randNumber1 = random(0,255);  //Generate second random number between 0 and 255
  randNumber2 = random(0,255);  //Generate third random number between 0 and 255

In the loop we generate three different random values that can go from 0 to 255.  These are the values that will set the intensity of each RGB channel.

int sensorValue = analogRead(mic);  //Read analog value from mic
 sensorValue = map(sensorValue, 0, 1023, 0, 255);  //Scale down analog readings to have a value between 0 - 255

Then we read the analog input from the microphone. These readings can range from 0 to 1023. In order to scale down the values to have a range from 0 to 255, we use the function map.

if(sensorValue>threshold){  //Check if analog value is greater than threshold
   for(int i=0; i<pixels; i++){  //Start for loop 
     strip.setPixelColor(i, randNumber, randNumber1, randNumber2);  //i selects the LED in the strip. randNumber sets the red intensity. randNumber1 sets the green intensity. randNumber2 sets blue intensity. 
   }
   strip.setBrightness(sensorValue);  //Set brightness of strip to the analog value of the mic
   strip.show();  //Display colors 
   delay(sensorValue/2);  //Wait some time to be able to see the different colors generated. 
  }

Here we check if the readings from the microphone input are greater than the threshold. If the case is true, then we use a for loop to set the color of each pixel in the strip. The color will depend on the values of the red, green, and blue channels, which are defined by the variables randNumber, randNumber1, and randNumber2.  Once all the pixels have a color, we set their brightness equal to the analog reading from the mic. The brightness function can only take values from 0 to 255, so we use the scaled down value obtained using the map function. This means that the louder the sound, the brighter the LEDs will light up. Then we use the show function to turn on the LEDs with their respective color and brightness. Then we leave them on for some time to be able to see the colors that are being generated. If we don’t use a delay the flickering of the lights would be so fast that you would not be able to see the colors clearly.

else{  //Check if value is below threshold 
    for(int i=0; i<pixels; i++){  //Start for loop 
     strip.setPixelColor(i, 0, 0, 0);  //Turn off all LEDs in the strip 
   }
   strip.show();  //Display LEDs turned off
  }
}

In the last section of the code  if the analog reading of the microphone is less than the threshold, then we turn off the LEDs.

Results

Now you just have to play your favorite music and enjoy the light show.