APC Australia

Make an Arduino-powered audio light show

Turn your favourite music into your own personal light show with an Arduino Nano and a 12-bit RGB LED ring. Darren Yates explains how.

-

The clever and popular WS2812B RGB LED contains tiny red, green and blue LEDs in each module. It’s popular and clever because you can string up to 300 of them together and control each individual­ly with just one wire. We introduced the 12-bit RGB ring in our January issue (APC 450, page 104). This time, we’re feeding audio into the Arduino Nano’s analog-to-digital converter (ADC) to turn the RGB ring into an audio-controlled light show.

PEAK-POSITION METER

We’re building what’s called a ‘peak position meter’. We use the ADC to capture audio, we analyse it, looking for the peak amplitude over 255 consecutiv­e audio samples. The 12-bit ring is divided into two vertical halves, starting at the bottom, with LEDs on each side lighting up sequential­ly in proportion to the peak signal value. On the initial rising progressio­n, the LEDs light up green, but as the peak amplitude hits the top LED and continues rising, the LED colour changes to red and the progressio­n continues back down. Or to put it simply, as the signal amplitude goes up, the LEDs light up in green, then back down again in red.

SIMPLE AUDIO SAMPLING

The ADC inside the Arduino Nano’s ATMEGA328P microcontr­oller chip is an incredibly useful feature. However, rather than using some of our overclocki­ng tricks to boost the sample rate, this project works happily just using the standard analogRead() function. This gives us a sample rate of approximat­ely 9.8kHz and an audio bandwidth of just under 5kHz.

The reason we look for the peak of 255 consecutiv­e samples is so that we get a consistent display. If we didn’t, one sample could light up all LEDs, the next sample none, depending on the point of the audio waveform we sampled. There’s no magic behind taking 255 samples, other than it gives a good mix of smoothness and display update speed that looks good. There are four resistors in this build and it’s worth understand­ing why they’re there. Analog audio takes the form of alternatin­g-current or ‘AC’ voltage. However, the ADC is expecting direct-current (DC) voltage referenced between 0 and 5VDC. If we just connect the analog audio straight to the ADC input, it will only see the upper or ‘positive’-half of the waveform and won’t be able to capture proper readings. To fix this, we use two 10kohm resistors in series (end-to-end) to create a voltage divider — with one end of the two resistors connected to ground (0V) and the other to 5VDC, the junction of the two sits at half the 5VDC voltage, that is, 2.5VDC. We connect this junction to the ADC input to set or ‘bias’ it. Now we can feed in the audio input via a capacitor connected to this same junction point and the audio signal will make the ADC input wobble up and down around this 2.5VDC point.

The ADC’s job is to convert the DC value of the voltage into a 10-bit number that’s proportion­al to it. If the DC voltage is 0V, the ADC records zero. If the DC voltage is 5VDC, it records ‘1023’ (two to the ten minus one). If we set or ‘bias’ the ADC input to 2.5VDC, the steady state reading from the ADC will be 512, since that’s halfway to 1024 (in practice, it’ll vary slightly on the resistor value tolerances). If we subtract 512 from the ADC reading and take the absolute value, we now turn each sample into a positive number from 0 to 512 and use these to determine the sequence in which the WS2812B LEDs in the ring light up.

BUILDING THE PROJECT

First up, you’ll need the source code for this project and you’ll find it on the website at www.apcmag.com/magstuff. We’ve built our Audio Light Show on a tiny 170-tiepoint breadboard. Because the Nano board doesn’t have a convenient link to 5VDC and GND near the A1 analog input pin, we cheat — we set digital output D13 ‘high’ to create a DC voltage of (nominally) 5VDC and we turn analog input A5 into a digital output, push it ‘low’ and get our pseudoGND rail. The current flowing through these resistors is no more than 1mA, which is much less than these I/O pins can handle, so everything is sweet. The 12-bit LED ring requires soldering of four header pins to slot into the breadboard.

GETTING THE AUDIO

You can take the audio from the headphone socket of your PC, phone or tablet. We use a standard 3.5mm TRRS (tip-ring-ring-sleeve) socket to tap the right and left-channel outputs and mix them together using two 330-ohm resistors. The junction of these two resistors gives us a single or monochanne­l mix of the stereo audio. We connect this to the capacitor, which connects to the analog input A1. Follow the wiring diagram and check against it before you plug in power or flash the Arduino board.

SETTING THE RING BRIGHTNESS

We’re using the excellent Adafruit NeoPixel library for this project and we’ve annotated in the source code where you set the LED brightness. Basically, you use the library command: strip.setBrightn­ess(num); ...where ‘num’ is a number between 0 (off) and 255 (max brightness). Just be aware this LED ring is seriously bright when you crank it up, using up to 720mA of current with all RGB LEDs full-on. We’ve set a conservati­ve value of ‘16’ in the code to start you off.

LOADING THE SOURCE CODE

If you haven’t as yet, download the latest Arduino IDE from arduino.cc/ downloads. Launch it and from the top menu, select ‘Sketch > Include Library > Manage Libraries’. When the Library Manager appears, search for ‘Adafruit NeoPixel’, click on the entry and press the Install button. When you’re done, grab our source code, unzip it, plug in your Arduino Nano board into your PC, load up the AudioLight­Show.ino file, select the Nano board from the tools/ board menu option, select the COM port (not COM1), then flash the source code to the board. Plug in an audio source and the LED ring should start dancing.

 ??  ?? LEDs light green going up and red back down as audio signal increases.
LEDs light green going up and red back down as audio signal increases.
 ??  ?? Our Audio Light Show uses an Arduino Nano and 12-bit RGB LED ring. This 12-bit RGB LED ring sells on eBay for $2, but requires soldering skills.
Our Audio Light Show uses an Arduino Nano and 12-bit RGB LED ring. This 12-bit RGB LED ring sells on eBay for $2, but requires soldering skills.
 ??  ?? A 3.5mm stereo headphone splitter will be useful for this project.
A 3.5mm stereo headphone splitter will be useful for this project.
 ??  ?? Use this overlay wiring diagram to build your own version.
Use this overlay wiring diagram to build your own version.
 ??  ?? Install Adafruit’s NeoPixel library using the Arduino IDE’s library.
Install Adafruit’s NeoPixel library using the Arduino IDE’s library.

Newspapers in English

Newspapers from Australia