Internet radio ESP32 and PCM5102

Place code snippets and demo code here
User avatar
Fernando Perez
Posts: 106
Joined: Mon Feb 15, 2021 10:09 pm
Location: Santander (Spain)
Has thanked: 55 times
Been thanked: 54 times

Internet radio ESP32 and PCM5102

Post by Fernando Perez »

A simple internet radio example with ESP32 and a pcm5102a I2S DAC module.
image.png
I have powered the DAC at 5 volts instead of 3.3 and bonded the SCK terminal to ground instead of leaving it open, to avoid instabilities.
image.png

Code: [Local Link Removed for Guests]

DIM url$(5) ' internet broadcasting stations
url$(0) = "http://listen.shoutcast.com/thegreatamericansongbook" 'American Song
url$(1) = "http://62.27.87.143/crtve/rnerc/main/mp3/high" ' Radio nacional clasica
url$(2) = "http://37.252.124.170:8010/adagioradio.mp3"
url$(3) = "http://23.106.34.27:24929/stream" ' The UK 1940s Radio Station Server
url$(4) = "http://64.40.99.76:8000/" ' Beatles Radio
url$(5) = "http://192.96.201.2:8000/;?icy=http" ' Italian 60_70

emi$ = "American,Clasica,Adagio,Los_40,Beatles,Italian" ' for listBox options
sel$ = ""
info$ = "" ' for streaming metadata
vol = 70 : vol$ = str$(vol) + "%"
status$ = "OFF"
htmlEventVar$ = ""

onplay laut        ' when receiving streaming metadata
onHtmlReload web   ' when the web page reloads
onHtmlChange sel   ' when the web page changes

gosub web
gosub sel

wait ' wait for some event
END

' ---------------------------------------------------------------------------------------------------
web:
 cls
 cssexternal "/style.css"
 a$ = ""
 a$ = |<center><h2 style="margin-top:25px;font-family:Tahoma;font-size:1.8rem;">Streaming Radio</h2>|
 a$ = a$ + |Station:| + listBox$(sel$, emi$, "box")
 a$ = a$ + textArea$(info$,"info")
 a$ = a$ + |Volume: | + textBox$(vol$,"porcen")
 a$ = a$ + slider$(vol, 0, 100, 5, "vol")
 a$ = a$ + |Right now i'm: &nbsp; &nbsp;|
 if status$ = "ON" then a$ = a$ + button$("ON",btn,"btn") else a$ = a$ + button$("OFF",btn,"btn")
 a$ = replace$(a$, "Choose here", "Select:")
 html a$
return

' ---------------------------------------------------------------------------------------------------
laut:
 info$ = play.message$
 info$=replace$(info$,"SiteName=", "Station: ")
 info$=replace$(info$, "Genre=", "")
 info$=replace$(info$, "Bitrate=", "Kbps: ")
 info$=replace$(info$, "StreamTitle=", "")
 refresh
return

' ---------------------------------------------------------------------------------------------------
btn:

if status$ = "ON" then
  play.stop
  info$ = ""
  status$ = "OFF"
  gosub web
end if

return

' ---------------------------------------------------------------------------------------------------
sel:

if htmlEventVar$ = "vol" then
  vol$ = str$(vol) + "%"
  play.volume vol
  refresh
endif

if htmlEventVar$ = "sel$" then
  index = WORD.FIND(emi$, sel$, ",")
  play.setup 1, 64
  play.stream url$(index-1), 15000
  status$ = "ON"
  gosub web
endif

return
And the style.css file:

Code: [Local Link Removed for Guests]

#box {
  font-size: 1rem;
  margin-left: 10px;
  width: 200px;
}

#porcen {
  font-size: 1.5rem;
  text-align: center;
  width: 15%;
  border:none;
}

#vol {
  display: block;
  width: 75%;
  max-width: 500px;
}

#info {
  display: block;
  font-size: 0.9rem;
  width: 75%;
  max-width: 500px;
  height: 25%;
  margin: 30px auto;
  padding: 12px 26px;
  background-color: yellow;
  border: 1px solid #132338;
  border-radius: 34px;
  box-shadow: 2px 2px 2px #505F69;
}

