Make a CD-qual­ity dig­i­tal au­dio player

The pop­u­lar ESP8266 Wi-Fi chip is no one-trick pony. Dar­ren Yates uses one in a new high-fi­delity 16-bit dig­i­tal au­dio player good enough to make your phone think twice.

APC Australia - - Contents -

There’s no doubt the Rasp­berry Pi has been a force for good, re­new­ing global in­ter­est in DIY elec­tron­ics and gen­er­ally mak­ing your own stuff. How­ever, it’s im­por­tant to re­mem­ber the Pi isn’t the only tool in your maker’s tool­kit. For in­stance, why spend $60 or so on a Rasp­berry Pi board to make your own mu­sic player, when you can do the same thing with a $5 mi­cro­con­troller board in­stead? This month, we’re do­ing ex­actly that — build­ing a sim­ple dig­i­tal au­dio player, but with au­dio qual­ity that’ll give most phones plenty to think about. And in an un­usual twist, it uses the ESP8266 Wi-Fi chip, plus the same ba­sic au­dio tech you’ll find in smart­phones and PC moth­er­boards.


The ESP8266 rev­o­lu­tionised the In­ter­net of Things (IoT) when it hit the mar­ket a few years ago, com­modi­tis­ing Wi-Fi to the masses. How­ever, look beyond the head­line Wi-Fi func­tion­al­ity and this lit­tle mi­cro­con­troller unit (MCU) is stacked with good­ies. For its $5 on­line pric­etag, you get a 32-bit mi­cro­con­troller run­ning at 80MHz, over­clock­able to 160MHz, ana­log-to-dig­i­tal con­verter (ADC), plenty of dig­i­tal I/O pins and up to 4MB of pro­gram stor­age. Alas, as good as it is, the ESP8266 doesn’t have any au­dio out­puts to con­nect to your sound sys­tem or head­phones, but its ‘In­ter-In­ter­grated Sound’ in­ter­face gives you the next best thing.


In­ter-in­te­grated sound, or ‘I2S’ for short, is a se­rial in­ter­face de­signed specif­i­cally for shift­ing around dig­i­tal au­dio data — not the ana­log sig­nal driv­ing your head­phones, but the pulse-code mod­u­lated (PCM) dig­i­tal data stored on your flash drive or hard drive. The task of turn­ing that data back into the ana­log volt­age your ears have greater affin­ity with goes to a de­vice called a ‘dig­i­tal-to-ana­log con­verter’ or DAC. It out­puts the ana­log volt­age equiv­a­lent of the dig­i­tal au­dio sam­ples that can con­nect straight to a power am­pli­fier and onto your ear­phones or speak­ers. I2S links the de­vice’s main pro­ces­sor with the DAC. Think of it as a bit like USB for au­dio elec­tron­ics.

While I2S takes care of send­ing the dig­i­tal au­dio sam­ples to the DAC, where do th­ese sam­ples come from? We’re us­ing the pop­u­lar WeMos D1 Mini MCU board that fea­tures the

ESP8266 chip and 4MB of flash stor­age — but 4MB won’t last long for high­speed dig­i­tal au­dio. The sim­plest and most prac­ti­cal stor­age is to use a mi­croSD card. Nor­mally, high­per­for­mance com­put­ing de­vices will hook up to mi­croSD cards us­ing what’s called a ‘Se­cure Dig­i­tal In­put/Out­put’ (SDIO) in­ter­face, but un­for­tu­nately, few bud­get MCUs have one of th­ese, and the ESP8266 MCU chip isn’t one of them. It does have a Se­rial Pe­riph­eral In­ter­face (SPI) port, though ­— its not as fast as an SDIO port, but it’s a good al­ter­na­tive and fast enough for our needs.

