arduino 0-5v output
What we’re exactly what we’re gon na do in this video. I know I mentioned Arduino, even if you’re not using Arduino are not familiar with Arduino. This video will be useful for you as well, if you’re just trying to take a PWM on a different type of microcontroller and use it as a DAC output. Of course, though, the I want to show some code examples, that’ll be Arduino specific in this video anyway, we’re gon na do exactly that. Show you how to use a PWM signal as an analog signal or change it to an analog signal. I should say – and if you look at my picture, we’re actually going to do this we’re gon na create a analog sine wave from a PWM signal. As an example, if you haven’t already check out the force Tronics comm website, I recently dropped the price on the Arduino Uno and the maker 1000 that I sell at my site. I cut the prices in half, so that’s gon na be a big sale. That’S going on for a while also check out for Sonic’s comm for for Stronach services, if you’re interested in that check me out on Facebook and Twitter, and if you like, what you see here subscribe to my youtube channel let’s get started: ok, so I’m gon na Start off with a little bit of theory and I’m gon na go through this pretty slowly and we’re not going to get deep into the math.
But I think this is important for understanding how we’re gon na turn the pulse width modulated signal into an analog DC type signal and I’ll. Tell you right away. It’S gon na involve creating a filter which will look at what is a pulse width modulated signal. Well, any waveform or any signal can be represented either as DC or as a summation of different sine waves and for a pulse width, modulated signal, which is essentially a square wave signal right. You have both DC elements and then you have sine wave elements and if you look at the picture on the left, you can kind of see how, from a fundamental sine wave frequency, we can add more sine waves and it starts to look like a square wave. So, for instance, for a square wave, we have our DC component right and then we have our first main frequency, and this main frequency is going to be for our pulse width, modulated signal it’s, going to be whatever the pulse width. Modulation signal frequency is so for, for instance, if you use the analog write function for Arduino, I believe the default frequency is about 500 Hertz. So if that’s the case, that means our pulse width modulated signal would be DC plus 500 Hertz. This would be the 500 Hertz Center frequency, then from there. The way you get a square wave is, you add, odd, harmonics at lower amplitudes, so we have our DC, we have our sine and then we would add a so if this is 500 Hertz.
This would be 1500 Hertz right and then this would be 2500 Hertz, so it’s the odd harmonics going out and now if we had a perfect square wave which there’s no such thing as a perfect square wave. But if we did have one, this would just go for an infinite right. You would just keep adding these odd harmonics sine waves. You might be saying, okay, why do we care about this theory? Well, we need to know this frequency as well as these other frequencies, to know what value to use for our filter, because we don’t want to know what frequencies we want to attenuate with our filter and that’s. What we’re going to talk about and it’s important? To note the way we make an analog signal is essentially by filtering out all the sine waves and just having the DC component, and the DC component varies based on the pulse width, modulation right, the the higher the duty cycle of the on time, the higher the Dc is going to be and then lower, vice versa, so that’s what we’re doing we’re trying to get this DC value to create an analog signal from our pulse width, modulated signal, okay, and to do that. We’Re gon na use a sail and key filter and we’re gon na use an active filter. I know, if you look online, a lot of people will show the you know the single pole, resistor and capacitor passive filter on pulse width, modulated, Six’s, pulse width, modulated signals.
You can definitely do that, but it’s not a very effective. First of all it’s, not a strong attenuation, so you’re still gon na have a you know: it’s not going to be a very clean DC signal at all and then, if you’re, using a passive filter. You also are going to attenuate a little bit of your DC as well. If we use an active, an active filter because we have an amplifier in here, we can get a much better attenuation of the frequencies. We don’t want, as well as a much reliable, more reliable DC output, and so this is a salient key filter. This is an op amp I’m, going to make an assumption that you have a basic understanding of op amps, these resistors and capacitors. I used a filter. Calculator for that I’ll show you in a second and then what you’re looking at is actually a cutout from a from an L spice model that I did to test this, and I actually have a video on low pass filters. If you want to check that out. For more detail on this and I’m actually going to show the design I used in that video of of low pass filters, I’ll have a link in the video description here but I’m going to use the design that I use for the from that video to do The filtering for my pulse width, modulation, signal to Dax signal, and so I won’t have the parts list in this video, but if you want to check that out, I’ll provide a link in the description of my other in the description section of this video.
Ok. So here is the sale and key filter calculator that I used. If you just search sailing key filter calculator, this will be the page that comes up and basically what you do is I’ve already done it. But I’ve entered my resistor values and my capacitor values to make this this active filter and this equals a cut off frequency of about a hundred Hertz. So that means around this frequency. The filter will start attenuating sine waves or frequencies or signals higher than that. Okay – and let me show that real, quick actually so this is showing the roll off of the filter. So right here is a hundred Hertz, so you can see we’re starting to attenuate at a hundred Hertz and then, if you look all the way to a thousand Hertz, you can see there’s a 40 DB of attenuation and that’s a lot. So, by the time we get to a thousand Hertz, we should pretty much have those signals. Pretty much cut out now I’m, showing you the the calculator that’s already has the result. You can also go back here. You can enter the resistance and the capacitors, but you can also enter just the center frequency. So if I want to enter 100 Hertz it’s going to tell me what capacitors and resistors you want to use so that’s, what I use to get my filter value and you might be asking well, why did you choose 100 Hertz and I’ll talk about that in A second okay, so some important notes.
Why did I choose 100 Hertz? I could have chose 50 Hertz and I would get better attenuation at my center frequency, whatever that is 500 Hertz, a thousand but there’s a trade off right. So the lower the cutoff frequency, the more AC that you’ll attenuate on with your signal, but the lower your cutoff frequency. These you can’t move fast right, because if you try to move too fast, that has frequency elements and you’re gon na attenuate those so the lower the cutoff frequency, the better the clean of the DC output, but the slower you can move your DAC value up and Down and once again the higher the cutoff frequency the faster you can change the analog output. But you have to deal with the fact that if you have AC Center frequency that gets close to the cutoff frequency, you’re gon na have a lot a strong AC element on your DAC signal output. So why is this important? Because, based on your application, how fast you got to move your signal and based on how fast your pulse, width, modulation frequency is that helps us balance? What our cutoff frequency is. So one thing we can say right away, is it’s better for the pulse width. Modulation signal to have a higher frequency, because that gives us a longer larger range for choosing our cutoff frequency of our filter. So one thing I said earlier is the Arduino AVR boards, their default pulse width.
Modulation signal is about 500 Hertz well there’s, ways to turn that that frequency up, which I’ll show you and if we do, that we get more flexibility in our cutoff frequency let’s. Look at an example of this here is an example on an oscilloscope where I captured. First, the pulse width, modulation signal and then I ran the signal through our filter that we showed with a hundred Hertz cutoff and we got our DAC signal on the output. So you can see our pulse width. Modulation signal looks like a regular pulse width. Modulation signal this is the default value of 500 Hertz, which is the default value for Arduino, and you can see this is a DC signal right. This is two volt. This is channel 2, so I’m running this at a 50 duty cycle, 5 volts. So what do we expect to see? Well, this is 1 volt per division, so we see about 2 and a half if we cut straight through this, we see about 200 half volts, which is what we should have right because we’re cutting it in half. We have a 50 duty cycle now. We can also see the fundamental frequency of 500 Hertz. You can see the sine wave remember. The first signal element in a square wave is the center frequency a sine wave representing the center frequency of the square wave that’s? What we’re seeing on top of this – and we can see we have – I don’t – know about 800 millivolt peak to peak now for some applications.
This might be fine, but for others this might be too much of an AC signal on our DAC. So how do we prevent this? Well, if we turn up our pulse width, modulation, signal I’m, still using the same filter and the same hardware. All I did was turn up. My pulse width, modulation signal to 331 kilohertz, so here it is and all of a sudden, our DAC signal looks much cleaner. We can still see some noise on it, but it basically looks like a DC level here and once again, one volt per division. So we see about two and a half volts that’s what we expect to see right. So here we just changed our pulse width modulation signal into a clean DC signal. Next let’s look at actually creating a dynamic DAC signal where we’re constantly moving it up and down, and we can see some of the trade offs associated with that. Actually, before that, let me show you my Arduino code and then we’ll, look at the video with a more dynamic example using the filter. Okay, here is my Arduino code. Basically, I declare some variables up here, here’s, what I’m gon na set my pulse width, modulation example signal for – and this is the one you saw earlier – 127, which is essentially a 50 duty cycle. Here is PI times 2, because we need this for doing art, we’re gon na do what I should mention. We’Re gon na do a sine wave example with our DAC we’re gon na turn our DAC output into a sine wave, and so I need pi we’re gon na use 100 samples to do it.
Here’S the array we’re going to serve our store, our samples in – and this is a count for just just executing the waveform that you’ll see later so I’m. Creating two pulse width. Modulation signals one on pin 10. This is just going to be the standard one that you solved with a 50 duty cycle and then one on pin four, and this is what we’re going to use to do our sine wave. This function right here, I’ll show it later. I got this from the Arduino website, but it’s it’s function that allows you to change the frequency of the pulse with modulation, signal and I’ll show you that function later, but basically this represents pin 10 and then one means set for the highest value possible and that’s. What we saw 31 kilohertz and then they have a bunch of other settings. You can do whether it’s 500 Hertz and a bunch of values in between. Then I do the analog right. No, actually, I was testing earlier and I did a 0 value, but, for example, I was using 127 now these variables I’m going to use for building the sine wave and I’m not going to go into the math of a sine wave. But the idea is here’s. Where I’m building my sine wave and I do a for loop to do a hundred samples – and this is where I’m storing the waveform – and so I do. First, my internal calculation and I equal it to the variable N and then I feed in into the sine function.
Now you might be asking why you’re doing plus a hundred set one hundred twenty seven point: five. The DAC is 8 bit. Excuse me, the pulse width modulation signal for Arduino is eight bit so that’s 255 0 to 255. One twenty seven point: five is half of 255 ourd. We knows can’t put out negative voltage, a sine wave will have a negative and positive element. So what I’m doing is adding this to it, so it raises the sine wave, so it’s never goes below zero. Alright, then, I just do the main loop and I have this function called bit: bang PWM and that’s. What I’m gon na use to print out my waveform samples, so why do I say bit: bang because I’m actually going to make a pulse with modulation signal I’m, not going to use the built in capabilities of Arduino. So what I do for that is I get my samples in I map them to a period of actually. I want this to be a thousand, because this is what I showed in the example I map them to a period of a thousand. So if a thousand I’m using this micros function, so this is really one milli, one millisecond, because I’m using micros and what I do is I say: how long does this pulse with modulation signal need to be on what’s its duty cycle and then I say start A timer go high on the output and wait until the duty cycle is over then start another timer and go low, so I’m.
Creating each pulse with modulation cycle, I wan na, say I’m gon na set by hand, but obviously not by hand by manually, let’s, say manually and that’s going to allow us to do a dynamic change to create a DAC output. Okay. Lastly, this is notes on the function that I pulled from the Arduino website. I forget who made it, but whoever did that great job and they talk about this function and using it, and this is going to allow us to set our pulse width, modulation frequency now, and this is what we’re gon na use on pin 10, and this is What you saw and the pictures that I showed you when you saw the 500 Hertz if we want to do 500 Hertz, we would do 64 or we did. I showed you 31 K, now it’s important to note that if you use this function, other things that Arduino will stop working – and this is what these notes are. Explaining and I’ll have this code on my blog. If you want to cut and paste it, but also here’s, the link to the code on Arduino on Arduino is webpages. Okay, so that was a quick run through of the code. It’Ll have notes, it has notes in it and you can grab it from my website. Alright, now we’re gon na see a video of creating the the sine wave, our DAC output sine wave from a pulse width. Modulation signal. So here is my Arduino Uno that I loaded that code.
You just saw on we’re pulling it from pin 4 remember. I had pin 4 set up to do the dynamic DAC output here’s my filter circuit. Now, if you once again, if you want to see this design, I’ll have a link to the the video in the description section of this video but and you’ll find more information on this. But basically I took off the gain amplifier because I’m not trying to amplify the signal at this point – and here is my filter right here and then I’m measuring it here’s my power supply, this red and black lead, and then I have a scope leads for measuring The output and I have another scope – lead to measure the input, so you can see both of them so there’s my power supply and then I’m gon na go up to the scope and there we are so look at that. So here is the pulse width. Modulation signal – and here is my sine wave and you can see that they get the signal. Obviously I need to make the horizontal display bigger to see every little pulse width. Modulation signal they’re a little too crunched here, but what we can see here, though, is when you have a low and a high. You start to see some daylight here right, because here we have mainly lows: we have a very low high duty cycle and then at the highs we have real long high cycles with no low cycles and that’s.
Why? You can see some daylight here. You can see our signal is about 10 Hertz. It should be 10 Hertz, because if you look at the period settings I have in the the code, you can see how I did the timing, and so basically one complete cycle is about 100 milliseconds, which is 10 Hertz, now we’re gon na see in a second. If I turn up the frequency much higher, the cutoff frequency of my filter can actually attenuate it. So what I’m gon na do here is I’m gon na just this period value. I have it at a thousand right now, which basically equals 10 Hertz for our sine wave I’m gon na lower it to a hundred and that’s, essentially going to increase my frequency by 10. So I’m gon na go from 10 Hertz to 100 Hertz. So what I’m going to do now is I’m gon na upload this to the arduino uno that you saw and then i’m gon na go back. I need to bring up the pulse width, modulation signal in a second there. It is, and so what are we looking at we’re now, looking at a signal? Hmm, this is 60 hertz. I may not have this signal. You don’t know why it’s not picking up that frequency anyway. This is the signal, and one thing I wanted to point out here is: we still should have the same amplitude, but what’s happening here is the filter is actually attenuating this signal, because the DAC frequency is getting too high that the the filters not letting those frequency Elements pass because this is about a hundred Hertz, so our filter is attenuating.
It because remember our filters. Cutoff frequency is a hundred Hertz. So if I wanted to get a hundred Hertz sine wave I’d have to move my cutoff frequency of my filter farther out. So once again, it’s all about this balance of what your filter setting is versus what you can do on the output of your DAC and that’s. One of the points I was trying to hammer home here: okay, that’s it for converting and Arduino pulse width, modulation, output to a DAC output; and if you have any tips or comments to add, use the comment section below the video or, if you have a question From the video use, the comment section below, if you like what you saw here, please subscribe to my youtube channel.
arduino 0-5v output Video
arduino 0-5v output news
Posted on Thursday September 26, 2019Hypnotic Visuals Synthesizer Hackaday … Continue Reading »
Posted on Sunday May 19, 2019New Arduino Nano Line Rolls Out In Four Flavors At Maker Faire Bay Area Hackaday … Continue Reading »
Posted on Thursday September 26, 201926 | September | 2019 Hackaday … Continue Reading »
arduino 0-5v output Social
🔬 Now that you’ve gotten your feet wet, dive into the kit and enjoy all nine experiments. Order yours today: http://bit.ly/2MnQ7fr
Originally posted 2016-08-17 00:15:00.