Using MicroSD Cards with ESP32 Boards: A Step-by-Step Guide to Playing Audio Files
Let's imagine you got a microcontroller board like this ESP32 and you just recorded an awesome sound clip which sounds like this: “Stay Creative!” Now of course you want to play back 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 in how many steps our original analog audio signal was digitally converted. 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.
Also, the higher the sampling rate, the closer we are to the real analog signal, but let me tell you that 16 bits and 44.1kHz 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.
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 PCB?
What is I2S?
I2S stands for Inter-IC Sound, which is a standard for audio interfaces on ICs (Integrated Circuits). In short, it's like the Ethernet protocol of the audio world, designed to communicate data between devices. Now you might be wondering what I2S transmits and how does it work. Let me tell you.
I2S transmits stereo audio over an inter-IC bus, which makes it very handy for creating multi-channel sound systems. The word select line constantly changes between high and low; this is due to the fact that this bus transmits stereo audio where the low state of the word select line indicates the left channel and the high state stands for the right channel.
Also, I2S generates a clock signal whose frequency depends on the sampling rate, bit resolution, and whether you want to transmit mono or stereo. In our microphone example, we selected 44.1 kHz with 16 bits and stereo which means we have to multiply 44.1 kHz by 16 which equals 1.411 MHz which is also the frequency we are measuring.
I2S Basics
In this section, we will delve into the basics of I2S and why it is a pretty easy-to-implementation and handy-to-use communication interface when it comes to receiving and sending digital audio.
I2S transmits data over an inter-IC bus which means that the clock signal generated by the transmitter is used to synchronize the receiver. This way, the audio data bits sent out by the microphone can be read by the ESP32 which is pretty standard knowledge you should know from my SPI, I2C, or CAN video.
But what makes I2S special? It's the frequency of the clock signal it transmits which allows for adjusting to different sampling rates and bit resolutions. In our example, we selected a 44.1 kHz with 16 bits and stereo, but you can choose any other configuration depending on your needs.
In the next section, we will explore how I2S works in practice using an ESP32 board.
Using I2S with ESP32 Boards
Now that we have covered the basics of I2S, let's dive into a real-world example where I2S is used with an ESP32 board.
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 me which could have taken months, I decided to use the available ESP8266 audio library and some code bits from Spark fun.
In the following paragraphs, we will see how everything works together.