• Circuits

    Music Visualizer Cube with RGB LED Strip (WS2812B)

    This is a Sound/ Music Reactive Cube with RGB LED Strip (WS2812B) inside of it, created as part of a group project in the MGA318 lesson.

    I was responsible for the Coding part as well as the assembling and connection of the Arduino with the PC and the Arduino with the Sound Sensor and RGB LED Strip.

    Hardware/ Software used

    Circuit

    • Arduino Uno
    • Sound Sensor
    • Arduino Software
    • WS2812B LED Strip
    • Batteries
    • 4 x AA Battery Holder
    • Cables

    2 Final Codes

    In the end, I created 2 codes, the 1st one (left side) is the one that we used for the final Music Visualizer Cube. It reacts to the sound levels and generates a different color to the whole RGB Led Strip based on the frequencies of the music. While the 2nd one (right side), it does the same thing, but with a slight difference, it generates the different colors in the middle of the RGB Led Strip and lets them travel at the end of it on both sides.

    #include <FastLED.h> //FastLED library
    #define NUM_LED 60  //number of LEDs in the strip
    #define LED_PIN 5  //data pin on the arduino
    #define BRIGHTNESS 255 //default brightness
    #define updateLEDS 5 //number of LEDs to update
    CRGB led[NUM_LED]; //array of LEDs
    const int audioSignal = A0; //pin for the audio signal
    int volumeLevel; //iniger for volume level
     
    void setup() {
      pinMode(audioSignal, INPUT); //input pin for the audio signal
      Serial.begin(9600); //serial for debugging
      FastLED.addLeds<WS2812B, LED_PIN, GRB>(led, NUM_LED); //fastLED initalization
      FastLED.setBrightness(BRIGHTNESS); //set the default brightness
      delay(1000); //wait 1 sec
      resetLedState(); //turn off all the leds
    }
     
    void loop() { 
      audioRead(); //reap the audio level and map it
      Serial.println(volumeLevel); //Send volume level to the serial monitor
       
      //for loop to move all the LEDs down the strip 
      for (int i = NUM_LED - 1; i >= updateLEDS; i--) {
        led[i] = led[i - updateLEDS];
      }
       
      //for loop to set the first LED corresponding to the audio level
      for (int i = 0; i < updateLEDS; i++) {
        displayFilter(volumeLevel, i); //set the right colour magic
      }
      FastLED.show(); //updates the LED strip
      delay(20); //delay
    }
     
    void resetLedState() { //function to turn off all the LEDs
      for (int turnMeOff = 0; turnMeOff < 61; turnMeOff++) {
        led[turnMeOff] = CRGB(0, 0, 0);
      }
      FastLED.show();
    }
     
    void audioRead() { //function to read the audio  
      int rawValue = analogRead(audioSignal) * 2; //double the audio level
      volumeLevel = map(rawValue, 0, 1023, 0, 1000); //map it to 1000 values
      if (volumeLevel > 1000) { //if it's higher than 1000 it lowers it back to 1000
        volumeLevel = 1000;
      }
    }
     
    /* a lot of IF statements to set the right color.*/
    void displayFilter(int volume, int color) {
      if (volume < 1) {
        led[color] = CRGB(0, 0, 0); //0, Off/Black
      }
      if (volume >= 1 && volume < 50) {
        led[color] = CRGB(255, 0, 0); //1-49, Red
      }
      if (volume >=50  && volume < 100) {
        led[color] = CRGB(255, 0, 85); //50-99, Pinkish Red
      }
      if (volume >= 100 && volume < 150) {
        led[color] = CRGB(255, 0, 170); //100-149, Pink
      }
      if (volume >= 150 && volume < 200) {
        led[color] = CRGB(0, 0, 255); //150-199, Blue
      }
      if (volume >= 200 && volume < 250) {
        led[color] = CRGB(170, 0, 255); //200-249, Purple
      }
      if (volume >= 250 && volume < 300) {
        led[color] = CRGB(85, 0, 255); //250-299, Purplish Blue
      }
      if (volume >= 300 && volume < 350) {
        led[color] = CRGB(0, 20, 255); //300-349, Blue
      }
      if (volume >= 350 && volume < 400) {
        led[color] = CRGB(20, 85, 255); //350-399, Lighter Blue
      }
      if (volume >= 400 && volume < 450) {
        led[color] = CRGB(0, 255, 0); //400-449, Green
      }
      if (volume >= 450 && volume < 500) {
        led[color] = CRGB(0, 255, 255); //450-499, Cyan
      }
      if (volume >= 500 && volume < 550) {
        led[color] = CRGB(0, 255, 170); //500-549, Bright-Pale Green
      }
      if (volume >= 550 && volume < 600) {
        led[color] = CRGB(0, 255, 85); //550-599, Neon Green
      }
      if (volume >= 600 && volume < 650) {
        led[color] = CRGB(255, 0, 255); //600-649, Bright Pink
      }
      if (volume >= 650 && volume < 700) {
        led[color] = CRGB(85, 255, 0); //650-699, A bit Lighter Green
      }
      if (volume >= 700 && volume < 750) {
        led[color] = CRGB(170, 255, 0); //700-749, Yellowish Green
      }
      if (volume >= 750 && volume < 800) {
        led[color] = CRGB(255, 255, 0); //750-799, Yellow
      }
      if (volume >= 800 && volume < 850) {
        led[color] = CRGB(255, 170, 0); //800-849, Pale Orange
      }
      if (volume >= 850 && volume < 1000) {
        led[color] = CRGB(0, 0, 255); //850-999, Blue  
      }
      if (volume >= 1000) {
        led[color] = CRGB(255, 255, 255); //1000, White
      }
    }
    
    #include <FastLED.h>    //FastLED library
    int r=152;
    int g=0;
    int b=10;
    #define LED_PIN     5    //data pin on the arduino
    #define NUM_LEDS    60  //number of LEDs in the strip
    CRGB leds[NUM_LEDS];
    CRGB led[NUM_LEDS];
    int s=0;
     
    void setup() {
      FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
      for (int i = NUM_LEDS/2; i >= 0; i--) 
      {
         leds[i] = CRGB ( r,g,b);
         leds[NUM_LEDS-i] = CRGB (r,g,b );
         delay(40);
        FastLED.show();
      }
      Serial.begin(9600);
       pinMode(A0,INPUT);
    }
     
    void loop()
    {
      s=analogRead(A0);
      s=s*2;
      Serial.println(s);
      delay(50);
       
      if((s>650)&&(s<=700)) // Light Green
      {
        leds[(NUM_LEDS/2)-1]=CRGB (70, 255, 80);
        leds[NUM_LEDS/2]=CRGB (70, 255, 80);
      }
       else if((s>600)&&(s<=650)) //Pink
      {
        leds[(NUM_LEDS/2)-1]=CRGB (255, 150, 200);
        leds[NUM_LEDS/2]=CRGB (255, 150, 200);
      }
      else if((s>550)&&(s<=600)) //Yellow
      {
        leds[(NUM_LEDS/2)-1]=CRGB (255, 255, 0);
        leds[NUM_LEDS/2]=CRGB (255, 255, 0);
      }
      else if((s>500)&&(s<=550)) //Blue
      {
        leds[(NUM_LEDS/2)-1]=CRGB (0, 0, 255);
        leds[NUM_LEDS/2]=CRGB (0, 0, 255);
      }
      else if((s>450)&&(s<=500)) //Dark Yellow
      {
        leds[(NUM_LEDS/2)-1]=CRGB (153, 153, 0);
        leds[NUM_LEDS/2]=CRGB (153, 153, 0);
      }
      else if((s>400)&&(s<=450)) //Purple
       {
         leds[(NUM_LEDS/2)-1]=CRGB (255, 50, 255);
        leds[NUM_LEDS/2]=CRGB (255, 50, 255);
       }
       else if((s>350)&&(s<=400)) //Dark Blue
      {
        leds[(NUM_LEDS/2)-1]=CRGB (10, 25, 217);
        leds[NUM_LEDS/2]=CRGB (10, 25, 217);
      }
       else if((s>300)&&(s<=350)) //Darker Blue
       {
         leds[(NUM_LEDS/2)-1]=CRGB (50, 50, 150);
        leds[NUM_LEDS/2]=CRGB (50, 50, 150);
       }
       else if((s>250)&&(s<=300)) //Red
       {
         leds[(NUM_LEDS/2)-1]=CRGB (230, 0, 10);
        leds[NUM_LEDS/2]=CRGB (230, 0, 10);
       }
      else if((s>200)&&(s<=250)) //Green
       {
         leds[(NUM_LEDS/2)-1]=CRGB (0, 160, 0);
        leds[NUM_LEDS/2]=CRGB (0, 160, 0);
       }
       else if((s>150)&&(s<=200)) //Black
       {
         leds[(NUM_LEDS/2)-1]=CRGB (1, 0, 1);
        leds[NUM_LEDS/2]=CRGB (1, 0, 1);
       }
      else
      {
         leds[(NUM_LEDS/2)-1] = CRGB ( r,s-100,b);
         leds[NUM_LEDS/2] = CRGB ( r,s-100,b);
      }
        for (int i = 0; i <= ((NUM_LEDS/2)-2); i++) 
      {
         leds[i] = leds[i+1];
         leds[NUM_LEDS-1-i] = leds[(NUM_LEDS)-i-2]; 
      }
     FastLED.show();
     delay(25);
    }