In this Instructables I am going to show you how to create a spectrum analyzer powered by an Arduino nano. Please watch my video first to get a rough explaination on how to build it.

A spectrum analyzer basically analyzes the intensity of different frequencies in a song. To display those values we are going to use a 10x10 RGB LED-Matrix. Every time one of the columns reaches the top of the matrix the hue value of the colors increases and the matrix looks different.

Step 1: Building the Matrix

To make this awesome spectrum analyzer I first had to build the RGB LED-Matrix. To accomplish this task I have followed the Tutorial of GreatScott on YouTube. He did an awesome job in explaining how to build such a matrix. Just follow the video step by step or use his Instructables as an instruction and then come back and follow the next steps.

Important note: make sure your LEDs are connected as shown in the diagram above. Otherwise my sketch won't work flawless with your matrix.

Step 2: Additional Parts

To convert your Matrix into a spectrum analyzer you need the following additional components:

  • 2x 3.5mm headphone sockets
  • 2x 1.8kOhm resistors
  • 1x 4.7kOhm resistor
  • some flexible wire

First of all I had to include the two headphone sockets to the matrix. We use them to loop the audio signal through the matrix to analyze it. We basically connect our audio source (e.g. a smartphone) to one of them and a speaker to the other one. I just drilled two holes according to the diameter of my sockets and glued them in place with two component adhesive.

In the next step you will see why we need those resistors.

Step 3: Connecting the Components

First of all, I have connected 3.3V to the AREF pin of the Arduino through a 4.7kOhm resistor. This is to get a better resolution of the sampled values of the audio signal since they normally reach a maximum of only 1V Peak-to-peak. The reason why we get a better resolution is that the 1024 values of the ADC are now not longer mapped to 0-5V but to 0-2.88V (2.88V because of the 4.7kOhm resistor). To understand why we used a 4.7kOhm resistor and how I have calculated those 2.88V have a look at the analog reference article on the Arduino website.

But there is one problem if you want to analyze an audio signal with an Arduino. The signal is mirrored around 0V. Therefore we get both positive and negative voltage peeks. And because Arduinos can't handle negative voltages we have to get rid of them and create an offset.

As you can see in the schematic above I used a voltage divider with two 1.8kOhm resistors to solve this problem. Since they have both the same resistance the signal now alters around 1.44V (2.88V/2) and we got rid of those negative voltages. To make life easier you can solder the resistors directly to the pins of your Arduino nano.

Lastly I connected the two audio jacks. I simply connected both right and both left channels to each other. I then soldered both GND pins together and connected them to the GND wire of the matrix. Which of the two channels you then connect to the Arduinos A7 Pin / voltage divider doesn't really matter.

Step 4: Programming

To program the built in Arduino nano you first of all have to download both the FastLED and the FFT library. Make sure you have exactly these versions of the libraries listed below otherwise the sketch may not work. Also check if you use at least Arduino IDE 1.6.8 or higher. After you have included both libraries into your Arduino IDE you have to open the "SpectrumAnalyzer" sketch.

Now change the data pin of your LEDs, upload it to your Arduino, connect an audio source and speakers and it is done!

Hope I could help you with this Instructables. If you have any questions feel free to ask them in the comments section below.