#btn {
  font-family: 'trebuchet ms';
  font-weight: bold;
  font-size:18px;
  margin: 40px auto;
  padding: 12px 26px;
  border: 1px solid #132338;
  border-radius:34px;
  box-shadow: 2px 2px 2px #505F69;
  color:#14396A !important;
  background: #EEE9E4;
  background: linear-gradient(to top,#EEE9E4,#B1CF7C);
}
image.png
image.png
You do not have the required permissions to view the files attached to this post.
User avatar
PeterN
Posts: 97
Joined: Mon Feb 08, 2021 7:56 pm
Location: Krefeld, Germany
Has thanked: 41 times
Been thanked: 45 times
Contact:

Re: Internet radio ESP32 and PCM1502

Post by PeterN »

I like your very elegantly programmed webinterface!
User avatar
Fernando Perez
Posts: 106
Joined: Mon Feb 15, 2021 10:09 pm
Location: Santander (Spain)
Has thanked: 55 times
Been thanked: 54 times

Re: Internet radio ESP32 and PCM5102

Post by Fernando Perez »

I appreciate it, Peter. I highly value his opinion.
cicciocb
Site Admin
Posts: 309
Joined: Mon Feb 03, 2020 1:15 pm
Location: Toulouse
Has thanked: 85 times
Been thanked: 197 times
Contact:

Re: Internet radio ESP32 and PCM5102

Post by cicciocb »

Thanks Fernando,
a very nice and clever project.
User avatar
Dompie
Posts: 2
Joined: Tue Feb 09, 2021 12:59 pm
Has thanked: 12 times

Re: Internet radio ESP32 and PCM5102

Post by Dompie »

Wauwwww nice!!! Thank you
ebs
Posts: 11
Joined: Fri Jun 25, 2021 2:33 pm
Been thanked: 2 times

Re: Internet radio ESP32 and PCM5102

Post by ebs »

Thank you for the clear diagrams and code explaining how to use an I2C DAC.

I have a question, please:

My ESP-32 module uses pins 5 (SDA) and 4 (SCL) for the I2C bus, so I have the statement "I2C.SETUP 5, 4" at the beginning of my code.
With these pins, can I use an I2C DAC, and what would be the right pins to connect the BCK, DIN and LCK pins to on the ESP-32?

Thanks,
Eric
User avatar
Fernando Perez
Posts: 106
Joined: Mon Feb 15, 2021 10:09 pm
Location: Santander (Spain)
Has thanked: 55 times
Been thanked: 54 times

Re: Internet radio ESP32 and PCM5102

Post by Fernando Perez »

Thank you for your comment.
As far as I know on this subject, the I2S BUS has nothing to do with the I2C BUS, despite the similarity in their names.
Up to the I2S bus, which only exists in the ESP32 and not in the ESP8266, Annex accesses through fixed pins that cannot be changed: GPIO2, GPIO5 and GPIO26.
The I2C bus, which exists in both the ESP32 and ESP8266, Annex can be accessed by practically the pins you choose, since it uses a software recreation.
By custom, the GPIO4 (SDA) and GPIO5 (SCL) pins are used on the ESP8266 and the GPIO21 (SDA) and GPIO22 (SCL) pins are used on the ESP32.
Therefore, I recommend that you respect the connection mentioned in the I2S and vary the instruction I2C.SETUP sda_pin, scl_pin with other different pins at your convenience.
User avatar
Fernando Perez
Posts: 106
Joined: Mon Feb 15, 2021 10:09 pm
Location: Santander (Spain)
Has thanked: 55 times
Been thanked: 54 times

Re: Internet radio ESP32 and PCM5102

Post by Fernando Perez »

I take the opportunity to upload an adaptation of the code with a few minimal variants: Turn off bluetooth and set a station that will be the one that will be played automatically when the module is turned on, without waiting for the selection.

Code: [Local Link Removed for Guests]

