Make a CD-quality digital audio player
The popular ESP8266 Wi-Fi chip is no one-trick pony. Darren Yates uses one in a new high-fidelity 16-bit digital audio player good enough to make your phone think twice.
There’s no doubt the Raspberry Pi has been a force for good, renewing global interest in DIY electronics and generally making your own stuff. However, it’s important to remember the Pi isn’t the only tool in your maker’s toolkit. For instance, why spend $60 or so on a Raspberry Pi board to make your own music player, when you can do the same thing with a $5 microcontroller board instead? This month, we’re doing exactly that — building a simple digital audio player, but with audio quality that’ll give most phones plenty to think about. And in an unusual twist, it uses the ESP8266 Wi-Fi chip, plus the same basic audio tech you’ll find in smartphones and PC motherboards.
WI-FI CHIP PLAYING AUDIO?
The ESP8266 revolutionised the Internet of Things (IoT) when it hit the market a few years ago, commoditising Wi-Fi to the masses. However, look beyond the headline Wi-Fi functionality and this little microcontroller unit (MCU) is stacked with goodies. For its $5 online pricetag, you get a 32-bit microcontroller running at 80MHz, overclockable to 160MHz, analog-to-digital converter (ADC), plenty of digital I/O pins and up to 4MB of program storage. Alas, as good as it is, the ESP8266 doesn’t have any audio outputs to connect to your sound system or headphones, but its ‘Inter-Intergrated Sound’ interface gives you the next best thing.
I2S AND SPI
Inter-integrated sound, or ‘I2S’ for short, is a serial interface designed specifically for shifting around digital audio data — not the analog signal driving your headphones, but the pulse-code modulated (PCM) digital data stored on your flash drive or hard drive. The task of turning that data back into the analog voltage your ears have greater affinity with goes to a device called a ‘digital-to-analog converter’ or DAC. It outputs the analog voltage equivalent of the digital audio samples that can connect straight to a power amplifier and onto your earphones or speakers. I2S links the device’s main processor with the DAC. Think of it as a bit like USB for audio electronics.
While I2S takes care of sending the digital audio samples to the DAC, where do these samples come from? We’re using the popular WeMos D1 Mini MCU board that features the
ESP8266 chip and 4MB of flash storage — but 4MB won’t last long for highspeed digital audio. The simplest and most practical storage is to use a microSD card. Normally, highperformance computing devices will hook up to microSD cards using what’s called a ‘Secure Digital Input/Output’ (SDIO) interface, but unfortunately, few budget MCUs have one of these, and the ESP8266 MCU chip isn’t one of them. It does have a Serial Peripheral Interface (SPI) port, though — its not as fast as an SDIO port, but it’s a good alternative and fast enough for our needs.
The block diagram should give you a pretty good idea of how this all connects together. Your audio files are stored on a microSD card, which is powered by and connected to the WeMos D1 Mini MCU board via the SPI port connections. That streams the digital audio data into the ESP8266 chip, which then converts it on the fly into I2S format and forwards it onto the DAC. The DAC is also powered by the WeMos D1 Mini board, and since the WeMos D1 Mini is powered via its microUSB port, it means you just need a single USB power source. We control the player by a small momentary-on pushbutton switch connected to the MCU board’s D2 I/O pin.
The whole thing is relatively simple, but I2S gives the audio designer some significant advantages, and most of them are to do with audio quality. By using I2S, the DAC’s sensitive analog electronic circuitry can be kept away from digital interference generated by the MCU and this physical separation helps the DAC produce a quieter, cleaner audio output. The trick is you need an MCU with I2S support, and the ESP8266 is one of the few budget MCUs that does.
Many audio enthusiasts will swear by the big money they’ve spent on their dedicated DAC unit, either connected to their phone or audio source, for generating the highest-quality audio. Yet for all the money you can spend on one, every DAC unit starts with a dedicated DAC chip, and one of the best value options is Texas Instruments’ (formerly Burr-Brown’s) PCM5102A. It powers many DAC units you’ll find online, but you’ll also find it on a number of small DIY circuit-board modules on eBay for around $4. The version we’re using is called the ‘GY-PCM5102A’ and only requires six connections to our project. Some online forum posters will tell you the PCM5102A isn’t perfect (read up on ‘Gibbs Effect’ if you want a deep dive into the tech), but we reckon it’ll still outgun just about any smartphone, and here’s why. The PCM5102A supports sample rates up to an eye-watering 384kHz and plumbs bit depths to 32-bits. It has a solid signal-to-noise ratio (SNR) of 112dB and its total harmonic distortion (THD) is rated better than 0.003%. These are highclass specs, however, with our fairly crude and simple build, we can’t quite match them. Still, on my testing, we can achieve 92dB SNR, which is very respectable, particularly given we’re
using no shielded audio cabling whatsoever. Few, if any, smartphones can match those specs.
That said, the PCM5102A is just one of a bucketload of I2S DACs on the market — Cirrus Logic makes the popular CS4334 16-bit stereo DAC and Realtek makes an ALC series of I2S DAC chips. These are the same chipmakers that supply PC motherboard makers such as Gigabyte and ASUS. For example, Gigabyte’s top-end Z370 AORUS GAMING 7-OP motherboard features the ESS SABRE 9018 DAC chip — its main data interface is I2S. Some chips come with multiple interfaces, I2S is a long-term standard that’s easy to use.
BUILDING THE PROJECT
We’ve built the whole project on two small 170-point breadboards — these sell on eBay for around $1 or so each. You’ll see interlocks on the sides of each breadboard that allow you to join them together. However, despite the fact we’re using breadboards, the WeMos D1 Mini and GY-PCM5102A boards require soldering of their header pins to plug into the breadboards. If you don’t have soldering skills, now is a good time to get them or rope in a knowledgeable mate. Seriously, learning to solder really is a key skill in making your stuff. As for other components, you’ll need some ‘dupont’ hook-up wires and a two-pin momentary-on pushbutton switch — again, we bought ours from eBay as a pack of 10 for $1 or so. Use the overlay diagram and photo of ours as a guide to building yours.
HOOK-UPS AND POWER-UPS
Powering the player is straightforward — the D1 Mini board drives the whole show, so you just feed in power via microUSB cable. Even though the ESP8266 chip and your microSD card run off 3.3VDC and USB is 5VDC, the D1 Mini board includes a 3.3VDC voltage regulator to keep everything happy. Audio output comes via a standard 3.5mm TRRS (tip-ring-ringsleeve) socket. However, keep in mind, this is NOT a headphone socket. The player has no volume controls, so think of it as a component CD player and feed the output to a powered audio system with its own volume control. If you need one, you can buy a 3.5mm-to3.5mm stereo cable from any home entertainment store for under $10.
That takes care of the hardware, now for the software. You’ll find the source code required in a zip file on our website at www.apcmag.com/ magstuff. You’ll also need the Arduino integrated development environment (IDE), which you can grab from arduino.cc/downloads. Install it, launch it. Now from the top menu, select Tools, Board and Boards Manager from the top of the new list. Wait until the platforms index has loaded into the new Boards Manager window and type ‘ESP8266’ in the search bar. If not installed, install the latest version of the ‘esp8266 by ESP8266 Community’ board set. When installed, press Close.
Next, go back to the top menu, but this time select Sketch, Include Library, Manage Libraries. When the Library Manager window appears, wait for the library index to load, then at the top right, type ‘ESP8266WiFi’ into the search bar. Depending on your version of the IDE, this library should be built-in — but if you don’t see the green ‘INSTALLED’ indicator next to the library entry, click on that ESP8266WiFi entry and press the Install button. Finally, press the Close button and close the IDE.
Now unzip the source code pack from our website and copy the contents of the ‘libraries’ subfolder into the same subfolder of the IDE installation. The library we’re using here is the opensource ‘ESP8266Audio’ library by Earle Philhower III. Once you’ve copied that across, open up the IDE again, load in the ‘esp_ player_v1.ino’ file and plug your WeMos D1 Mini board into your PC via a suitable cable.
CODING THE ESP8266
To program the source code into the WeMos D1 Mini’s ESP8266 chip, we need to compile it into ESP8266-speak — that’s what the Arduino IDE will do. First, from the main menu, select Tools, Board, scroll down until you see
‘WeMos D1, R2 & Mini’ and select it. Next, go back to the Tools menu and ensure you set the following: Flash Size: “4M (1M SPIFFS)” Debug Port: Disabled IwIP Variant: v2 Lower Memory CPU Frequency: 80MHz Upload Speed: 512000 Erase Flash: Only Sketch Port: choose which ever is not COM1 When you’re ready, press the green right-arrow button top-left on the IDE and the code will automatically compile and upload to the ESP8266. You should see the blue LED on the board flash during upload.
The player handles WAV-format 44.1kHz/16-bit stereo audio files — the type you get from ripping audio CDs. Sure, MP3 and AAC files are more convenient, but if you’re serious about audio, uncompressed pulse-code modulated (PCM) WAV files are as good as you’ll get. The microSD card module will handle cards up to 32GB, which should be near enough to 60 CDs’ worth of 44.1kHz/16-bit stereo audio.
HOW THE BUTTON WORKS
To keep the build as simple as possible, we decided to have just a single button do everything we needed. As soon as power is applied, the player will begin playing the first file on the microSD card, provided a card is installed and FAT32-formatted. If you press the button once, it will pause the playback; press it again and it will restart. If you hold it down for more than a second and let go, it will automatically move to the next track. Completion of one audio track will automatically begin playback of the next and once the full play list is complete, playback stops.
The WeMos D1 Mini board has a number of add-on ‘shields’ to expand functionality and one of those boards is a microSD shield. So, you might be wondering why did we go and use a separate microSD board rather than just use the microSD shield instead? Unfortunately, the WeMos MicroSD shield board is hardwired to use I/O pin D4 for the ‘chip select’ function. However, that same pin is also hardwired in the ESP8266 chip as the I2S clock pin — since we can’t change this and changing the ‘CS’ pin on the microSD shield is well beyond the simple, we had to use a separate microSD card board that allows us to select our own CS pin.
The other limitation in this first version is that the audio files must exist in the root folder of the microSD card and they have to be in 8.3-character format. We hope to fix this shortly and get it to work with standard Windows filenames. We’ll let you know how we go.
You don’t have to operate the audio player using the single pushbutton. That button pulls I/O pin D2 low (digital-0) to activate it, but you can achieve the same thing by using a transistor driven via a 10kohm resistor by some other sensor — light, sound, temperature, take your pick. A common BC547 NPN transistor with a 10kohm resistor connected to its base would be ideal. We’ll leave how this works as design homework (although if there’s enough interest, we’ll cover how to do it in a future masterclass).
Once you’ve programmed the source code into the ESP8266 chip on the WeMos D1 Mini board, you can power it with almost anything you like. The total current consumption is approximately 40mA, so you could power it via a USB battery pack, solar panel setup, even your smartphone via USB-OTG cable — basically, anywhere you can get a clean 5VDC voltage with at least 50mA of current output should do nicely.
These 170-point breadboards can interlock together — we use two of them here. The WeMos D1 Mini is powered by the popular ESP8266 Wi-Fi chip.
Grab the latest updates of the ESP8266Audio library from GitHub.
Make sure to install the ESP8266 board library via the IDE’s Boards Manager.
To program the WeMos D1 Mini board, you’ll need the Arduino IDE.
The ESS 9018 DAC inside Gigabyte’s Z370 Aorus Gaming 7 motherboard uses I2S.
The block diagram of our digital audio player.
This tiny card module supports microSD cards up to 32GB. The GY-PCM5102A DAC board needs only six connections to our project.
Use the Library Manager to check the ESP8266WiFi library is installed.
Use this overlay diagram to build your own digital audio player.
Our digital audio player has audio quality approaching CD-levels.