Building a Digital Music Player with I2S?! What is I2S! EB#45

Let’s imagine you have a microcontroller
board like this ESP32 and you just recorded an awesome sound clip that sounds like this:
“Stay Creative!” Now of course you want to playback this sound
with the help of the ESP32, so what do you do? Well, the input side is pretty straightforward
by simply adding a micro SD card breakout board to the ESP32 through its SPI interface. Due to this fast data line, all the bits of
the original audio file can be read by the ESP32 without losing any audio quality. That means we still got the original resolution
of 16 bits which describes how many steps our original analog audio signal was digitally
converted. As a rule of thumb, the more bits we get,
the closer we are to the original analog signal. Also, the sampling rate of 44.1 kHz is kept
intact which describes how often the voltage of the analog signal got sampled; in this
case around every 22.7uS.

And once again the higher the sampling rate,
the closer we got to the real analog signal but let me tell you that 16 bits and 44.1
kHz sounds pretty good. So the input side is done and for the output
side, we could use the internal DAC aka Digital to Analog Converter of the ESP32 to create
the analog audio signal. But sadly it only comes with a resolution
of 8 bits which would act as a bottleneck for our audio quality. Instead, I wanted to use this MAX98357A amplifier
breakout board which uses I2S to communicate. So in this video, we will find out what I2S
is and why it is pretty handy to use and along the way we will not only learn working with
the I2S amplifier board but also with an I2S microphone board.

Let’s get started! This video is sponsored by JLCPCB, who produce
high-quality PCBs for a very affordable price. After over 3 years of using their service
I can still highly recommend it. So why not upload your Gerber files today
and receive your PCBs sometimes even within a week? First off, I want to say that we are not forced
to use an I2S DAC Amplifier board on the output. We could also build up a 16-bit R-2R DAC but
that would require 16 pins of the ESP32 and thus is not practically useable. Or we could simply get a 16-bit DAC IC which
we could hook up to the ESP32 through for example I2C. But then again if we already use a communication
protocol, why not use I2S which was specifically designed for audio applications?

I2S stands for Inter-IC Sound and is an
electrical serial bus interface that connects digital audio devices. It communicates PCM audio data which stands
for Pulse-Code Modulation and that is a method to digitally represent sampled analog signals
which is for example used by our .wav sound file. It stores the binary values of all
the sampled analog voltage values in it, which is basically what we want to send around
with I2S.

For a first practical example, however, let’s
not start with the amplifier board but instead with this INMP441 Microphone breakout
board. With it, the I2S concept will be easier to
understand since we only have to read in data with the microphone while we would have to
read in data and send out data with the micro SD card and the amplifier board. But anyway, the microphone features 6 pins
of which 2 are for power, 3 are for the I2S interface and one is kind of a bonus to select
whether the microphone outputs its data on the left or right channel. The three pins required for the I2S are called
word select, serial clock, and serial data. In the case of the amplifier board, later on,
they feature slightly different names but still mean the same thing.

So as a first test, I soldered wires to the
5 important pins and continued by having a look at the ESP32 pinout diagram in order
to find out where to connect the I2S pins and to my surprise, there were no specific
pins mentioned. As it turns out the ESP32 comes with two I2S
peripherals but you can select the pins for them by yourself.

So I decided on pins 15, 2 and 4 according
to this wiring diagram and after connecting a micro USB cable, I had a quick look at the
I2S programming guide page for the ESP32. I simply copied a bit of the shown code to not only set the resolution to 16 bits and the sampling rate to 44.1kHz but
also to initialize and start the I2S communication. All I had to add were a couple of lines to
output the received data through the serial monitor. After uploading the code and opening the
serial monitor we can see that by whistling into the microphone or tapping it, the serial
monitor or even better the serial plotter seems to react accordingly, awesome.

That means everything works fine and it was
time to have a look at the three I2S pins on the oscilloscope. First off we can see that the word select
lines constantly alter between high and low. The reason is that this bus transmits stereo
audio so the low state of the word select line indicates the left channel and the high
state stands for the right channel. By the way in this example, we only got data
on the left channel because the unconnected L/R pins define that. Then we got the Serial Clock generated by
the ESP32 at whose rising edge the data of the Serial Data gets sampled.

