# Tiny Arduino Music Visualizer

## Overview

**UPDATE: this is an older guide designed for boards like the Arduino Uno and Adafruit METRO 328. It relies on _assembly language_ code that’s specific to 8-bit AVR microcontrollers and will not work with boards based on other chips (ESP32, RP2040, SAMD, etc.).**

http://youtu.be/Tmpl5KA02S4

(Music: _The Owl Named Orion_ by Dan-O at DanoSongs.com)  
  
Here’s an easy-to-build project that really packs a lot of _blinkenlight_ for the effort: a little pocket-size music visualizer we call “Piccolo.”  
  
Set Piccolo next to the telly or some speakers and you’ll see the lights respond to music and sound — lowest notes toward the left end of the graph, highest notes toward the right.  
  
Technically this would be called a “spectrum analyzer,” but as this is not a precision scientific instrument, we’re more comfortable labeling it a “visualizer.” It’s strictly for show.  
  
This intermediate Arduino project shows a clear progression from input to processing and then output in a package that’s appealing and easy for minds to grasp: music and lights. It’s not abstract or “science-y” unless you choose to peel back the layers…

# Tiny Arduino Music Visualizer

## Wiring

Begin by assembling the LED matrix and “backpack” board&nbsp;as described in the [Adafruit LED Backpacks tutorial](http://learn.adafruit.com/adafruit-led-backpack/bi-color-8x8-matrix).  
  
If using the LED matrix for the first time, we <u>very strongly</u> recommend working through that full tutorial first. This will let you test and confirm that the Matrix is properly assembled before moving on to this more advanced project.

![](https://cdn-learn.adafruit.com/assets/assets/000/002/700/medium800/led_matrix_backpack.jpg?1396785913)

If working with a breadboard as shown below, solder row headers on to the microphone breakout and LED matrix backpack boards (three pins on the former, four on the latter). You can optionally use 90 degree headers if you’d like either component to be positioned standing up.

Thanks to the “smarts” of the LED matrix backpack, this whole project requires fewer than a dozen connections total:

- Connect the Arduino’s **3.3V** &nbsp;pin both to the mic amp **VCC** pin and the Arduino’s **AREF** pin.&nbsp;We use one of the power rails on the breadboard to route 3.3 Volts to both locations.&nbsp;The AREF connection is very important — don’t overlook this!
- Connect the Arduino&nbsp; **5V** pin to the LED matrix **+** pin.
- Connect Arduino&nbsp; **GND** pin to both the mic amp **GND** pin and the LED matrix **–** pin. You can use a breadboard power rail, or the Arduino has multiple GND pins available.
- Connect Arduino **analog pin 0** to the mic amp **OUT** pin.
- Connect Arduino pins **SDA** and **SCL** to the matrix backpack **D** (data) and **C** (clock) pins, respectively. **Earlier Arduino boards don’t include SDA and SCL pins — instead, use analog pins 4** (for SDA) **and 5** (SCL) **.**

For a current “R3” Arduino board (such as the Arduino Leonardo or more recent Arduino Uno and Mega&nbsp;boards), the wiring should resemble this:

![](https://cdn-learn.adafruit.com/assets/assets/000/002/696/medium800/led_matrix_layout-r3.png?1396785885)

If using a “classic” Arduino board — an Arduino Uno version R1 or R2, Arduino Duemilanove or&nbsp;Diecimila (with 328P chip), the matrix data and clock lines should be connected to analog pins 4 and 5:

![](https://cdn-learn.adafruit.com/assets/assets/000/002/695/medium800/led_matrix_layout-classic.png?1396785867)

_ **Note: the project only works with 8-bit AVR boards.** It relies on a library that’s written in assembly language, and won’t compile on newer 32-bit boards like Arduino Zero or Adafruit Metro Express._

Piece of cake!

You can power the Arduino from the USB connection or using a battery or power supply connected to the DC jack. The LED matrix draws a couple hundred milliamps at most, so we can safely power it through the Arduino.

# Tiny Arduino Music Visualizer

## Code

This button will download a folder containing all the code needed for this project:

[Download “Piccolo” Arduino source code](https://github.com/adafruit/Adafruit_Learning_System_Guides/tree/main/Tiny_Music_Visualizer)
The code can [also be found on Github](https://github.com/adafruit/Adafruit_Learning_System_Guides/tree/main/Tiny_Music_Visualizer).

After uncompressing the ZIP file, inside you’ll find a folder called _Piccolo._ This can be moved to your Arduino documents folder if you like. The **Piccolo.ino** file in there is the main project source code — open this in the Arduino IDE.

Two libraries are required for this: **Adafruit\_LEDBackpack** and **Adafruit\_GFX**. If you worked through the basic LED matrix backpack guide first, these should already be present. Otherwise, both can be found in the Arduino Library Manager (Sketch→Include Library→Manage Libraries…)

With the Piccolo sketch open, select your Arduino board type and serial port from the Tools menu. Then click the Upload button. After a moment, if all goes well, you’ll see the message “Done uploading.” The project should now be responsive to sound…try clapping your hands!

## Troubleshooting
If there’s no response from the device, try the following:

- Test the matrix using the example code from the Adafruit\_LEDBackpack Library. If there’s no response, the clock and data pins might be swapped, or the matrix may have been soldered to the board backwards.
- Double-check all wiring against the diagrams. Did you include the 3.3V-to-AREF connection?&nbsp;D and C pins from the matrix backpack should connect to SDA and SCL on newer Arduinos, or analog pins 4 and 5 on older boards.
- The gain on the mic amplifier may be set low. There’s a dial on the back of the board that can be adjusted with a small screwdriver.
- Try increasing the music volume.

## Principle of Operation
Using the normal Arduino analogRead() function would be <u>much</u> too slow for sampling audio.&nbsp;Instead, a feature&nbsp;of the microcontroller’s analog-to-digital converter called _free-run mode_ is utilized. This automatically takes repeated analog samples at precise intervals…about 9.6 KHz for this project, the maximum a 16 MHz Arduino can handle with 10-bit samples.

This is strictly mono. There isn’t enough RAM or processor _oomph_ for stereo.  
  
The raw audio samples are converted into a _frequency spectrum_ using a _[fast Fourier transform](http://en.wikipedia.org/wiki/Fast_Fourier_transform)_&nbsp;or FFT.&nbsp;There are a number of Arduino FFT libraries out there, but we keep finding ourselves returning to the venerable&nbsp;[ELM-ChaN ffft library](http://elm-chan.org/works/akilcd/report_e.html)&nbsp;for its speed and good looks.  
  
The FFT output still needs a bit of massaging to make for a good presentation on the limited 8x8 matrix. Several tables of scales and weights de-emphasize certain frequency ranges as they’re reduced to just eight columns. The software works at keeping the graph interesting, but some columns will always be less lively than others, especially comparing live speech against music of varying genres. If everything seems to stick toward one end of the graph, try another musician, musical genre, or different speakers.

# Tiny Arduino Music Visualizer

## Ideas

Our wiring diagrams illustrated this project with a half-size breadboard, but for a smaller package the parts can also&nbsp;fit on an Arduino breakout shield:

![](https://cdn-learn.adafruit.com/assets/assets/000/002/697/medium800/led_matrix_proto.jpg?1396785894)

Depending how you choose to orient the LED matrix, the matrix.setRotation() function can be used to keep the graph upright.

Just a concept — this is not functional — but it’s interesting&nbsp;to note that an Arduino Micro, the LED matrix and the microphone amplifier are almost perfectly matched to the dimensions of a 9 Volt battery:

![](https://cdn-learn.adafruit.com/assets/assets/000/002/698/medium800/led_matrix_idea.jpg?1396785897)

If you do attempt an extra-compact build like this, please keep in mind that the Arduino Micro includes a 5 Volt regulator, allowing it to be powered from a 9 Volt battery. Most small form-factor Arduino-compatible boards **do not include a regulator** ,&nbsp;and **will be damaged if you attempt to power them directly from 9 Volts!** For such boards, either add your own regulator to the circuit, or use an appropriate battery pack: three alkaline AA or AAA cells in series, or four rechargeable NiMH cells.  
  
Additionally, the code may need adjustments for some “alternative” Arduino boards…most likely the analog channel number, which may be mapped to different pin numbers on different boards.

Finally, because ADC registers are accessed directly, specific interrupts are used, and the FFT code is in AVR assembly language, this software **will not run** on upscale boards like the ESP32, Pico RP2040 or Teensy 3.0. It is strictly for “classic” Arduinos.


## Related Guides

- [Adafruit LED Backpacks](https://learn.adafruit.com/adafruit-led-backpack.md)
- [Schluff - The Sleep Monitor](https://learn.adafruit.com/schluff-the-oshw-sleep-monitor.md)
- [Circle of Fifths Euclidean Synth with synthio and CircuitPython](https://learn.adafruit.com/circle-of-fifths-euclidean-synth-with-synthio-and-circuitpython.md)
- [Working with Multiple Same Address I2C Devices](https://learn.adafruit.com/working-with-multiple-i2c-devices.md)
- [Animatronic Fizzgig MonsterM4sk Puppet](https://learn.adafruit.com/animatronic-fizzgig-monsterm4sk-puppet.md)
- [micro:bit Lesson 4. Sensing Light](https://learn.adafruit.com/micro-bit-lesson-4-sensing-light-and-temperature.md)
- [Boomy The Boombox](https://learn.adafruit.com/boomy-the-boombox.md)
- [Adafruit IO Home: Security ](https://learn.adafruit.com/adafruit-io-home-security.md)
- [MIDI for Makers](https://learn.adafruit.com/midi-for-makers.md)
- [reef-pi Guide 1: Setup and Demonstration](https://learn.adafruit.com/reef-pi-installation-and-configuration.md)
- [PyPortal IoT Weather Station](https://learn.adafruit.com/pyportal-iot-weather-station.md)
- [Adafruit IO Basics: Digital Input](https://learn.adafruit.com/adafruit-io-basics-digital-input.md)
- [Trellis Feather DSP-G1 Synthesizer](https://learn.adafruit.com/feather-trellis-dsp-g1-synthesizer.md)
- [CircuitPython on the Arduino Nano RP2040 Connect](https://learn.adafruit.com/circuitpython-on-the-arduino-nano-rp2040-connect.md)
- [MagicLight Bulb Color Mixer with Circuit Playground Bluefruit](https://learn.adafruit.com/magiclight-bulb-mixer.md)
- [Scrambled Number Security Keypad](https://learn.adafruit.com/scrambled-number-security-keypad.md)