<p>It's good result!</p><p><iframe allowfullscreen="" frameborder="0" height="281" src="//www.youtube.com/embed/E5_6OgPVaZA" width="500"></iframe></p>
<p>I have fixed the code issue I was having in uploading the sketch. I had<br> to delete the arduino15 folder and it seemed to do the trick. Now that <br>the code is on my Arduino Uno and I have changed the analog pin from a7 <br>to a0 in the code, Im not too sure what digital pin the signal for the <br>LED matrix is supposed to come from? I found a signal on D3 and D1 but <br>they are only lighting up the first LED in my matrix chain. Am I even <br>using the right pins to connect to the LED matrix?</p>
<p>I'm sorry but I am not able to open the picture / video you added to your comment. Have you tried to upload an example sketch of the FastLed library to your arduino? It's always a good idea to test all parts of your project individually. I would suggest to upload a simple test program which you can find in the library to see if you have connected and configured the leds properly.</p>
<p>I have made my LED matrix and finally completed the circuit. So I turned my focus to the code thinking it will be a breeze after reading all of the comments and most people not reporting issues, but rather success stories. But of course I get an error upon compiling the code. I will say I am using Arduino IDE 1.6.9 and a Teensy 3.2 microcontroller which is compatible with Arduino.</p><p>The error read:</p><p>TIMSKO was not declared</p><p>Which came from the line:</p><p>TIMSKO = 0;</p><p>I know you didnt write the code but do you know what is happening and why it wont compile for me but it will for most everyone else upon first try?<br></p>
<p>In case you didn't receive my private message:</p><p>please check your code again. I think you wrote TIMSKO istead of TIMSK0. The letter at the end must be a zero.</p><p>kind regards and good luck with your project!</p>
<p>i keep getting this error message. Arduino: 1.8.1 (Windows 10), Board: &quot;Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)&quot;</p><p>In file included from C:\Users\\Downloads\FRF9LJ5IVO3SD4B\FRF9LJ5IVO3SD4B.ino:16:0:</p><p>C:\Users\Documents\Arduino\libraries\FastLED-master/FastLED.h:17:21: note: #pragma message: FastLED version 3.001.005</p><p> # pragma message &quot;FastLED version 3.001.005&quot;</p><p> ^</p><p>C:\Users\</p><p>\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.17\cores\arduino\main.cpp: In function 'main':</p><p>C:\Users\</p><p>\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.17\cores\arduino\main.cpp:51:1: error: r28 cannot be used in asm here</p><p> }</p><p> ^</p><p>C:\Users\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.17\cores\arduino\main.cpp:51:1: error: r29 cannot be used in asm here</p><p>C:\Users\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.17\cores\arduino\main.cpp:51:1: error: r28 cannot be used in asm here</p><p>C:\Users\\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.17\cores\arduino\main.cpp:51:1: error: r29 cannot be used in asm here</p><p>lto-wrapper: C:\Users\\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\4.9.2-atmel3.5.3-arduino2/bin/avr-gcc returned 1 exit status</p><p>c:/users//appdata/local/arduino15/packages/arduino/tools/avr-gcc/4.9.2-atmel3.5.3-arduino2/bin/../lib/gcc/avr/4.9.2/../../../../avr/bin/ld.exe: lto-wrapper failed</p><p>collect2.exe: error: ld returned 1 exit status</p><p>exit status 1</p><p>Error compiling for board Arduino/Genuino Mega or Mega 2560.</p><p>This report would have more information with</p><p>&quot;Show verbose output during compilation&quot;</p><p>option enabled in File -&gt; Preferences.</p><p>can you help?</p>
I met the same situation, but I solved the problem by replaceing the Arduino15 folder, which is in my C:\Users\\AppData\Local\Arduino15. It contains the core files for AVR programming. I replaced it with the original one(which is automatically built when you run Arduino IDE for the first time). Remember to backup! Hope this can help you.
<p>I am really sorry but I can't really help you with the code because a friend of mine wrote it. <br>But it may be possible, that the problem occours because you use a arduino mega and not a nano. But I'm not sure. </p>
<p>Good project you made here. Is using pin A7 necessary in this case, or I can use any other Analog input? I`m just using another diy arduino compatible board.</p>
You can use any analog pin you like :) Just change it in the code
<p>Thank you for the reply ^^ but...</p><p>I almost built my matrix and just realised how dumb I am... I made wrong led connection pattern like on the picture instead of your in the instruction. So every even column is reversed. I think it can be fixed with code. Colud you please help me with it, because its my first arduino project and I`m not familiar with coding yet. Thank you again.</p>
<p>hello Alexey, I wonder how you changed A7 to another pin. You know, it's a </p><p>binary data. What if I want to use A0? </p><p>The original setting is :</p><p>ADMUX = 0b01000111; //use pin A7</p><p>(Line 54)</p>
<p>I think pin A0 should be 0b01000000.</p><p>But I'm not 100% sure because a friend of mine actually wrote the code.</p>
<p>Try this one</p><p>for(unsigned char y = 0; y &lt; 10; y++){ //set colors of pixels according to column and hue</p><p> if(hs[column] &gt; y){</p><p> if (y % 2) {</p><p> leds[(y*10)-column+9] = CHSV((hue*10)+(column*10), 255, 200);</p><p> }</p><p> else {leds[(y*10)+column] = CHSV((hue*10)+(column*10), 255, 200);</p><p> }</p><p> //leds[y+(column*10)] = CHSV((hue*10)+(column*10), 255, 200);</p><p> } else {</p><p> if (y % 2)</p><p> {leds[(y*10)-column+9] = CRGB::Black;</p><p> } else {</p><p> leds[(y*10)+column] = CRGB::Black;</p><p> }</p><p> }</p><p> }</p>
<p>I'm glad you're trying to follow my instructables. However I'm sorry that I can't really help you with the code because a friend of mine wrote it. Is there any chance to change your led connection to the one i showed in the instructables? (desolder the cables and solder them in the correct way)<br>Also check out my new video I made about the spectrum analyzer: <iframe allowfullscreen="" frameborder="0" height="281" src="//www.youtube.com/embed/ZqfmvpSD20s" width="500"></iframe></p>
<p>One more thing. I don't have 2x1.8k resistors at my hand so can I use 2x1.68k instead? I tried and it just doesn't work. No LEDs are lighting up. And serial monitor outputs some mes(if there is no music, its in one line and if there is an input then this mess is outputting in multiple lines).</p>
<p>1.68k resistors should work fine. I think the problem are not the resistors but the code. Are you able to send a screenshot of your serial monitor when you apply music to your arduino?</p>
<p>Sure </p>
<p>Sorry I forgot to mention that you have to open the <a href="https://www.youtube.com/watch?v=PyD9cMarVJk&ab_channel=StudioEarsTones" rel="nofollow">Serial Plotter</a> not the Serial Monitor. You can test your code with a video that plays a sine wave (such as <a href="https://www.youtube.com/watch?v=PyD9cMarVJk&ab_channel=StudioEarsTones" rel="nofollow">this</a> one) You then shoud see the sine wave on your serial plotter.</p>
<p>Seems like I used wrong Arduino IDE version, so by updating it to 1.6.7 problem was solved. Every second column is still reversed but that's still looks fine to me. Will post some photos later.</p>
<p>Nice :D Glad you got it to work :)</p>
<p>Also I can hear some pitch noise that the circuit outputs to 3.5 mm jack. Hope resistors with right value will fix this problem for me.</p>
LEDs are already glued on its places so I can't change their pattern. Anyways, I found some code for zigzag matrix so will try to make a Frankenstein code for myself. Cool video :)
<p>I ran your code and a lot of &quot;error: r29 cannot be used in asm here&quot; came out, just the same as <a href="https://www.instructables.com/member/ljcorky" rel="nofollow">ljcorky</a> 's comment. I tried to run fft_adc example code, but more errors occured. I even tried different IDE, including 1.8.1 and 1.6.7. TAT</p><p>Is that a common problem? Or just me? If the FFT library is not available, what can I do to replace it? Please help! THX</p>
<p>Excellent code. Works only with 1.6.7</p><p>If you have a Matrix Horizontal Snake Bottom Left then the code can be hacked -&gt;&gt;&gt;</p><p>for(unsigned char y = 0; y &lt; 10; y++){ //set colors of pixels according to column and hue</p><p> if(hs[column] &gt; y){</p><p> if (y % 2) {</p><p> leds[(y*10)-column+9] = CHSV((hue*10)+(column*10), 255, 200);</p><p> }</p><p> else {leds[(y*10)+column] = CHSV((hue*10)+(column*10), 255, 200);</p><p> }</p><p> //leds[y+(column*10)] = CHSV((hue*10)+(column*10), 255, 200);</p><p> } else {</p><p> if (y % 2)</p><p> {leds[(y*10)-column+9] = CRGB::Black;</p><p> } else {</p><p> leds[(y*10)+column] = CRGB::Black;</p><p> }</p><p> }</p><p> }</p>
<p>You only monitor one channel. Some stereo signals have huge differences between both channels so you could add left and right with resistors of high impedance. Your trick to shift up the signal is clever, I would have uses a reverse diode but this introduces distortion. Good job indeed.</p>
Thats true but ther are only few songs I now that habe such high differences. Furthermore I have already glued up my case and don't want to open it again.
<p>Great Instructable, and really the first time I see a real spectrum analyzer without any additional analog or external graphic equalizing chip.</p><p>Any chance you can post a youtube video of the result?</p>
Thanks :) I already di upload a youtube video. The third picture is actually the video :) But due to a copyright claim it is without music. I am going to make a new video today.
<p>Great instructable, this came out really well :)</p>
<p>Thank you :)</p>
nice work
<p>Thanks :)</p>

About This Instructable




More by FischiMc:Arduino Spectrum Analyzer on a 10x10 RGB LED-Matrix 
Add instructable to: