ESP32 Sound - Working with I2S

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today in the workshop we'll be working with the i squared s protocol you'll see how we can use inexpensive i squared s devices to build digital audio projects with an esp32 we'll also build an mp3 player and an internet radio it's sounding pretty good in here today so welcome to the workshop [Music] well hello and welcome to the workshop and today we are going to be working with the i squared s protocol and the esp32 now you may have seen the term i squared s on the list of specifications for the esp32 or the raspberry pi or a number of other microcontrollers that support it and you might not have known what it was in fact you might think well isn't that just i squared c maybe just a different form of it well it isn't indeed the i squared part does mean the same thing it means inter-integrated circuit but that's where the similarity ends because i squared s is a protocol for the transmission and reception of sound and it can handle two channels of digital audio and so what we're going to do today is we're going to take some i squared s peripherals we're going to take an esp32 and see how we can get them to work together but before we do any of that you're going to need to learn a little bit about digital audio and about the i squared s sound protocol so let's do that right now i squared s is the inter-integrated circuit sound protocol it was developed by philips semiconductors in 1986 it was built as a connection standard for digital audio devices and integrated circuits and was originally for use within the television industry i squared s can control one or two audio channels of pulse width modulated audio with i squared s the user specifies the bit level and sample rate so it can be used for telephone quality signals as well as for super high fidelity one of the two basic units of digital audio is the analog to digital converter this is a device that converts an analog input signal into a digital output signal the resolution of the analog to digital converter is determined by the number of bits that it uses the frequency response of the a to d converter is determined by its sample rate which must be at least double the highest frequency that you wish to sample the other fundamental component of digital audio is the digital to analog converter this converts the digital input signal back into an analog output it has the same specifications as far as resolution and sample rate go let's take a look at the process of delivering digital sound to the consumer the sound is captured and fed into an amplifier where it appears as an analog signal this analog signal is fed into an a d converter and turned into a digital representation of the sound on the consumer side the digital signal is fed into a digital to analog converter which produces an analog output that analog output is amplified and is delivered to the consumer in digital audio applications the analog to digital and digital to analog converter resolutions can vary between 8 and 32 bits sample rates can vary between 8 kilohertz and 192 kilohertz as a reference cd quality audio is 16 bits resolution with a 44.1 kilohertz sample rate the digital audio signal is in serial pcm or pulse coded modulation format this signal is routed to the different components using i squared s i squared s uses a standard three wire connection plus a ground connection there are a couple of optional wires such as a master clock and multiplex data lines on the three wire bust the first connection is the continuous serial clock or sck connection the ws or word select pin determines whether we are transmitting the left or right channel of audio and the sd is the serial data the actual pcm audio data itself we examine the waveforms of the i squared s bus we'll see the serial clock is at a constant rate the serial data is the audio data and it varies between 0 and 1. whether that audio data represents the left or the right channel is determined by the word select pin the clock frequency is determined by multiplying the sample rate by the number of bits per channel and by the number of channels so for cd quality audio we would have a sample rate of 44.1 kilohertz multiplied by 16 bits per channel multiplied by two channels for a clock frequency of 1.4112 megahertz if we were just sending a telephone quality signal our sample rate would be only eight kilohertz and we would only require eight bits per channel and we'd only need one channel this would result in a clock frequency of 64 kilohertz an i2s network consists of a single controller device and one or more target devices the controller produces and controls the system clock target devices receive the clock the controller controls the word select line in other words it determines whether the left or right channel is being manipulated target devices receive the word select information and act accordingly here's a typical application with an audio transmitter and audio receiver device using i squared s the transmitter is our controller and the receiver is our target so the transmitter is sending the clock the word select and the serial data which is the actual audio that's being transmitted but things don't have to be configured this way if we don't want to we can also have our receiver become the controller and our transmitter become the target in this case the receiver as the controller is providing the clock and the word select information however the transmitter is still providing the serial data as it is the source of the music another viable configuration is to have both the receiver and transmitter become targets and have an external controller device that provides the clock and the word select information again the serial data is coming from the transmitter because it is the source of the audio the esp32 has two i squared s peripheral devices these devices can be configured as either controller or target they can also be set up as either a transmitter or a receiver direct memory access allows the serial data to bypass the esp32 processor for improved performance each of the i squared s peripherals is a half duplex device so together the esp32 can perform full duplex the i squared s0 peripheral can route its output directly to the esp-32's internal 8-bit digital-to-analog converter i-squared s0 can also transmit pulse density modulation signals the esp-32 implementation of i-squared-s also supports what's called lcd mode which is a mode allowing the controls of external cameras and displays using i squared s i squared s and the esp32 can be used for multimedia controllers music players telephony toys and games professional audio and a myriad of other applications so let's put some sound into our projects using i squared s and the esp32 so i've got a little collection of i squared s devices to show you i've got them here on the workbench along with some of the connectors that go along with them and you can basically divide these devices into two categories they're either microphones that produce i squared s data they have microphones and analog to digital converters on them or they are the opposite and they take in i squared s data and they produce an analog output for a speaker or for headphones and so they will have a digital to analog converter plus an amplifier on board let's take a look at the microphones first it's these two tiny things here and if you notice the size of my pen as a reference you can see just how tiny they are these little metal squares on here are actually both the microphones and the analog to digital converters and the output here is going to be on i squared s and you can select whether you want it to be the left or the right channel these three over here are pretty well the same type of devices these are the opposite end in which they take in an i squared s signal and they produce an analog output to a speaker output so they've got a digital to analog converter and amplifier on board and you can strap them to use either the left channel the right channel or do a combination of both channels and this device over here is from adafruit it's a stereo dac and it's got a stereo headphone jack over here that you can plug into some speakers or into some headphones if you wanted to and it's got a high quality dac on it and again this is a device that you'd feed i squared c2 and it would produce a stereo output from it so you're going to be seeing a few of these devices in today's experiments and all in all just an example of the great selection of i squared s devices that you can experiment with so now that we know more about i squared s and digital audio it's time to get to work with it and we're going to begin with one of the i squared s microphones that i showed you a bit earlier now these microphone modules are pretty amazing they've got a very tiny mems microphone on them and they also have the required analog to digital converter in order to convert this into digital audio so they're quite easy to use now in this experiment we're just going to use the i squared s library that is included when you install the esp32 board on your arduino ide so we're not going to be using any dedicated libraries although we will a little bit later on in the video use some dedicated libraries so let's begin by hooking up our i squared s microphone to our esp32 the i squared s microphone that we'll be using today is a very popular module the inmp-441 despite its low cost it has some very impressive specifications including a 24-bit digital is4s interface the device has a signal-to-noise ratio of 61dba which is quite impressive it also has a frequency response of 60 hertz to 15 kilohertz note that on this module as with most i scored s mems microphone modules you actually need to mount it upside down as there is a hole in the bottom of the printed circuit board and this is where the sound enters for the microphone we'll be using the inmp-441 with an esp-32 board you could use any esp-32 board that you like i'm showing the wr overboard if you use a different board make certain that you wire everything by the gpio number and not by the actual physical pin number you could also use different gpio pins if you wish is none of them are critical you would of course have the chain to sketch to reflect your changes we'll begin by connecting one of the esp32 grounds to the ground on the inmp-441 we'll connect the 3.3 volt output of the esp-32 to the vdd pin on the microphone module the sd line which is our data line will be connected to gpio pin 32 the sck line which is the clock line will be connected to gpio33 and the ws line which is the word select line that determines left from right is going to be connected to dpio pin 25. we'll also connect the l r pin to ground this sets the microphone to give a left channel output if you bring this pin high the output will be on the right channel and this completes our wiring now here's the code that we're going to be using to sample our i squared s microphone and all we're going to be doing with this code is taking the sound in decoding it and printing it out to the serial plotter so we can look at the waveforms on the plotter now we're not going to use any special library with this we're going to be using the i2s library that was included with your esp32 installation in the arduino ide so we called the i square s library right over here and then here we define the connections we've made to our microphone now there's nothing particularly special about these three gpio pins so if you want to use three different ones you can just change everything accordingly the esp32 as you recall has two i squared s processors we're going to be using processor zero so we define that here and we define a buffer length that we're going to be using now we have two functions that we've created and these functions are just to make configuration of the i squared s a little bit easier this one does all the configuration functions for it and you can see a number of them buried here in the configuration screen we're going to be running this in the controller mode and we're going to be running it as a receiver sample rate of 44.1 kilohertz we're going to be using 16 bits and we're going to be using only the left channel now something you might want to note the i squared s library has recently changed and some of the previous functions were degraded and when they were some of them stopped working and you'll find a lot of code that goes with those libraries it fails to compile and i'll tell you from some experience that if it does fail to compile it is this line over here that probably is the culprit because the syntax has changed with the new library so this is correct for the new library if you run into any old code that won't compile you might want to look at that at any rate after putting all the configuration data in we just install that data and that's the end of that function we have another function called i squared s set pin and it sets up the pin configuration and so we just pass it the three pin values and then we do an i squared s set pin in order to configure it and then we'll go into our setup now now in the setup we're going to set up the serial monitor because of course we're using the serial plotter so we do that and then we set up the i squared s and we can call the three functions that we called earlier the install the set pin and then we'll do a start on the i squared s port that we have and that completes our setup now we're going to go into the loop and remember we're going to try to print this out onto the serial plotter now the serial plotter has a dynamic y-axis and you can't do anything about that and sometimes it's a bit annoying including in this particular situation because what will happen is if we have hardly any sound the access will increase in sensitivity so that all we're seeing on it is the noise and then we suddenly have sound the access is going to jump up to a level of about two three even four thousand and so in order to steady it what i've done is i've printed two different values a positive and a negative one and i'm calling them the range limit and you can adjust this value over here for range limit i found 3000 was pretty good but you might find a higher or lower number works and that'll just kind of lock the range of the serial plotter to make the waveform easier to see and then after that we're going to get the business we're out for the i2s data we're going to grab that data and if we get the results we're going to go and print that data we'll average it and print it out to our serial plotter and then we'll do it over and over again so it's a relatively simple sketch and it illustrates how to use the i squared s library let's load it up and take a look at it in action so here's my setup with the i squared s microphone and my esp32 dev kit i've got the microphone mounted on a small sawless breadboard and i'm not sure if you can see it that well but the microphone is mounted what you might consider to be upside down in other words the component sides are facing the solderless breadboard and that is the correct way of mounting this microphone there's a hole on the bottom and that's where the sound is coming in now if you're looking at the serial monitor you can see my voice right now as i'm speaking as it's going on the serial monitor you'll also see a line at the top near 3000 and one down near negative 3 000 and those are the fixed values that we put there just to hold the serial monitor in place and so i can actually exceed those values sometimes let me just go and clap i'm going to make a loud noise over here and they come pretty yeah over there i exceeded it a couple of times so you could play with the 3000 value but i found that it was suitable for most applications without that you're going to see this dancing all over the place but as you can see we can see the waveforms of my voice on the serial plotter and so this does seem to indeed work we are picking up sound in an i squared s format and delivering it over to the esp32 which in turn is translating it and printing it out to our serial plotter so it does seem to work pretty well and so using the i squared s library that comes with the esp32 board is a pretty easy way to talk to i squared s peripherals but there are other libraries as well that we can use now there's a bit of a story behind my library selection so i'm going to try to make it brief it started out back in march when i was actually working on this video because i was planning to bring this video out at the end of march and it was a sunday morning i was working on the video decided to take a break and see what andreas had on his channel because he releases his videos on sunday mornings and when i went and looked andreas had just released a video on using i squared s with the esp32 so i thought to myself well i can't release this video next week it's going to look silly we have a lot of the same audience and it's going to look like i'm just copying andreas which i certainly wasn't doing but in andreas video he brought up this excellent library it's kind of like the swiss army knife of libraries for the esp32 and i squared s yes swiss army knife and dress with the swiss accent anyway this library was wonderful it had everything it had equalizers all kinds of audio devices that you could build with it but i felt that using the same library he used really wouldn't serve any point because he's already covered it very thoroughly so i'm using another i squared s library today and the first project that i'm going to be building with this library is an mp3 player we're just going to take an mp3 file that's on a micro sd card and play it using an i squared s amplifier so let's go and see how we hook that up and then how we code for it the output of our mp3 player will be provided by an max98357a i squared s amplifier module this is a very popular module and it's made by several different manufacturers the device is a three watt class d amplifier and it has an i squared s input it has a built in i squared s digital to analog converter it can be configured to use the left the right channel or by default a mix of both channels now here are the pinouts for this device the lrc connection would be the i squared sws connection this is the line that selects whether the left or right channel is being used the bclk is the i scored s clock line the sck line now here's where things get a bit confusing the d inline is actually the sd line on the i squared s bus this is where the data comes in it's not to be confused with the other pin labeled sd which i'll talk about in a moment the gain pin can use a resistor to set the value of the gain of the amplifier this pin can also be grounded the sd line as i mentioned is not the data line instead this is how you select the left or right output channel you use a resistor on this line up to the vcc in order to do this now by default it is set to a blend of both channels so if you have no connections you will get a mono output dnd is of course the ground line and v in is the power supply line and this device can take anywhere from three to six volts dc for the micro sd card i didn't want to use just any module and i chose the adafruit 254 microsd module i chose this device because it is both 3.3 and 5 volt compatible you'll have to be careful about that because although the micro sd's themselves work on 3.3 volts most modules are 5 volt modules and will output a 5 volt logic signal which would be dangerous to the esp32 one other feature of this device is that it also has an on-board activity led so you can tell if it's actually working now we'll be connecting both of our modules to an esp-32 and once again i urge you to look at the gpio numbers and not the physical pin numbers if you're using a different module than i am of course the amplifier will also need a speaker and any conventional 4 to 8 ohm speaker will work note that you can't use a dynamic speaker or feed this into an amplified speaker because the voice coil in the speaker is actually part of the filter circuitry used in the output of the audio amplifier we'll begin with the wiring of the max9357a amplifier module we'll connect the vn on this module to the 3.3 volt output on the esp32 note that you could also use a 5 volt connection for this and you would get a lot more power from the amplifier if you did the gnd pin is of course connected to one of the esp32 grounds we're also going to connect the gain pin to ground the dn pin which is the data in is connected to gpio pin 22 bclk which is the clock pin will be connected to gpio26 and the lrc which is the ws line that selects the left or right channel is connected to gpio pin 25 and we'll also need to connect our loudspeaker to the module depending upon your module you may have screw terminals for this or you might just want to solder these wires directly to the board on the microsd module we'll connect the 5v connection to the 3.3 volt line note that although it's labeled 5v this device will also work on 3.3 volts we'll connect ground to one of the esp-32 grounds the clk line will be connected to gpio pin 19 the do line which on some modules may be labeled miso is connected to dpio pin 18 and the di line which on some modules could be mosi is connected to gpio pin 23. the cs or tip select line is connected to gpio pin 5 and this completes our wiring now here's the sketch for our esp32i2s sd music player and the sketch is made quite simple thanks to the esp32 audio i squared s library that you can grab upon github and you'll need to grab that as a zip file and install that into your arduino ide now this library makes it very very simple to work with i squared s and it kind of hides all of those intricacies that we had to deal with with the microphone sketch from you so all of the things about setting up an i squared s connection are dealt with with the library now we're going to start by including our required libraries and the audio library is the ah is the library in question we're also including the sd and file system libraries as well because we're using the micro sd card we'll define our connections to the micro sd card and we'll also define our i squared s connections then we create an audio object which we've done over here and we can move into our setup now in setup we have to set the cs line on the micro sd card as an output and then set it a high and that's the tip select line for it so it knows that it's been selected because of course the spi bus can have two different devices on it and we want to make sure we know that this is the one we're selecting now we'll initialize the spi bus over here for the micro sd card we'll start our serial port and then we'll start the micro sd card over here now we're going to set up i squared acid as you can see it's very easy to do with the audio library we just do set pin out and we passed with the parameters of our i squared s connection we're going to set the volume initially you can play with this level don't make it too high because you'll be surprised at how loud it gets and then we're going to go and open the music files so we're going to do an audio connect the file system on the sd and we're going to open up the ben sound clear day dot mp3 which is the theme music for the dronebot workshop and then after that we're just going to go into our loop and all we do in the loop is play audio so it's a very simple sketch thanks to this library let's load it up and see if we can make some music with it and so here's my mp3 playing micro sd demonstration all set up and ready to go it's got my esp32 my i squared s amplifier and my micro sd card over here this is an old speaker from a television that i was scrapping and i took out and i'm going to use that as my output device and it's all ready to go all i need to do is hit the reset button on my esp32 and we should start everything off again so let's just do that [Music] and it plays a pretty familiar tune [Music] now there are no controls on this mp3 player uh the only way to stop it is to literally pull the plug uh the only way to start the song again is to literally pull the plug and i have no control over volume so let me just do that right now got a little bit loud but it is a basic demonstration of how easy it is to take an mp3 file and use it with the audio library to drive an i squared s output device so as we just saw using the esp32 i squared s audio library really simplifies working with i squared s and it was quite easy to build an mp3 player with it it is just as easy to build an internet radio to listen to all of the hundreds of internet radio stations that are out there and that's exactly what we're going to do right now now hooking up the internet radio is very simple because it's exactly the same hookup as the last experiment minus the micro sd card so if you've already got that hooked up you can just leave it there and ignore the micro sd card it also opens up the possibility of building a device that can play both mp3s and listen to the internet radio and so you could work on that as well using the code that we're about to present so let's go and take a look at that code and then we'll hook up our internet radio like i said our internet radio wiring is identical to that of our mp3 project minus the micro sd card if you've already wired up the micro sd card you could just keep it there it's not going to do any harm otherwise you could just remove it from that circuit and so now let's take a look at some of the code that we're going to use in order to make our internet radio work so here's the sketch that we're going to be using for our simple internet radio demo and once again the sketch has been simplified by the use of the esp32 audio i squared s library that you need to download from github as a zip file and install in your arduino ide we'll start off by including the required libraries including the wi-fi library and the audio library that we just spoke of and then we'll define our i scored s connections into these three gpio pins if for some reason you want to use different pins just change the sketch accordingly then we can go and create an object for our audio we do that here we have a couple of variables also to hold the network credentials so our network ssid internetwork password and then we'll jump into the setup in this setup we'll start our serial monitor we'll start our wi-fi in station mode we will begin it by passing the password and the ssid to it assuming that we have connected we're going to print that out to the serial num monitor including our local ip address and then we'll connect the i2s amplifier module and we just use set pin out with the audio library and pass it the three connections we're going to set the volume right now and don't make this too high a number when you start off you can always play with it i use 10 and then we have to connect to an internet radio station i've got a number of different ones that you can connect to over here i've unremarked one of them if you find that they aren't working very well you can maybe just try a different one and then we go into the loop and the loop is very simple it's like the loop that we had in our sd player we just do an audio loop and it just simply plays now some extra functions we've added down here these are kind of optional but they will print out to the serial monitor is some status functions for audio and there's a whole bunch of different statuses we can play we can play the title we can play some stream info there's a number of pieces of information that we can put up on the serial monitor and you can pick and choose from these you don't necessarily need to use all of them but it's fun to put them there and so let's load this up to our esp32 and see if we can listen to some music with our internet radio okay so i'm playing my internet radio right now and if you take a look at the serial monitor you can see i've got a whole bunch of information about what's playing right now including the fact that i'm playing a classic queen song bohemian rhapsody however you're probably noting that you can't hear it at the moment and the reason for that is simple enough if you look over here i've disconnected my speaker and unfortunately the reason i've done that is because of youtube copyright rules i can't actually play you bohemian rhapsody right now because this video would be flagged as a copyright violation so you'll just have to take my word for it you can see now it's flipped over to yet another classic song santana's black magic woman so we got some great music going here on our internet radio which we sadly can't hear at the moment but you'll have to take my word for it that as soon as i connect this speaker up it will be filling the room with music right now and it does seem to work pretty well one thing i have noticed though is when you start off sometimes you need to reset a few times occasionally to get a connection and that sort of depends on the internet radio station as well and that issue may indeed be an issue on the other end about the maximum number of connections or something that it can accept but otherwise as you can see with an esp32 and an i squared s amplifier we can build a pretty effective internet radio now the experiment that we're going to do to finish off the video ended up changing a little bit and it was because of an accident i had over here i'm still not quite sure how the accident happened i wanted to show you how we could give both a stereo output and add a volume control to these circuits because stereo output obviously would make the music sound better and a volume control is a pretty essential thing for any audio device and so i was going to do that using the mp3 player because of course with the mp3 player i can play you some music that doesn't violate youtube's copyright rules and we won't have any problems with the video well i had it wired up and was working with it and then i ejected my micro sd card and when i went to put it back in it wouldn't go in it kept popping out and i have no idea why nothing physically happened to the card reader so i put it under a magnifying glass found a little tab on it and used a jeweler's screwdriver to bend the tab in a bit and now the card does latch but it doesn't seem to work properly i get intermittent data out of it so i have no idea what happened to my micro sd card but it kind of messed up the last demo and it also delayed the release of this video for which i apologize so i've reworked it with the internet radio and of course that's going to make it a little bit more difficult to demonstrate because of the copyright issues but i'll just try to do it really quickly and so what i've done is i've changed our internet radio to have a stereo output and to also have a much needed volume control now for the stereo output we will of course be using two i squared s amplifier modules and in order to use these modules we're going to have to strap one for the left channel and one for the right channel so let me show you how you do that then i'll show you how we wire everything up and code our stereo internet radio now in order to create a stereo output we'll be using two max 98357a amplifier modules and they need to be set to either only the left channel or only the right channel and you do that on most of these modules by using the sd pin now as i discussed earlier sd is not the serial data pin that is the dn pin this is the pin whose voltage level determines the output mode of the amplifier when this pin is grounded or set to zero volts the amplifier will have no output at all so you could use this as a form of a mute control if you wish when the voltage level on sd lies between 0.16 and 0.77 volts dc then you will get a stereo average or mono signal out of the amplifier this is the default mode because there is an internal pull down resistor on the board that maintains this voltage level you can use a pull-up resistor to the v-in in order to get the voltage higher and if you set it between 0.77 and 1.4 volts the amplifier will output the right channel if you make the voltage higher than 1.4 volts it will output the left channel now if you happen to have a sparkfun implementation of this module it's a lot easier there is a jumper on the bottom that is thereby default for mono and you can cut that jumper and then look above it at a couple of traces there is a center trace and two marked as l and r you just need a blob of solder or some other way of shorting the traces so you can set this for either the left or right output accordingly the hookup for our stereo radio is going to be very similar to that of the mono one except of course we're going to have two amplifiers however one change i'm making is instead of powering this with the 3.3 volt output i'm now powering it with the 5 volt dc output note that the 5 volt pin on the esp32 is both an input and an output if you're powering this with a micro usb then you'll get a 5 volt output here otherwise you'll be applying 5 volts here in order to power the esp32 i'm using 5 volts instead of the 3.3 volts because i feel that the 3.3 volt regulator on board the esp32 is going to be overly taxed if it's trying to drive two amplifiers it can each output three watts plus we'll get a little bit more volume this way we're going to add a second amplifier board and we're going to connect it exactly the same as the first one so everything is in parallel note that you'll need to have already strapped your board for the right and the left channel using whatever method your board uses as we just discussed also note that when you're wiring up the speakers you should try to wire them in phase in other words connect the positive to the same terminal on each speaker that way you'll get better stereo sound for a volume control we're going to be using a potentiometer and the value is not critical i used a 10k linear plot now you might think that an audio taper pot would do a better job over here but the actual tapering is going to be done in software so a linear taper pot will be just fine we're going to connect one side of the pot to one of the grounds on the esp32 the center lead or wiper of the pot will be connected to dpio pin 39 this is also analog to digital converter 0 3 and the other end of the plot will be connected to the 3.3 volt output it is very important to use the 3.3 volt output and not the 5 volt output and that's all there is to the wiring for our stereo radio now let's go and take a look at the code modifications we need to make in order to use our volume control now here's a sketch for our internet radio with the volume control and stereo output and actually the sketch has nothing to do with the fact that it has stereo output that has to do with the way that we strap our amplifier modules and you could have received stereo output from our previous sketch so this sketch really just shows the addition of the volume control now we're going to start off by including the same libraries we've always included in these sketches and then we're going to define our i squared s connections and now we're going to define the connection that we have to the potentiometer that we're using as a volume control now we're using analog to digital converter number three which is the same as gpio pin 39 so we define this as a 39 and you could use a different adc providing you kept it within the same group there are a couple of groups of adc's on the esp-32 and the first group is where you need to have this control because the second group is not available when you're using wi-fi now we define an integer that represents our volume level this can be a number between 0 for no volume and 20 for full volume and i've defined it in the middle as being 10. we create an audio object and get our wi-fi credentials so you'll need to substitute your own credentials in over here of course now we go into the setup which is identical to what it was for a previous internet radio we start our serial monitor we'll set up our wi-fi in station mode and print the connection information to the serial monitor we will connect to our i squared s amplifier module and now we'll set the volume to the level we previously defined which was 10 and connect to an internet radio station and once again i've got a number of different ones you can choose from over here and then we go into the loop and this is where the real change for the volume control addition is made we run the audio player as we did before and now we go and get the volume level so we do an analog read on the volume control and then we map that to a level from 0 to 40 95 and we bring that to a level of zero to 20 and we assign that to the variable volume and then we set the volume using that variable and we just go back to the loop again so it's a pretty simple addition now after that we have the same status functions we did before and this will just print out some status functions out to the serial monitor so a very simple change or sketch let's load it up and check it out so here's my stereo internet radio wired up on the solderless breadboard and you can see the two speakers i've got over here the two of the salvaged out of my television now the amplifier modules i used are over here and these are the sparkfun modules and of course the sparkfun modules were very easy to use because all you need to do is cut a jumper on the bottom and then strap it for either the left or the right channel otherwise you're going to need to use a pull up resistor on the sd pin as i showed you earlier and i would suggest if you're going to do that to use something like a 100k pot on that pin tied up to the 5 volt line or the v in and then adjust the pot until you've got the right voltage level for left or right channel respectively after that you could just leave the pot in or you could replace it with a fixed resistor if you wished now here of course is my volume control and my esp32 and it's running right now it runs very well but of course playing music is difficult because of copyright violations i'm going to see if maybe we can do this very very quickly and hopefully that won't flag it on youtube so and we do have level and it goes up when i turn the control over here [Music] and hopefully we won't be in violation for that queen song over there that was playing in the background but you could make the same modifications to the mp3 player had i not uh somehow damaged my micro sd card i would have demoed with that instead and as you can see from the code adding a volume control was a very simple thing to do and it really makes the internet radio a lot more practical all right well that pretty well wraps up our look at i squared s for today i hope that you enjoyed it and i hope it's opened your eyes up to the fact that we can use the i squared s protocol to build audio devices with our esp32 now if you want some more information about i squared s or if you'd like to grab the code that i used today you'll find that of course on the article that accompanies this video on the dronebotworkshop.com website and there is a link below the video right to that article and while you're on the website if you haven't yet please sign up for my newsletter no it's not a spam letter it's just something i send out occasionally and i've been a little lacks about sending it out lately but i will be sending out one soon and it's free to subscribe and it just lets you know what's going on here in the workshop and also if you'd like to discuss this a little bit more if you want a little bit more information about i squared s or you're having problems with one of the sketches best place to reach me is on the dronebot workshop forum the forum of course is free to join and it's full of like-minded individuals who love working with electronics and who can help you out with your project or would just like to hear about your project so sign up for the forum if you haven't done that yet and while you're in a signing up mood if you haven't please subscribe to the youtube channel it's very easy to do you just need to click the red subscribe button and also click that bell notification and assuming that you've enabled notifications on your youtube you'll get notified every time i make a new video so until we meet the next time please take care of yourself please stay safe and i will see you again very soon here in the dronebot workshop goodbye for now [Music] [Applause] [Music] you
Info
Channel: DroneBot Workshop
Views: 320,309
Rating: undefined out of 5
Keywords: i2s audio, i2s protocol, esp32, esp32 i2s
Id: m-MPBjScNRk
Channel Id: undefined
Length: 46min 4sec (2764 seconds)
Published: Sun May 22 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.