This way the audio data bits sent out by the
microphone can be read by the ESP32 which is pretty standard communication interface
knowledge you should know from my SPI, I2C, or for example CAN video. But what is special about this communication
is the frequency of the clock signal. It is adjustable and depends on the
sampling rate, bit resolution, and whether you want to transmit mono or stereo. In our microphone example, we selected 44.1kHz
with 16 bits and stereo which means we have to multiply 44.1kHz 16 by 2 which equals 1.411MHz
which is also the frequency we are measuring. Now of course at this point, we could alter
the received microphone data to for example create a voice changer or even a pretty scientific
sound level meter but that was not the goal of this video.

th-77

Instead, I soldered wires to my I2S amplifier
board as well as the micro SD card board and hooked it all up to the ESP32 according to
this wiring diagram. This time though the coding was not only about
the I2S write part but also about SPI which is why instead of writing all of the code
by myself which could have taken months, I decided to use the available ESP8266 audio library
and some code bits from Spark fun. But in the code, we can still see that the
I2S part is pretty straightforward to implement. And after uploading the code, connecting a
speaker, and inserting the micro SD card with the sound file, we can hear this: “Stay
Creative!” Awesome! And just like that you are now familiar with
the I2S basics and all in all I can say that it is a pretty easy-to-implement and handy-to-use communication interface when it comes to receiving and sending digital audio.

And if you are interested in a more advanced
video about the topic then I would highly recommend checking out Bitluni’s video about
creating a sound driver for the ULP of the ESP32. But anyway I hope you enjoyed this video and
learned something new. If so don’t forget to like, share, subscribe, and hit the notification bell. Stay creative and I will see you next time.

Free Personalized Birth Chart Reading | Individualogist.com

                                     ⁿᵃᵗᵃˡ.ᶦⁿᵈᶦᵛᶦᵈᵘᵃˡᵒᵍᶦˢᵗ.ᶜᵒᵐ

ɪɴᴅɪᴠɪᴅᴜᴀʟᴏɢɪꜱᴛ ɴᴀᴛᴀʟ ᴄʜᴀʀᴛ
ʏᴏᴜʀ ᴀꜱᴛʀᴏʟᴏɢʏ ᴄʜᴀʀᴛ ʀᴇᴠᴇᴀʟꜱ ʏᴏᴜʀ ᴅᴇꜱᴛɪɴʏ…

ᴀ ᴄᴏᴘʏ ᴏꜰ ʏᴏᴜʀ ʙɪʀᴛʜ ᴄʜᴀʀᴛ ᴡɪʟʟ ʙᴇ ꜱᴇɴᴛ ᴛᴏ ʏᴏᴜʀ ᴇᴍᴀɪʟ ᴀᴅᴅʀᴇꜱꜱ. ᴘʟᴇᴀꜱᴇ ᴇɴᴛᴇʀ ᴀɴ ᴇᴍᴀɪʟ ᴀᴅᴅʀᴇꜱꜱ ᴛʜᴀᴛ ʏᴏᴜ ᴀᴄᴛɪᴠᴇʟʏ ᴜꜱᴇᴅ ɪɴ ᴏʀᴅᴇʀ ᴛᴏ ʀᴇᴄᴇɪᴠᴇ ʏᴏᴜʀ ꜰʀᴇᴇ ʙɪʀᴛʜ ᴄʜᴀʀᴛ ʀᴇᴀᴅɪɴɢ.

ʳᵉᶜᵉᶦᵛᵉ ᶠʳᵉᵉ ᵍᵘᶦᵈᵃⁿᶜᵉ ᵗʰʳᵒᵘᵍʰ ᵒᵘʳ ᵈᵃᶦˡʸ ʰᵒʳᵒˢᶜᵒᵖᵉ ʳᵉᵃᵈᶦⁿᵍ. ᵈᶦˢᶜᵒᵛᵉʳ ᵃᶜᶜᵘʳᵃᵗᵉ ᵖʳᵉᵈᶦᶜᵗᶦᵒⁿˢ ᵃᵇᵒᵘᵗ ʸᵒᵘʳ ᶠᵘᵗᵘʳᵉ ʷᶦᵗʰ ᵈᵃᶦˡʸ ʰᵒʳᵒˢᶜᵒᵖᵉˢ!