Capture stereo sound with Arduino Nano
How do you capture two audio channels with just one analogto-digital converter? Darren Yates explains how with this new project using the popular Arduino Nano.
If there is one thing Arduino boards have never really been designed to do, it’s record digital audio. So over the last couple of years, we’ve taken, first, an Arduino Uno and then an Arduino Mega and done exactly that. But those designs could only capture one audio channel. This month, we fix that by turning the tiny Arduino Nano into a stereo digital audio recorder.
ONE GETS YOU TWO?
Even if you don’t know anything about the original 8-bit Arduino boards, the key things to remember are: a) they’re not very fast; b) they don’t have much on-board storage; and c) they only have one analog-todigital converter (ADC).
We don’t have space here for a full rundown, but just quickly, an ADC turns analog voltage (of the type you get from audio) into digital values. However, you usually have one ADC per audio channel, so if you want to record stereo, you’d have two ADCs. Problem is, the Arduino Nano only has the one.
CHANNEL MULTIPLEXING
We could use two Nano boards and somehow synchronise them together, but the solution we’re using here needs just the one board. The trick is provided by the Arduino Nano itself and it’s the built-in analog input multiplexer. A multiplexer is a bit like an electronic set of traffic lights that switches between different lanes onto a highway. Traffic can flow from each lane when the light for that lane goes green. The Nano has eight analog inputs that can each be switched to the ADC — and we take advantage of that to create our stereo sound sample.
TIMER INTERRUPTS
The key to any digital audio recorder is sampling the audio at regular, fixed time periods. The Nano gives us these fixed time periods through its on-board ‘ timers’. We set up a timer to trigger an ‘interrupt’ every 41.66 microseconds — this interrupt is an electronic red flag that diverts the current code operation to an ‘interrupt service routine’ (ISR) that we specify — code that grabs the audio samples.
We do that by setting the analog multiplexer first to input A3, grabbing the left-channel sample and temporarily storing it, then switch the multiplexer to input A4 and grab the right-channel sample. During normal operation, the Nano takes these samples and writes them to the microSD card reader module we’ve also added. The ’41.66 microseconds’ is important — it gives us a sample frequency of 24kHz, which is a valid (although lesser-known) WAVformat rate. The reason for using 24kHz is two-fold. First, the Arduino simply can’t grab stereo samples any faster than that — if we try, we start dropping samples all over the place. Second, with only 2KB of RAM, we’ve only space for 1KB of audio buffer and the faster the sample rate, the less recording time that buffer holds.
Before recording begins, we create a WAV file header that outlines to media players how the file works. We then store the samples and update the header at the end.
WHY A BUFFER?
If you’re wondering why we don’t just save the samples straight to the microSD card, we would dearly love to — but the problem is that these cards have what’s called a ‘ latency’ or delay period. Writing data to a flash card requires the card to organise its storage, and each time you write, there’s a small delay while it finds the appropriate spot in that storage. Unfortunately, the delay can vary from as little as 10 microseconds up to half a second. In professional audio recorders, sufficient RAM is included to cover this delay, acting as temporary
storage. But with only 2KB on the Nano, there’s only so much we can do. What’s more, for this to work, you have to use a SanDisk Ultra microSD card. We’ve tested a number of cards and the SanDisk Ultra had the lowest overall latency — you can pick up an 8GB card for around $7.
The buffer provides somewhere to store samples every 41.6 microseconds and with 1KB of storage, we can store 512 samples from both channels. At a sample rate of 24kHz, 512 samples takes just 21.3 milliseconds. The SanDisk Ultra card we purchased has a latency of around 15 milliseconds, worst-case. Since the buffer can store more samples than the time it takes the SanDisk card to write them, we shouldn’t lose any samples. And from our testing, we don’t. This isn’t to say other microSD cards won’t work — but it comes down to their latency times, not necessarily write speeds.
BUILDING THE PROJECT
We’ve built the ‘NanoRecorder’ on a small 400-point breadboard from parts purchased on eBay — use the overlay diagram as your guide. The resistors bias the ADC input to half the supply rail (2.5VDC) to maximise signal input range before any clipping distortion occurs. You can grab the source code from www.apcmag.com/magstuff, unzip it, copy the contents of the libraries
subfolder into the same of your Arduino IDE, load up the nanoRec_v1.ino source code and flash it to the Arduino Nano board. Get the Arduino IDE from arduino.cc/downloads.
To operate the recorder, first install your FAT32-formatted card into the card module and power up. If you don’t, the LED will flash indefinitely, requiring a reboot of the Nano. Plug your line-level audio source into the 3.5mm stereo socket, press the button and the LED will flash four times to indicate recording as begun. To stop recording, press the button again — this will initiate a fast 10-cycle LED flash. Take your microSD card, load it into your PC, fire up Windows Media Player, select the file (WMP happily plays 24kHz WAV files) and enjoy.
SOUND QUALITY?
Despite the single ADC, you should hear excellent channel separation, made better by the small delay between left- and right-channel samples as the multiplexer switches between the two. We’ve measured the distortion at approximately 0.4% at 1kHz full-scale (-0.5dBFS) and the signal-to-noise ratio (SNR) is around 40dB. With a 24kHz sample rate, Harry Nyquist says that gives us a frequency response to 12kHz. Given this is only 8-bit audio, those figures are about as good as you can get. The 10uF capacitor on the Nano’s Vref pin filters the reference voltage to the ADC, which plays a big role in keeping the SNR and distortion tight.
This isn’t the recorder to use for classic music and it’s not CD-quality, but for recording voice, rock, pop or any similarly reduced-dynamicrange audio input, you might be surprised at how good this little recorder sounds.