The block di­a­gram should give you a pretty good idea of how this all con­nects to­gether. Your au­dio files are stored on a mi­croSD card, which is pow­ered by and con­nected to the WeMos D1 Mini MCU board via the SPI port con­nec­tions. That streams the dig­i­tal au­dio data into the ESP8266 chip, which then con­verts it on the fly into I2S for­mat and for­wards it onto the DAC. The DAC is also pow­ered by the WeMos D1 Mini board, and since the WeMos D1 Mini is pow­ered via its mi­croUSB port, it means you just need a sin­gle USB power source. We con­trol the player by a small mo­men­tary-on push­but­ton switch con­nected to the MCU board’s D2 I/O pin.

The whole thing is rel­a­tively sim­ple, but I2S gives the au­dio de­signer some sig­nif­i­cant ad­van­tages, and most of them are to do with au­dio qual­ity. By us­ing I2S, the DAC’s sen­si­tive ana­log elec­tronic cir­cuitry can be kept away from dig­i­tal in­ter­fer­ence gen­er­ated by the MCU and this phys­i­cal sep­a­ra­tion helps the DAC pro­duce a qui­eter, cleaner au­dio out­put. The trick is you need an MCU with I2S sup­port, and the ESP8266 is one of the few bud­get MCUs that does.


Many au­dio en­thu­si­asts will swear by the big money they’ve spent on their ded­i­cated DAC unit, ei­ther con­nected to their phone or au­dio source, for gen­er­at­ing the high­est-qual­ity au­dio. Yet for all the money you can spend on one, ev­ery DAC unit starts with a ded­i­cated DAC chip, and one of the best value op­tions is Texas In­stru­ments’ (for­merly Burr-Brown’s) PCM5102A. It pow­ers many DAC units you’ll find on­line, but you’ll also find it on a num­ber of small DIY cir­cuit-board mod­ules on eBay for around $4. The ver­sion we’re us­ing is called the ‘GY-PCM5102A’ and only re­quires six con­nec­tions to our project. Some on­line forum posters will tell you the PCM5102A isn’t per­fect (read up on ‘Gibbs Ef­fect’ if you want a deep dive into the tech), but we reckon it’ll still out­gun just about any smart­phone, and here’s why. The PCM5102A sup­ports sam­ple rates up to an eye-wa­ter­ing 384kHz and plumbs bit depths to 32-bits. It has a solid sig­nal-to-noise ra­tio (SNR) of 112dB and its to­tal har­monic dis­tor­tion (THD) is rated bet­ter than 0.003%. Th­ese are high­class specs, how­ever, with our fairly crude and sim­ple build, we can’t quite match them. Still, on my test­ing, we can achieve 92dB SNR, which is very re­spectable, par­tic­u­larly given we’re

us­ing no shielded au­dio ca­bling what­so­ever. Few, if any, smart­phones can match those specs.

That said, the PCM5102A is just one of a buck­et­load of I2S DACs on the mar­ket — Cir­rus Logic makes the pop­u­lar CS4334 16-bit stereo DAC and Real­tek makes an ALC se­ries of I2S DAC chips. Th­ese are the same chip­mak­ers that sup­ply PC mother­board mak­ers such as Gi­ga­byte and ASUS. For ex­am­ple, Gi­ga­byte’s top-end Z370 AORUS GAM­ING 7-OP mother­board fea­tures the ESS SABRE 9018 DAC chip — its main data in­ter­face is I2S. Some chips come with mul­ti­ple in­ter­faces, I2S is a long-term stan­dard that’s easy to use.


We’ve built the whole project on two small 170-point bread­boards — th­ese sell on eBay for around $1 or so each. You’ll see in­ter­locks on the sides of each bread­board that al­low you to join them to­gether. How­ever, de­spite the fact we’re us­ing bread­boards, the WeMos D1 Mini and GY-PCM5102A boards re­quire sol­der­ing of their header pins to plug into the bread­boards. If you don’t have sol­der­ing skills, now is a good time to get them or rope in a knowl­edge­able mate. Se­ri­ously, learn­ing to sol­der re­ally is a key skill in mak­ing your stuff. As for other com­po­nents, you’ll need some ‘dupont’ hook-up wires and a two-pin mo­men­tary-on push­but­ton switch — again, we bought ours from eBay as a pack of 10 for $1 or so. Use the over­lay di­a­gram and photo of ours as a guide to build­ing yours.


