This is a full tutorial for connecting up the PCM5102 or MAX98357A I2S decoder DAC’s. With the demo’s being how to use them for Internet Radio. It also includes a explanation of what I2S is and how it functions. I will build on this in future episodes which will show how to play MP3’s from SD card and add in an amplifier for the PCM5102. Note I talk about 16bit audio throughout, I am aware that both the chips featured support 24 and 32 bit sound. I have simplified for simplicity! Code referred to in video is at the bottom of this page.
Time codes, if you want to skip to a relevant part without watching everything.
00:53 I2S Explanation
05:37 What is Internet Radio?
06:34 Differences between MAX98357 & PCM5102
8:05 PCM5102 wiring
11:01 MAX98357A wiring
14:40 Libraries
18:31 PCM5102 Demo
20:15 MAX98375A mono demo
21:44 MAX98357A – how to do stereo
23:50 Round up waffle (inc. pleas to subscribe and like etc. Usual Desperation…)
Affiliate links for the items shown: Clicking these costs you the same price but gives me a small commission, please consider supporting this channel by using them 🙂
PCM5102 : https://amzn.to/3grijdT
(This has two units, you may only need one of these)
MAX98357A : https://amzn.to/3eovdrd
(You will need two of these for stereo sound)
ESP32 : https://amzn.to/2Xzhc3k
Breadboards: https://amzn.to/2THZTvy
Speakers : https://amzn.to/3gA2sdh
The Demo Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
#include "Arduino.h" #include "WiFi.h" #include "Audio.h" // Digital I/O used #define I2S_DOUT 25 // DIN connection #define I2S_BCLK 27 // Bit clock #define I2S_LRC 26 // Left Right Clock Audio audio; String ssid = "Your Wifi SSID"; String password = "Your Wifi Password"; void setup() { Serial.begin(115200); WiFi.disconnect(); WiFi.mode(WIFI_STA); WiFi.begin(ssid.c_str(), password.c_str()); while (WiFi.status() != WL_CONNECTED) delay(1500); audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT); audio.setVolume(15); // 0...21 // audio.connecttohost("http://www.wdr.de/wdrlive/media/einslive.m3u"); // audio.connecttohost("http://macslons-irish-pub-radio.com/media.asx"); // audio.connecttohost("http://mp3.ffh.de/radioffh/hqlivestream.aac"); // 128k aac // audio.connecttohost("http://mp3.ffh.de/radioffh/hqlivestream.mp3"); // 128k mp3 audio.connecttohost("http://vis.media-ice.musicradio.com/CapitalMP3"); // 128k mp3 // audio.connecttospeech("Wenn die Hunde schlafen, kann der Wolf gut Schafe stehlen.", "de"); // audio.connecttohost("http://media.ndr.de/download/podcasts/podcast4161/AU-20190404-0844-1700.mp3"); // podcast } void loop() { audio.loop(); } // optional void audio_info(const char *info){ Serial.print("info "); Serial.println(info); } void audio_id3data(const char *info){ //id3 metadata Serial.print("id3data ");Serial.println(info); } void audio_eof_mp3(const char *info){ //end of file Serial.print("eof_mp3 ");Serial.println(info); } void audio_showstation(const char *info){ Serial.print("station ");Serial.println(info); } void audio_showstreaminfo(const char *info){ Serial.print("streaminfo ");Serial.println(info); } void audio_showstreamtitle(const char *info){ Serial.print("streamtitle ");Serial.println(info); } void audio_bitrate(const char *info){ Serial.print("bitrate ");Serial.println(info); } void audio_commercial(const char *info){ //duration in sec Serial.print("commercial ");Serial.println(info); } void audio_icyurl(const char *info){ //homepage Serial.print("icyurl ");Serial.println(info); } void audio_lasthost(const char *info){ //stream URL played Serial.print("lasthost ");Serial.println(info); } void audio_eof_speech(const char *info){ Serial.print("eof_speech ");Serial.println(info); } |
This is an interesting project. Although I have built Internet radios around the ESP32 previously, I used the Espressif EDF software to upload to the ESP32. This project uses the Arduino IDE so it is a new way of doing things. I have several VS1053 boards lying around but have not tried the PCM5102 or MAX98357A boards. So I am tempted to have a try at constructing this project. Thanks for taking the time to publish it.
Thank you for taking the time to give this feedback. I’m going to be adding a touch screen and SD card soon to create a sort of “media centre” ,if that’s of any use.
I have a couple built on the VS1053, one in a stand alone mode using some cells recovered out of a macbook that I burned up with a cup of hot coffee, she is rather heavy but sounds great with a couple quality speakers and a small 3 watt amplifier. The other I have set up as a deck that can plug into any amplifier, both using displays and each with around 50 preset stations. I use a rotery encoder to change stations and for basic volumn as a sort of preamp for the little amp that drives the speakers. These are a lot more elaborate then the one built in this project, however the one in this project is very compact and can drive earphones with no problem, since I only want to listen to one station on it anyhow it works great for that. I would urge you to go for it. I have even had one I tied into a replica of a tabletop radio I got from my mother I simply hooked her in at the volumn control on the radio and she plays well, I have another little gizmo I built from a kit that does an FM transmitter, I have hooked one through that and played Christmas Music over it last Christmas for my wonderful wife who, at this very minute lay in hospital fighting a loosing battle with cancer.
Well I have my 3rd wifi radio up and running, this one is going to fit in an old transistor radio case that I got from my niece when my eldest sister died of cancer last year (damn cancer!). Some of the stuff is sort of missing since the video is no longer there but I did manage to find out the missing connections one to ground the other to vcc to make her work, then I inserted my favorite online station, a replay of the old AFRTS Armed Forces Radio and Television Service in Vietnam back in the late 60’s early 70’s, playing tunes I listened to on that station back when I was a young soldier serving our great nation over in that quaint little nation filled with very unfriendly little natives who wanted to kill us all. s5.voscast.com:8700 will get you there.
Apologies for that bad link, I’ve corrected it. That’s an interesting comment and thank you for the link to the station.
Thanks. I am still playing with this setup, I may well leave her as is as I love that station, it takes be back to that time when I was so damn young and knew I would live for ever, even when in a war zone with folks trying to kill me from time to time. Well I was right I guess, as I made it home. I am considering just putting it all in a tiny box with an earphone jack and making a single station out of her..
I was told to keep working on simple projects like this by my doctor, who is treating my wonderful wife and I both. She, because she is on her death bed as cancer eats her body, me as I try to hold it together on the brink of loosing her after 51 wonderful years together. So it is, I have made another step, I soldered both boards onto a strip board and wired it all direct so there are no wires to fall off. Now I am considering which small amplifier I want to run it through so I can hook it to speakers, or to just leave it as a earphone radio.
I was told to keep working on simple projects like this by my doctor, who is treating my wonderful wife and I both. She, because she is on her death bed as cancer eats her body, me as I try to hold it together on the brink of loosing her after 51 wonderful years together. So it is, I have made another step, I soldered both boards onto a strip board and wired it all direct so there are no wires to fall off. Now I am considering which small amplifier I want to run it through so I can hook it to speakers, or to just leave it as a earphone radio.
hi….
i.m looking since 2 years for an stupid internetradio for 1 station….
and now… i need 3 raspberry pi´s they work for a couple of weeks….than
they die—–i switch the light in the bathroom on….pi starts in 40 sek.
but if the powerconnection lost ( light off)….sometimes the software died on sd
and now
buy esp32…pcm 5102….look the video….soldering the esp…..flash the code….finish…starts in 3 seconds
thnx a lot from germany
Eberhard
Great stuff, yes the start up time for an ESP32 is far better than a Raspberry Pi. ESP32 is much more suited to this task.
Hi,
Can you tell me what version of #include “Audio.h” you are using as I always get the following error.
fatal error: DMAChannel.h: No such file or directory #include “DMAChannel.h”
Many thanks,
Paul
Version 2.0
Hi
I am trying use this on a esp 32 with board ESP Dev in Arduino IDE. Library ESP32-audioI2S-master. But it will not compile. I keep getting this error.
C:\Users\Al\Documents\Arduino\libraries\ESP32-audioI2S-master\src\Audio.cpp: In constructor ‘Audio::Audio(uint8_t, uint8_t, uint8_t)’:
C:\Users\Al\Documents\Arduino\libraries\ESP32-audioI2S-master\src\Audio.cpp:134:17: error: ‘class WiFiClientSecure’ has no member named ‘setInsecure’
clientsecure.setInsecure(); // if that can’t be resolved update to ESP32 Arduino version 1.0.5-rc05 or higher
can you see what I am doing wrong !
Thanks Al
Sounds like you are not up to date with a library somewhere. It suggest updating your Arduino IDE. Try that and see what happens.
Have the same problem. Additional message suggests updating to higher board version. This is the development version of the board which you can also get from the espressif git files. I took the highest (r.6) which allowed the program to compile ok but then I got load failures on my dev-kit-c wroom-32 which I have not yet resolved.
Further runtime problem causing reboots – solved by providing separate power for PCM5102A. Network connection issue – replaced String ssid and password with const char *ssid and *password and now works.
One question: is it normal for the output to slowly unscramble from random buzzing to a proper output over several seconds (even upto a minute)?
no, well mine doesn’t do that. I’m scratching my head as to what is wrong. Are there any unused connections left floating? If so check what they do and tie to positive or negative as required.
I just finished building it and its giving me the same error! Updated all libraries but no improvement. Board is ESP32 DEVKITV1.0. Your other sketches like PlayWavBetter works.
Working on new library as I write. Should solve many issues.
Further to our recent exchanges… I did try the MAX98357 today. Very nice, although a bit of a bugger about that Gain pin floating. I miss the 9dB and that’s not doing my OCD any favours:) So wacked it up to 15db with a 100k and there’s no distortion. Maybe because I’m only powering it at about 3.7V. Anyway you may remember I had snap-crackle-pop sounds with the PCM5102 I used on my retro radio conversion https://www.hackster.io/pnjorge/retroradio-wifi-pilot-05b075 when playing short audio announcements. Well I had the same with this MAX chip too. After a bit of fiddling I found a workaround. I’m using the SD shutdown pin to only unmute the audio just after I start the mp3 and mute it again just before it finishes. I include the track lengths into a config json file I’m using… maybe there’s a more elegant way (by way of reading the mp3 file info data perhaps. Anyway, it’s done and we’re cooking on gas:)
Thanks for that, I don’t remember any snap-crackle-pop but must have been there and if present on both devices it sounds like an issue with either the library or ESP32 libraries for supporting I2S. I’ll bare this in mind if I come across it. (Currently writing my own library). The Gain pin thing is weird, the docs say it can be left floating, but if you do the gain jumps about all other the place. I like the 15 Gain option – Max Volume!