Using the ESP32 DAC - Voltages, Waveforms & Sounds

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today in the workshop we're working with the esp32 DAC you'll learn how the DAC works and how to use it to produce DC voltages and waveforms we'll also create some oscilloscope art and an edible musical instrument we're making waves today so welcome to the workshop [Music] well hello and welcome to the workshop and today we're going to be working with our friend the esp32 now this is becoming probably one of the most popular microcontrollers here in the dronebot workshop and for good reason it's got Bluetooth and Wi-Fi capabilities and it opens us up to a number of remote control and iot projects however the esp32 is also a very powerful microcontroller with a number of other features and it's one of those overlooked features that I'm going to be dealing with today the digital to analog converters or Dax now pretty well every microcontroller that we've used here has analog the digital converters in the esp32 has a number of them as well these allow you to input an analog voltage and get a digital representation of its level while a DAC does the opposite thing it allows you to take a digital number and convert it into an analog voltage in the esp32 has two 8-bit dacs now these aren't audio quality dacs so you're not going to be able to put High Fidelity audio into that for that you would probably use I squared s and we've talked about that earlier but you can use the Dax to Output output voltages you can use it to create waveforms and as you'll see you can even use it to make some music and so what we're going to do is take a look at how the esp32 dacs work and then we're going to see how we can use them in all of their different modes a digital to analog converter is a device that has a digital input and an analog output the analog output is in proportion to the value of the digital input digital to analog converters or dacs are used in audio and video applications telecommunications for instrumentation for pulse width modulation and complex waveform generation and as digital potentiometers there are many methods that can be used to convert a digital input to an analog output including such items such as pwm two very common methods are weighted resistor Dax and r2r Ladder Dax and these are Illustrated here note that the switches in these dacs actually just represent the ones or zeros of its digital input the number of bits that a DAC has determines its Precision or resolution as an example an 8-bit DAC would provide 256 discrete output voltages the esp32 has two 8-bit dacs for the majority of esp32 boards you'll find that the two dacs are located on gpio pin 25 and gpio pin 26. however if you have an esp32-s2 you'll find the channel one of the DAC is on gpio pin 17 and channel 2 is on gpio pin 18. the esp32 DAC supports three different methods of analog output the direct voltage output method the cosine wave generator output and continuous output by direct memory access or dma direct voltage output is the simplest form vote put on the DAC it simply converts the 8-Bit value that you present to the DAC to an analog voltage every time that the DAC is called this voltage will remain on the output Channel until the next call using the Arduino IDE we can use the DAC right and DAC disable functions in order to control the DAC in direct voltage output mode the esp32 DAC has an internal cosine wave generator there's only one of them and its output can be used by either channel the user can control the frequency amplitude and phase of the wave the clock source for the cosine wave generator is the esp32's real-time clock another method about putting to the DAC is to use the dma or direct memory access buffer there are three methods of use normal writing or synchronous writing takes the output of the buffer and sends it to the DAC and continues to do that with cyclical writing the data in the buffer is looped and repeated this is often used for generating waveforms asynchronous writing requires an external callback signal so you can synchronize the output of the dma and the DAC the output of the esp32 DAC is referenced to the a ref for analog reference pin this is the same pin used to reference the analog to digital converters in the esp32 now many esp32 boards don't expose the a ref pin without using a ref the default reference voltage is a 3.3 volt power supply in the esp32 now the output of the esp32 DAC is fairly linear as this chart will show however the chart also shows the output does not reach zero when the input is zero and it does not achieve the a ref voltage when the input is 255. this is a known limitation of the digital to analog converters in the esp32 but despite those limitations the DAC and the esp32 can open up a number of different applications and so let's start working with the esp32 DAC so now that we know a little bit more about the esp32 DAC it's time to start working with it so what we're going to do now is a number of different experiments with the DAC just to get a handle on how to use it I'm going to show you how to make output voltages at specific levels after that we'll look at a number of different ways to make waveforms including using an external library now in order to do this you're going to need just an esp32 and a little bit of test equipment so let's go and take a look at those requirements now for all of our experiments we will of course require an esp32 module and you could pretty well use any esp32 module that you happen to have on that module we're going to be interested in three pins channel one of the DAC which is on gpio pin 25 Channel 2 of the DAC which is on gpio pin 26 and we'll also need one of the many ground connections on the esp32 module now for our experiments today we'll also require some test equipment specifically we're going to require a voltmeter or multimeter it would be very handy if you had an oscilloscope that had at least two channels now if you don't have an oscilloscope you can do some of the experiments using an audio amplifier module instead now for our first experiment we're going to just be using our multimeter or volt meter and the connection is very simple we'll connect the negative side of our volt meter to the ground connection on the esp32 the positive side of the voltmeter will be connected to the channel 1 output on gpio pin 25. keep in mind we're going to be measuring voltages between 0 and 3.3 volts so set your multimeter or volt meter accordingly and now let's take a look at some code that we can use to run our first experiment we'll begin our work with the esp32 dock by doing a very simple sketch that will just output various voltages to one of the DAC pins now we start off our sketch by defining those DAC pins so we do that over here and we're actually only going to be using one of them in this sketch in this setup we're going to set up our serial Monitor and then we go into the loop and basically we're just using one function over and over again the DAC right function and if you look at the DAC right function you'll see it has two parameters it has the parameter for the pin and a parameter for the value so the pin we are giving as DAC channel one which is pin 25 and we're giving it a value and in this case we're giving it a value of zero then we'll print to the serial monitor what that value is and we'll hold it there for about three seconds and we just continue to do that and change the values and so these are the values that we're passing to the DAC pin and they range from 0 to 255 so I'm doing it in increments of 64. you can add extra ones in here if you wish so we go 64 128 192 and then a final one at 255. so in 30 the one at zero should produce no outputs to produce zero volts whereas the one at 255 should produce our reference voltage and since I'm not using an external reference that could be the 3.3 volts but we'll see what it actually does when we run it and look at it on a multimeter and so here's our DAC experiment running right now and as you can see with a DAC value of zero we actually didn't get zero volts and so watch the multimeter as you watch the serial Monitor and you can see it climbing now we're at 255 and we also didn't get 3.3 volts we got 3.1 something and here yes at zero you might have seen 0.095 volts so a 0 isn't quite down at zero volts and 255 isn't quite up at 3.2 volts there it is 3.173 and this is a known issue with the esp32 DAC and so you'll need to consider that if you're trying to give precise output voltages but otherwise as you can see it's pretty consistent with the voltages that it gives out and it does indeed correspond to the numbers that we are feeding into the DAC for a DAC value for the waveform experiment we are going to require an esp32 and ideally we'll need an oscilloscope if you don't have an oscilloscope or if you want to augment the experiment you could also use an audio amplifier because all of the waveforms are going to be generating are within audio frequencies if you're using the oscilloscope you can connect the ground of the oscilloscope probe to a ground on the esp32 connect the input of the oscilloscope probe the esp32 PIN gpio25 if you're using the audio amplifier you can connect its input to gpio pin 25 and its ground to the esp32 ground if you are using the audio amplifier you want to make sure you have a way of turning down the volume or turning it off very quickly because these tones could get rather monotonous now let's go and take a look at some code we can use to generate waveforms using the esp32 DAC now for our next experiment we're going to generate a very simple waveform and what we're going to generate is a sine wave now the method I'm going to show you really isn't the best method of doing this because you really have no control over the frequency of the sine wave but it does illustrate one way of doing it and we're simply going to be outputting to the DAC as we did before but we're going to constantly change the voltage levels throughout all of the positions in the sine wave so we're going to start off again by defining the DAC pins and once again I'm really only using one of them there's nothing in the setup so everything is down here in the loop and basically it's all in this for Loop over here we Define an integer called deg which means degree and it can go from 0 to 360 and we're going to increment it in steps of one and then for each one of these steps we're going to do a DAC right so we're going to write to our DAC Channel and we're going to take the integer value of this formula over here which calculates our position on the sine wave and will discontinue to write that to the output and we'll look at that output on our oscilloscope and even listen to it with our audio amplifier now here's how I've hooked up my esp32 in a subtle as breadboard to an amplifier and a speaker this is actually a stereo amplifier module but I'm using just one channel of it and I currently have the speaker disconnected because it is creating a tone and it can get a little bit annoying so if we take a look at the scope we can see that we definitely have a sine wave and my scope is telling me that I've got a frequency of 86.2 Hertz so it's a fairly low frequency sine wave so I'll hook up the speaker so we can take a listen to that and of course at 86 Hertz this little half inch speaker that I've got over here is having some difficulty playing it it would have sounded a lot better with a subwoofer or something but um basically it is working it is giving us a sine wave and uh we've got what we wanted as an output but of course we didn't have much control over the frequency or anything now I'm going to show you another way that we can create a waveform with our DAC and we're going to make a sine wave this time but you could actually make just about any type of waveform you want including very complex ones and this method involves using a table now we're going to start by defining the DAC pins as we always did and I'm just using the first one and then we're going to define the number of samples that we're going to use and I'm using 112 samples and the sample is a specific point in the waveform and we'll also Define an integer to count those samples and I'm defining this one called I now here's an array with all of the samples in it so there are 112 entries in this array and each one of these are entries which is in hexadecimal is a value for a point on the waveform so we're dividing our waveform into 112 different points and you could change the number over here to more or less different points as you want and of course you could change these values to make a complicated waveform now we're not really again controlling frequency over here this is controlling the waveform nothing in the setup and in the loop we are simply going to use our DAC right command to our DAC Channel and we're going to take a value from that table so we're going to take a value from that array and write it out to the DAC then we're going to increment the counter and go back through the loop and do it again now if the counter ends up being the number of samples which in our case is 112 then we're going to reset it to zero and so it's a very simple method of generating a complex waveform let's go and take a look at it and so here's the demonstration using the table method of creating a waveform and once again we've created the sine wave this time our frequency is a bit higher it's hovering around 470 480 Hertz it's varying slightly we can listen to it if we want to we'll put the amplifier on and it's a bit more pleasant to tell them than the last one uh kind of like the sound you might hear from your smoke detector so I should probably turn it off before people start evacuating but it does work and of course using the table method we could create pretty well any shape of waveform that we want but we still don't have a lot of control over the frequency we could control the frequency by reducing or increasing the number of elements in the table but as we're going to see there are better ways of doing this now so far we've been able to create waveforms with our DAC using the direct voltage method essentially we're just continually writing to the DAC giving it a voltage value and it's changing that and creating a waveform but as we've seen it's a bit restrictive we can't really have much control over the frequency of the waveform using the method we had and as for frequency we can only do fairly low frequency waves now as you'll recall from the introduction there are actually three methods that the esp32 can output on its DAC the direct voltage that we've already used The Continuous output by dma and the cosine wave generator and to use the latter two it is a lot easier to use a library than to write the code directly now there are a few libraries that are available for the esp32 DAC including one that is already built into the esp32 that you get when you download all the board files there was another one that was very popular for all audio work it was by someone called extonical who also has a YouTube channel here however it was a bit of a strange library in the sense that it wasn't on GitHub that wasn't in the Arduino Library manager and the only way you could get it was to download it from the extonical website unfortunately the latest version of the library is broken it won't compile correctly and although there are fixes up on the internet for it the original creator has abandoned this and I don't really think it's a good path going forward so I'm going to use a different library and sadak esp32 library and you can download it right in your library manager just type in DAC esp32 and install it so once you've installed it we can start to use it and it allows you to use among other things a cosine wave generator and it makes it very simple to do so so first of all we'll include the library and then we create a DAC object and notice that we don't need to define a pin this time because it already has these enumerators that are predefined with the DAC pins so I'm just going to create an object that I'm going to call a DAC one I could call it anything I want but dac1 is channel one on gpio number 25. now in the setup we're going to Output a cosine wave with a frequency of one kilohertz and as you can see it's very simple we'll just do an output CW cosine wave and then the parameter which is the frequency of the cosine wave we're going to hold that for five seconds and then we're going to go into our Loop and in our loop we're just going to continue to Output the waveform but we're going to change the amplitude every second and so for we're going to use a for Loop delay by one second and then go into the loop and check the value if the value is zero we're going to Output the full amplitude with the scale one if it's one we'll do scale two which is lower amplitude there are four different amplitudes over here so this will change the level of the waveform continuously so what we could observe is that we will see or if using the audio amplifier here a one kilohertz tone at full amplitude for five seconds and then we'll start to reduce the level of that tone and then we'll repeat that and go up to the top and reduce the level again so let's output this to our board and take a look and I'll listen to it now here's our one kilohertz tone demonstration using the ESP DAC library and as you can see it is changing in amplitude it's already been through the first part of the sequence where it just gives a full amplitude signal for about five seconds and you can see on the scope it's basically doing exactly what you expected to do and it creates a fairly nice sine wave now I'll connect the speaker up to our little amplifier setup [Music] and you can hear that the tone varies in amplitude and we'll disconnect this annoying sound and consider that to be a good demonstration so now that we've seen how to create voltages and waveforms with our esp32 DAC let's have a bit of fun with it what we're going to do next is create some oscilloscope art now obviously for this next experiment you're going to need to use an oscilloscope and so you'll need to have a scope that has at least two input channels we're also going to need to set up our scope in what's called X Y mode where one channel modulates the x-axis and the other channel modulates the y-axis so let me show you how I set my scope up we'll show you the very simple hookup for this and then I'll show you some code that you can use to produce a very simple piece of oscilloscope art now in order to run the oscilloscope Arch we're going to need to put the scope in an X Y mode what this means is that one channel is going to modulate the x-axis of the scope and the other channel is going to modulate the y-axis of the scope and now of course if you have a different oscilloscope than I do the method of doing this will be different but pretty well every oscilloscope can do it now if you do have the Regal ds1054 like I do you can do it as follows you hit this menu button and then where it says time base you press the menu over here and you've got a different three different selections uh YT which means Y is this the time signal X Y which is the mode we want and roll mode so you move that down and press this in to select it and now we are in X Y mode on our oscilloscope and we can do our oscilloscope art for our oscilloscope art experiment we will of course need an esp32 module and naturally we're going to need an oscilloscope and the scope needs to have at least two channels connect the probe to Channel A of the scope and another probe to channel B on the probe for channel a connect the ground to the esp32 ground and connect the scope input to gpio pin 25 on the esp32 now on scope probe number two you can connect its ground to the dsp32 ground however since you've already grounded the other probe this connection really isn't necessary the input connection of the B probe will be connected to gpio 26. and this completes the connections now let's go and make some art with our esp32 so here's our oscilloscope art demonstration and this demonstration code actually was not mine it originated by a fellow named bit Looney and you can go to his GitHub repository he also has a YouTube channel and on his YouTube channel he's shown you a number of different ways of not only doing scope art but actually connecting a video camera and displaying it on the scope so you might want to check that out at any rate it uses the built-in DAC library or driver that came with the esp32 as well as the math library and so we're going to include those at the beginning and also Define a float that's used as an increment within the program now in the setup we go in and we use a DAC output enable and this is a function again internally included with the esp32 and we're going to enable both of the DAC channels because we're going to be using both outputs for this experiment now what we are going to do to generate our waveform is we're going to create a sine wave on one output and the cosine wave on the other one so they'll be offset in Phase from each other and we're going to use the scope as I showed you earlier in an X Y mode and so one candle is going to modulate the x-axis and the other one is going to modulate the y-axis we're also going to Define a variable that we increment by and it's a very slow short increment here of .01 and then we're going to step through everything we're going to Output a sine wave on Channel 1 and a cosine wave on channel 2. we're going to step through an entire circle and do that and after we step through the entire circle we are going to send each output High very briefly for about 10 milliseconds and the reason we're going to do that is to provide a trigger for the oscilloscope so we're going to do this on both of the channels it also illustrates how you can just do an output voltage directly to the DAC using this method and so let's load this up and take a look at our art on the oscilloscope and so here we are our soloscope art and as we can see we've got an ellipse that's moving around and of course what's happening is that we have the scope in X Y mode and we are modulating the X Channel with a sine wave and the Y Channel with a cosine wave and this is the result over here and you can even change the shape of the ellipse by changing the sensitivity on one channel versus the other channel and of course it's a pretty neat little display so if you're creating some sort of a 50s sci-fi movie type thing and you need something in the background of the mad scientist Library you might want to give this one a try now when you hear somebody tell you that they're going to be making music with an apple you probably assume that they're using a Mac or an iPad and some software such as Logic Pro or GarageBand but we could use an esp32 to make music with an apple and this is a rather scrawny Granny Smith apple I actually had some very nice Macintosh apples ready to do this experiment with but because of a Communications error they became applesauce so I'm going to be using these and a couple of oranges with my esp32 the esp32 DAC and another feature of the esp32 which is its touch switch capability so let's go and take a look at how we hook up our fruit basket to an esp32 and make music out of it now here's the hook up for our Musical Fruit and it's actually pretty simple we're going to start of course with an esp32 module and you could pretty well use any esp32 device now I'm also using a potentiometer as a volume control for the output now if the amplifier that you're using already has a volume control you won't need to use this component but otherwise I'd use a higher value something in the 50 to 100K region and if you happen to have an audio taper one that would work great but I used a linear taper pot and it seemed to work just fine and you will of course need an audio amplifier this can be a separate unit or it could just be a little module like I'm using on the breadboard and we will of course need our touch switches now I'm using oranges and apples but you can pretty well use anything conductive that you could touch a piece of aluminum foil would work great as well now we're going to begin by connecting dpio pin 25 which is DAC channel number one to one side of the potentiometer the other side of the pot we're going to be connecting to the esp32 ground then we'll take the wiper of the pot and connect it to the amplifier's input again if you're not using the potentiometer you can disconnect gpio pin 25 directly to the amplifier input in either case we'll also connect the ground of the amplifier to the ground of the esp32 now we'll start wiring our sensors sensor number one will go to gpio pin 32 Sensor 2 to gpio pin 33 sensor 3 to gpio 27 sensor 4 will go to gp14 sensor 5 will go to 12. sensor 6 to 13 sensor 7 to 15 and sensor 8 will go to gpio pin 4. and this completes the wiring of our Musical Fruit now let's take a look at the code that we can use to start making music now here's the sketch that we're going to be using for our Musical Fruit and it's actually a pretty simple sketch once again I've based it around the DAC esp32 Library so we're going to start by including that library and creating a object that represents the DAC on channel one which is on gpio pin 25. now we're going to define the touch pins that I use and I should note that the definitions I use Touch One Touch two Etc are my own definitions the esp32 does have official designations for each one of those pins and they're similar designations but they don't match these ones and so don't get the too confused now I've used eight of the touch switches but there are 10 and you could of course use the other two you'll just have to expand the code accordingly now I've got some variables that hold the values that are going to come back from these touch pins the way the touch switches work is that they'll deliver back a value from 0 to 255 and a high value means they're not being touched in a low value you means that they are being touched now for that we Define a threshold for each one of the pins and that's the threshold between being touched and not being touched and this can change depending on the touch surface so I've got eight different variables for that one for each one of the switches and you can start off with a value of about 150 or 200 I found that worked with most of them but you might need to alter these we're going to print the values to the serial monitor so you can get an idea if you need to change them now these are the frequencies that we've defined here for our musical notes and I've included the link to a chart that will relate musical notes to frequency if you want to change these now in the setup as I said we're going to use the serial monitor for Diagnostic purposes so we'll set that up I'm going to start off by disabling our DAC now we'll go into the loop I'm going to check the status of each of the touch switches which is just done with the touch read function it's a very simple function the only parameter is the pin that you want and it'll give back a value of 0 to 250 5 and then we're going to print those values to the serial Monitor and as I said that's so you can adjust and fine tune the threshold if you find that you have an apple that's a little bit out of tolerance or perhaps you're using a banana or a pear now we're going to go and do a bunch of is if else's and we're going to check the touch switch value and compare it to that threshold and if the value is lower than the threshold then we're going to enable the DAC and output the frequency for that particular switch and so we're going to Output a sine wave at that frequency and we go through all of that at the end of it if we find that nothing's been pressed then we disable the DAC we put a short delay on it and we do it over and over so it's a pretty simple sketch let's load it up and let's go and play our Musical Fruit and so ladies and gentlemen we are here for the premiere of the dronebot workshop Orchestra starring a number of small Granny Smith apples and a few oranges now now I've got the esp32 over here the amplifier module here it's a stereo module but I'm just using one channel and the potentiometer that I'm using is here on the board and that just is a volume control for it and of course I've got a speaker back here and so without further but do let's go and see if we can make some music with this fruit [Music] and it works pretty good you'll notice a couple of times when I pressed it it went on and off and that can be fixed by looking in your serial monitor at the different numbers that you're getting and adjusting the threshold accordingly because of course I've all got mine set to the same number but you can't expect every apple and every orange to have the same conductance and capacitance qualities to it but all in all if you've ever wanted to play an instrument and eat it later this is the project for you well that about wraps up our look at the esp32 DAC I hope that it's opened up your eyes to the fact that the esp32 has a lot of other features other than just Bluetooth and Wi-Fi it's not just a pretty face now if you want to get the code that I use today or if you want some more information about the DAC you'll find all of that in the article that accompanies this video on the dronebotworkshop.com website and there's a link right below the video to that article there's also a link below the video to sign up for the newsletter so if you haven't done that please consider doing it it's not a sales letter it's just my way of keeping in touch with you and letting you know what's going on here in the workshop now if you want to discuss the DAC further if you want to show off your Musical Fruit examples you can do that on the dronebot workshop Forum it's a great place to discuss Electronics with a number of like-minded people and of course you can join the forum for free and finally if you haven't please subscribe to the YouTube channel I make videos about electronics and my microcontrollers I'd love to have you as a subscriber all you need to do is hit that red subscribe button and also click on that Bell notification and assuming you've enabled notifications on YouTube you'll get notified every time that I make a new video now before we go today I want to leave you with a little bit more Musical Fruit so we're going to class it up a little bit we're going to play a little bit of Mozart with our apples and oranges so let's take a listen to that take care of yourself and I'll see you again very soon here in the dronebot workshop goodbye for now foreign [Music] [Music] bye [Music]
Info
Channel: DroneBot Workshop
Views: 55,203
Rating: undefined out of 5
Keywords: esp32, esp32 dac, digital to analog converter, musical fruit, dronebot workshop, dronebot workshop esp32
Id: -7FosQmioRY
Channel Id: undefined
Length: 34min 46sec (2086 seconds)
Published: Sun Jan 08 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.