Friday, March 14, 2014

Spectrum Analyzer with Arduino + Adafruit 16x32 RGB Matrix!

As you may have noticed, I've been toying around with tons of LED's and looking at ways they apply to the user interface of music machines.   One of the first use cases I had for an LED matrix was a spectrum analyzer!    Obviously, this is nothing new and the ground work has totally been laid out by a bunch of people smarter then I, but its interesting none-the-less.

I came across three projects I found interesting

1)  32 x 16 led matrix arduino spectrum analyzer



Pretty cool, but no source available..  looks like a pretty solid refresh rate though.   Some detail about its workings in the comments

2) Arduino / Processing Audio Spectrum Analyzer


This one has source code (yay).  It uses processing on the pc to do the FFT.    This is the basic configuration I've got setup in my first video which is my implementation.     On mine, I'm just capturing off the mic on my laptop, but you can target pretty much any output.   I'd like it to be sort of a stand alone audio device, which leads me to... 

3) LOLShield Spectrum Analyzer


This one uses the "LoL" shield and SURPRISINGLY, uses an 8-bit version of fix-fft to do the FFT calculations ON THE ARDUINO! This is amazing to me, seems like everyone insists you can't do it. I suppose in this situation theres some "good enough" moments in resolution to be expected, but it still makes for some super cool possibilities in LED, Audio and beyond!

Putting it all together

The first piece of the puzzle in #2 is the processing sketch.    I'd never used processing, but figured wth, may as well install it with the other 10^25 development tools I've got on here.   I'd known that arduino was either written with, or based on processing - much to my surprise, it looks almost identical.   I was able to get the sketch running and sending serial data to my arduino, so now I had to look at the arduino sketch itself. 

The code on the instructable got my pretty close, but I'm using the Nootropic RGB Matrix backpack for the Adafruit LED matrix. This is a little different  then the instructable, but I'm not scared.   Adafruit provides two libraries that I used.  The first handles the matrix intricaies itself, spi data going to a bunch of chips on the matrix itself. The second deals w/ the graphics aspect, drawLine, drawPixel, print, etc.     I was able to quickly refactor the GFX lib in, but I had a few problems along the way. 

1) The original code used an array of 16 bit ints.  Any time i operated on that, the sketch crashed.  It actually used two - the other was a temp array to check if columns needed to be updated.    Seems like it just blew out the memory constraints as soon as it started? 

2) I couldn't get the arduino to actually start on the srial data, or maybe i might have if i didn't get so tripped up by #1.   Either way, I refactored the "loop" function in my sketch to remove some of the iteration.  The library handles any buffering of the data, and so I call swapBuffer at the end of a frame.

3)  I was experiencing sort of slow updates.  I removed the delay(1) from their code, and I decreased it from 2 column wide blocks to 1 column for my demo.   I think I'll try and utilize the GFX libraries drawRectangle function to try and clean that up a little, and maybe move the refreshes to an interrupt or timer so I can just set a known refresh rate and not hold up the incoming serial data too much.

I've posted the code on my ardmos repository on github here: https://github.com/billieblaze/ardmos/tree/master/test/adafruitmatrix_spectrumanalyzer

And heres a video of it in action!


2 comments:

Unknown said...

Hello Bille Blaze,

Great work!
I am doing something very similar as well, and I was trying to find your processing code on Github, but I couldn't find it. I also have the Nootropic RGB Backpack as well as the Adafruit 16x32 panel. As far as setting up the audio input, this is where i'm not sure what to do. I want the music that is playing on my computer from spotify to generate the spectrum from processing.

If I need to provide more clarification I would more than glad to.

Thanks!

zars said...

Hi, sorry me being silly, can i have a download link for processingSendSpectrum.pde please?