Pow­er­ing the player is straight­for­ward — the D1 Mini board drives the whole show, so you just feed in power via mi­croUSB cable. Even though the ESP8266 chip and your mi­croSD card run off 3.3VDC and USB is 5VDC, the D1 Mini board in­cludes a 3.3VDC volt­age reg­u­la­tor to keep ev­ery­thing happy. Au­dio out­put comes via a stan­dard 3.5mm TRRS (tip-ring-ringsleeve) socket. How­ever, keep in mind, this is NOT a head­phone socket. The player has no vol­ume con­trols, so think of it as a com­po­nent CD player and feed the out­put to a pow­ered au­dio sys­tem with its own vol­ume con­trol. If you need one, you can buy a 3.5mm-to3.5mm stereo cable from any home en­ter­tain­ment store for un­der $10.


That takes care of the hard­ware, now for the soft­ware. You’ll find the source code re­quired in a zip file on our web­site at www.apc­ magstuff. You’ll also need the Ar­duino in­te­grated development en­vi­ron­ment (IDE), which you can grab from ar­­loads. In­stall it, launch it. Now from the top menu, select Tools, Board and Boards Man­ager from the top of the new list. Wait un­til the plat­forms in­dex has loaded into the new Boards Man­ager win­dow and type ‘ESP8266’ in the search bar. If not in­stalled, in­stall the lat­est ver­sion of the ‘esp8266 by ESP8266 Com­mu­nity’ board set. When in­stalled, press Close.

Next, go back to the top menu, but this time select Sketch, In­clude Li­brary, Man­age Li­braries. When the Li­brary Man­ager win­dow ap­pears, wait for the li­brary in­dex to load, then at the top right, type ‘ESP8266WiFi’ into the search bar. Depend­ing on your ver­sion of the IDE, this li­brary should be built-in — but if you don’t see the green ‘IN­STALLED’ in­di­ca­tor next to the li­brary en­try, click on that ESP8266WiFi en­try and press the In­stall but­ton. Fi­nally, press the Close but­ton and close the IDE.

Now un­zip the source code pack from our web­site and copy the con­tents of the ‘li­braries’ sub­folder into the same sub­folder of the IDE in­stal­la­tion. The li­brary we’re us­ing here is the open­source ‘ESP8266Au­dio’ li­brary by Earle Phil­hower III. Once you’ve copied that across, open up the IDE again, load in the ‘esp_ play­er_v1.ino’ file and plug your WeMos D1 Mini board into your PC via a suit­able cable.


To pro­gram the source code into the WeMos D1 Mini’s ESP8266 chip, we need to com­pile it into ESP8266-speak — that’s what the Ar­duino IDE will do. First, from the main menu, select Tools, Board, scroll down un­til you see

‘WeMos D1, R2 & Mini’ and select it. Next, go back to the Tools menu and en­sure you set the fol­low­ing: Flash Size: “4M (1M SPIFFS)” De­bug Port: Dis­abled IwIP Vari­ant: v2 Lower Mem­ory CPU Fre­quency: 80MHz Up­load Speed: 512000 Erase Flash: Only Sketch Port: choose which ever is not COM1 When you’re ready, press the green right-ar­row but­ton top-left on the IDE and the code will au­to­mat­i­cally com­pile and up­load to the ESP8266. You should see the blue LED on the board flash dur­ing up­load.


The player han­dles WAV-for­mat 44.1kHz/16-bit stereo au­dio files — the type you get from rip­ping au­dio CDs. Sure, MP3 and AAC files are more con­ve­nient, but if you’re se­ri­ous about au­dio, un­com­pressed pulse-code mod­u­lated (PCM) WAV files are as good as you’ll get. The mi­croSD card mod­ule will han­dle cards up to 32GB, which should be near enough to 60 CDs’ worth of 44.1kHz/16-bit stereo au­dio.


