Project in progress
Music Adaptive RGB LED with Arduino and Processing

Music Adaptive RGB LED with Arduino and Processing

Make a RGB LED shine adaptively to a music played on a computer with processing.

  • 2,679 views
  • 8 comments
  • 23 respects

Components and supplies

About this project

I was playing around with processing and FFT (Fast Fourier Transform) and had the idea to use the 3 components of a FFT as a RGB colour and communicate it to my Arduino card using the processing serial library to make a RGB led shine with this colour.

The result is a led that shines adaptively to music and it’s nice looking and very easy to do.

For those who don't know what Processing is. It is a programming tool built on top of java, for coding visual arts. You can download it here:

https://processing.org/

and for this project you will need to install the Processing Sound library.

https://processing.org/reference/libraries/

How to change the audio file path:

After creating a new sketch and writing the code below, go to your sketch folder and create a folder and name it "data".

Copy-past the audio file you want, to the "data" folder.

Then change the file name in your code.

sample = new SoundFile(this, "music.mp3");  // select your music here

Please note that Processing works very well with mono-channel sound files. You could find some issues with stereo files.

Code

music_led.inoArduino
int red_pin = 9;
int green_pin = 10;
int blue_pin = 11;


int r = 0;
int g = 0;
int b = 0;

void setup()
{
  Serial.begin(9600);
  
  pinMode(red_pin, OUTPUT);
  pinMode(green_pin, OUTPUT);
  pinMode(blue_pin, OUTPUT);  
}
 
void loop()
{
  if (Serial.available() > 0) {
                r = Serial.read();
                g = Serial.read();
                b = Serial.read();
                
                analogWrite(red_pin, r);
                analogWrite(green_pin, g);
                analogWrite(blue_pin, b); 
       }
  delay(100);

}
music_led.pdeProcessing
import processing.sound.*;  
import processing.serial.*;  

SoundFile sample;   
FFT fft;    
AudioDevice device;  

Serial myPort;   

float[] spectrum =new float[3];
float r, g, b;

float smoothing = 0.2;

public void setup() {
  size(640, 360);  
  
  device = new AudioDevice(this, 44000, 4);
  
  sample = new SoundFile(this, "music.mp3");  // select your music here
  sample.loop();

  fft = new FFT(this, 4);
  fft.input(sample);
  
  String portName = Serial.list()[2]; // for me it's 2 but could be at a different port
  myPort = new Serial(this, portName, 9600);
}      

public void draw() {
  background(0, 0, 0);
  
  fft.analyze();

   spectrum[0] += (fft.spectrum[0] - spectrum[0]) * smoothing;
   spectrum[1] += (fft.spectrum[1] - spectrum[1]) * smoothing;
   spectrum[2] += (fft.spectrum[2] - spectrum[2]) * smoothing;
   
   r = spectrum[0]*height*4;
   g = spectrum[1]*height*4;
   b = spectrum[2]*height*4;
   
   stroke(255,0,0); 
   line( width/3 - 50, height, width/3 - 50, height - r );
   stroke(0,255,0);
   line( 2*width/3 - 50, height, 2*width/3 - 50, height - g);
   stroke(0,0,255);
   line( width - 50, height, width - 50, height - b );
   
    myPort.write((int)r % 255);
    myPort.write((int)g % 255);
    myPort.write((int)b % 255);
}

Schematics

Circuit
Music led bb muqupvalj9

Comments

Similar projects you might like

Arduino Bluetooth Basic Tutorial

by Mayoogh Girish

  • 454,838 views
  • 42 comments
  • 238 respects

Home Automation Using Raspberry Pi 2 And Windows 10 IoT

Project tutorial by Anurag S. Vasanwala

  • 285,508 views
  • 95 comments
  • 671 respects

Security Access Using RFID Reader

by Aritro Mukherjee

  • 229,416 views
  • 38 comments
  • 237 respects

OpenCat

Project in progress by Team Petoi

  • 195,956 views
  • 154 comments
  • 1,362 respects
Add projectSign up / Login