bluetooth.delete
DIM url$(6) ' internet broadcasting stations
url$(0) = "http://listen.shoutcast.com/thegreatamericansongbook" 'American Song
url$(1) = "http://62.27.87.143/crtve/rnerc/main/mp3/high" ' Radio nacional clasica
url$(2) = "http://37.252.124.170:8010/adagioradio.mp3"
url$(3) = "http://23.106.34.27:24929/stream" ' The UK 1940s Radio Station Server
url$(4) = "http://64.40.99.76:8000/" ' Beatles Radio
url$(5) = "http://192.96.201.2:8000/;?icy=http" ' Italian 60_70
url$(6) = "http://icestreaming.rai.it/5.mp3" ' RAI Classica

emi$ = "American,Clasica_RNE,Adagio,Los_40,Beatles,Italian,Classica_RAI" ' for listBox options
sel$ = ""
info$ = "" ' for streaming metadata
vol = 70 : vol$ = str$(vol) + "%"
status$ = "OFF"
htmlEventVar$ = ""

onplay laut        ' when receiving streaming metadata
onHtmlReload web   ' when the web page reloads
onHtmlChange sel   ' when the web page changes

gosub web
sel$ = "American"
htmlEventVar$ = "sel$"
gosub sel
refresh
wait ' wait for some event
END

' ---------------------------------------------------------------------------------------------------
web:
 cls
 cssexternal "/style.css"
 a$ = ""
 a$ = |<center><h2 style="margin-top:25px;font-family:Tahoma;font-size:1.8rem;">Streaming Radio</h2>|
 a$ = a$ + |Station:| + listBox$(sel$, emi$, "box")
 a$ = a$ + textArea$(info$,"info")
 a$ = a$ + |Volume: | + textBox$(vol$,"porcen")
 a$ = a$ + slider$(vol, 0, 100, 5, "vol")
 a$ = a$ + |Right now i'm: &nbsp; &nbsp;|
 if status$ = "ON" then a$ = a$ + button$("ON",btn,"btn") else a$ = a$ + button$("OFF",btn,"btn")
 a$ = replace$(a$, "Choose here", "Select:")
 html a$
return

' ---------------------------------------------------------------------------------------------------
laut:
 info$ = play.message$
 info$=replace$(info$,"SiteName=", "Station: ")
 info$=replace$(info$, "Genre=", "")
 info$=replace$(info$, "Bitrate=", "Kbps: ")
 info$=replace$(info$, "StreamTitle=", "")
 wlog info$
 refresh
return

' ---------------------------------------------------------------------------------------------------
btn:

if status$ = "ON" then
  play.stop
  info$ = ""
  status$ = "OFF"
  gosub web
end if

return

' ---------------------------------------------------------------------------------------------------
sel:

if htmlEventVar$ = "vol" then
  vol$ = str$(vol) + "%"
  play.volume vol
  refresh
endif

if htmlEventVar$ = "sel$" then
  index = WORD.FIND(emi$, sel$, ",")
  play.setup 1, 64
  wlog url$(index-1)
  play.stream url$(index-1), 15000
  status$ = "ON"
  gosub web
endif

return

ebs
Posts: 11
Joined: Fri Jun 25, 2021 2:33 pm
Been thanked: 2 times

Re: Internet radio ESP32 and PCM5102

Post by ebs »

Hello Fernando,

Thank you for clearing up my confusion about the I2C and I2S buses.
Now I understand that I can indeed use both the OLED on the I2C (software-emulated) bus and the I2S DAC simultaneously.

This is a great community! I only joined a few days ago and all my questions were answered quickly and in detail.

If you don't mind, I have one more:
Did you have to tie any of the pins on the DAC board to ground (FLT, DEMP, FMT) or 3.3/5V (XSMT)?

Regards,
Eric
User avatar
Fernando Perez
Posts: 106
Joined: Mon Feb 15, 2021 10:09 pm
Location: Santander (Spain)
Has thanked: 55 times
Been thanked: 54 times

Re: Internet radio ESP32 and PCM5102

Post by Fernando Perez »

I only linked the SCK pin to GND. I do not know if it is really necessary but I have read avoid instabilities. Audio output by L (Left), R (Right) and G (Gnd) to Hifi amplifier or a simple pair of amplified PC speakers. The sound obtained with a small Hifi system is really attractive.
Before you put it all together in a box, you can do a quick test like this:
image.png
You do not have the required permissions to view the files attached to this post.
Post Reply