To keep the build as sim­ple as pos­si­ble, we de­cided to have just a sin­gle but­ton do ev­ery­thing we needed. As soon as power is ap­plied, the player will be­gin play­ing the first file on the mi­croSD card, pro­vided a card is in­stalled and FAT32-for­mat­ted. If you press the but­ton once, it will pause the play­back; press it again and it will restart. If you hold it down for more than a sec­ond and let go, it will au­to­mat­i­cally move to the next track. Com­ple­tion of one au­dio track will au­to­mat­i­cally be­gin play­back of the next and once the full play list is com­plete, play­back stops.


The WeMos D1 Mini board has a num­ber of add-on ‘shields’ to ex­pand func­tion­al­ity and one of those boards is a mi­croSD shield. So, you might be won­der­ing why did we go and use a sep­a­rate mi­croSD board rather than just use the mi­croSD shield in­stead? Un­for­tu­nately, the WeMos Mi­croSD shield board is hard­wired to use I/O pin D4 for the ‘chip select’ func­tion. How­ever, that same pin is also hard­wired in the ESP8266 chip as the I2S clock pin — since we can’t change this and chang­ing the ‘CS’ pin on the mi­croSD shield is well beyond the sim­ple, we had to use a sep­a­rate mi­croSD card board that al­lows us to select our own CS pin.

The other lim­i­ta­tion in this first ver­sion is that the au­dio files must ex­ist in the root folder of the mi­croSD card and they have to be in 8.3-char­ac­ter for­mat. We hope to fix this shortly and get it to work with stan­dard Win­dows file­names. We’ll let you know how we go.


You don’t have to op­er­ate the au­dio player us­ing the sin­gle push­but­ton. That but­ton pulls I/O pin D2 low (dig­i­tal-0) to ac­ti­vate it, but you can achieve the same thing by us­ing a tran­sis­tor driven via a 10kohm re­sis­tor by some other sen­sor — light, sound, tem­per­a­ture, take your pick. A com­mon BC547 NPN tran­sis­tor with a 10kohm re­sis­tor con­nected to its base would be ideal. We’ll leave how this works as de­sign home­work (although if there’s enough in­ter­est, we’ll cover how to do it in a fu­ture mas­ter­class).

Once you’ve pro­grammed the source code into the ESP8266 chip on the WeMos D1 Mini board, you can power it with al­most any­thing you like. The to­tal cur­rent con­sump­tion is ap­prox­i­mately 40mA, so you could power it via a USB bat­tery pack, so­lar panel setup, even your smart­phone via USB-OTG cable — ba­si­cally, any­where you can get a clean 5VDC volt­age with at least 50mA of cur­rent out­put should do nicely.

Th­ese 170-point bread­boards can in­ter­lock to­gether — we use two of them here. The WeMos D1 Mini is pow­ered by the pop­u­lar ESP8266 Wi-Fi chip.

Grab the lat­est up­dates of the ESP8266Au­dio li­brary from GitHub.

Make sure to in­stall the ESP8266 board li­brary via the IDE’s Boards Man­ager.

To pro­gram the WeMos D1 Mini board, you’ll need the Ar­duino IDE.

The ESS 9018 DAC in­side Gi­ga­byte’s Z370 Aorus Gam­ing 7 mother­board uses I2S.

The block di­a­gram of our dig­i­tal au­dio player.

This tiny card mod­ule sup­ports mi­croSD cards up to 32GB. The GY-PCM5102A DAC board needs only six con­nec­tions to our project.

Use the Li­brary Man­ager to check the ESP8266WiFi li­brary is in­stalled.

Use this over­lay di­a­gram to build your own dig­i­tal au­dio player.

Our dig­i­tal au­dio player has au­dio qual­ity ap­proach­ing CD-lev­els.

Newspapers in English

Newspapers from Australia

© PressReader. All rights reserved.