(soft music)
- Welcome, and today we are going to be talking about
pulse width modulation, or PWM.
Now, the reason we're doing this is because
we have referred to pulse width modulation
in quite a few of our tutorials.
We've done it in our LED, like dimming an LED.
We've talked about creating one
with our PIC10F200 in that series,
and also doing it in the Astable mode of our 555 timers,
which is another tutorial series
that we've just recently finished.
So, we did all those things
and just made the assumption that people knew what PWMs are
and we've gotten some feedback that not everybody does.
And frankly, PWMs are quite simple,
but if you don't know what it is, you don't know what it is.
So we're just gonna go over it really quick,
discuss some of the different concepts behind it,
some of the things to watch out for.
It's gonna be straightforward,
and by the time we're done with this, you're gonna be a pro,
and it's not gonna be anything to worry about.
So, let's get started.
So, pulse width modulation is basically
taking your typical square wave
and taking the width of those high pulses,
and making them either longer or shorter
as a percentage of the overall pulse.
Overall pulse chain.
There we go.
So, all it is, is you can have a square wave
of anything from zero to five volts, is very, very common.
It could be zero to 3.3, it could be zero to one,
it could be zero to 200, it doesn't really matter.
It's just the concept of you're going from one voltage,
up to another voltage, and then back down.
And I'm even saying from zero to something,
typically, they start at zero, that's the low,
but you can even have negative voltages
with your square wave and with your pulse width modulation.
But typically, it's zero to another voltage.
And pulse width modulation is simply modulating
or changing the width of that pulse,
that high, where it goes from zero
up to whatever voltage it is, and then back down.
Now, the way this is measured is called the duty cycle,
or the percentage of which it's high, versus low.
So, if you have something and it's going cross
up, down, up, down, you take one of those cycles,
where you go up, down,
and then right before it goes back up, that's your period.
And whatever percentage of that period is high,
is considered your duty cycle.
So, a 50% duty cycle would be literally,
high, half the time, low, half the time,
high, half the time, low, half the time.
If you have 20% duty cycle, exact same idea.
High, 20% of the time, low, 80% of the time,
high, 20% of the time, low, 80% of the time.
And that's really it.
And that concept of duty cycle, its fundamental.
It's actually the biggest thing
that you need to worry about with pulse width modulation,
because that's where you get the communication,
that's where you get that percentage.
Now, where you'd want to use this is when you want to create
kind of an average voltage easily.
So, in the world they have DACs,
DACs, digital to analog converters,
and that will make it, so if you say,
I have my microcontroller, or my IC, or whatever,
and it has zero to 10 volts in,
but I want to get 3.25 volts out,
you can use a DAC and say, output 3.25 volts,
and it can do it, it'll be great.
And you can even say, hey, I want a sine wave,
and it'll make a very nice sine wave.
But DACs are expensive, relatively.
Relatively expensive, relatively difficult to set up,
and a lot of the times you can just fake it
by creating something that averages 3.25 volts in this case.
And so, what you would do is, if you had,
again, a zero to 10 volt...
Excuse me, a zero to 10 volt pulse that you had,
then you can simply say, well,
if I want to have an average of 3.25 volts,
I would create a pulse width modulated signal
that is high 32.5% of the time.
And then over time the average will be 3.25 volts.
And it's a lot easier than using a DAC.
But it obviously has some drawbacks,
if you ever think about it like, well,
what if I need a very clean signal
where I need exactly 3.25?
This isn't going to work.
If you throw in a resistor and a capacitor in there,
and you don't have too big of a load, you might be able
to finagle it with a PWM, or you might not.
Your mileage may vary depending on how you set it up.
But for a lot of things where it's more hand wavy,
or whatever, this is a great simple way of doing it.
So, the only other thing
that we really need to think about this,
is we've been talking about that period,
and we've been talking about the percentage,
but we also need to know the frequency
of the signal on the first place.
So, let's just say that we have a
signal that goes high and goes low,
and the period is five seconds.
That's not gonna average very well.
That's not gonna be something that you really
get a clean signal out, because it's gonna be high
for two and a half seconds,
and low for two and a half seconds.
But if your frequency is more like 1,000 hertz,
and it's high for one millisecond,
low for a millisecond, or two and a half,
or something along those lines, if it's something shorter,
then the average works out better.
So, in particular, when you're dealing with LEDs,
and you're using pulse width modulation
to give them a different average voltage,
it actually really matters for our eyes,
because if you do anything slower than,
let's say, 50 to 60 hertz,
you're not going to really like it.
Instead of it being dimmer,
it's gonna flash on and off, flash on and off.
And so, a lot of the times
when you are controlling the dimness of LEDs,
and you have a pulse width modulated signal,
you will want to make sure your frequency
is probably at least 100 hertz,
so that you cannot see the individual flashes,
but your eye will just see the average dimness go down
as you decrease the width of that pulse
width modulated signal.
So, that is another thing to think about,
is that frequency does matter.
Too long of a period, and it's not gonna average very well.
You're gonna see it, your circuit's gonna
respond to it differently.
On the flip side, if you go too high of a frequency,
one, it's gonna be a lot harder to generate.
You might run into the limits of your microcontroller,
or your IC that you're using.
And also it might start creating
a little bit more spurious like EMIs,
so you might be producing a little bit more
electrical noise than you want.
Okay, so now we've talked about it conceptually,
we've kind of got the idea.
I want to just show you really quick.
I'm gonna use the waveform generator and the oscilloscope,
and just hook them up directly.
And we're just gonna talk about those things
that I've already discussed with this visual representation.
Again, this is pretty straightforward,
but it's always fun to see something in real life.
So, let's jump into that really quick,
and then we'll wrap this up.
So, here we have the input, and so this is just
being generated by my waveform generator,
but we can look at the things
that I've already discussed here.
We have our frequency, you can see down here,
that it's being measured at about
1,000 hertz, one kilohertz.
Our divisions are two volts, right here.
So, we're showing that we're doing
zero to five volts up here.
And right now you can just visually tell
that this is about a 50% duty cycle.
So, it is high half the time, and then low half the time.
So, we're about 500 microseconds,
and then low for about 500 microseconds,
to give us that one millisecond period.
Okay, you'll also notice that this is going from zero,
not from a negative number, and that required me to do
a dc offset on my waveform generator.
But this is more generally what a wave pulse
modulated signal looks like.
Pulse width modulated signal.
Very quickly, as you can see with this 20% duty cycle,
we have very, very quickly gone from 50%,
where our average voltage is 2.5 volts
with this zero to five dc, to dropping it down to 20%.
And now our average voltage will be one volt,
because it is only going up 20% of this five volt signal.
So that's the biggest thing that you need to worry about,
is again, the duty cycle, but there's also the frequency.
So, I can go up and I can change my frequency,
and let's just bump it up to two kilohertz,
or I can drop it down to, If I can,
drop it down to 500 hertz.
So, something along those lines,
as you change the frequency
it will change how often it happens.
But notice that as I make the frequency higher,
it's still a percentage based change.
So at one kilohertz, this was every 200 microseconds,
and now that I'm at 3.3 kilohertz, rather randomly,
it's looking like it's closer to
70 to 80 microseconds for that high pulse,
because the basis of pulse width modulation
is the percentage of the high, versus the low.
Okay, and that's really about all you need to worry about
in terms of a pulse width modulated signal.
Anything more than this, and you're getting into the weeds.
Like 95% of the applications you deal with
this is really all you need to worry about.
So that's about it.
You can really use it to create
that percentage of a voltage, or when you're using LEDs,
or if you're creating a power supply, you can use a
power supply that's called a switch mode power supply.
It's based basically on this concept,
but much, much, more complicated,
and it makes things a lot simpler.
Now, there are other times where you want to use this,
that it's not a voltage percentage that you're after.
Sometimes you want to control a servo motor.
So the funny thing is if you use a DC motor
you can control the speed by just saying,
well, I'm giving it five volts,
I want it to go half as fast, give it 2.5 volts,
something along those lines, and you can control that speed.
But, with a servo motor you actually need to have
a very specific frequency, and then it's that percentage
that that servo motor looks at, and says,
oh, I'm getting, I think it's around 10 or 12%.
I don't recall off the top of my head.
It'll say, I need to be in this position.
Oh, okay, now it's down to 8%,
I need to be in this position.
Oh, I'm up to 15, go to that position.
Something along those lines.
So besides varying the voltage
or giving a fake average voltage,
it can be used for communication with simple ICs,
or simple sensors, or things along those lines.
So, that's one of the great things about
pulse width modulation is, it's pretty powerful
in different applications.
So, besides those applications,
you can also get some audio effects,
or other things along those lines.
It's really, truly a wide ranging variety of applications
that you can use with pulse width modulation,
just by changing that percentage, changing the frequency,
and changing the peak to peak voltage.
And you can do this with, obviously,
a waveform generator, if you have one sitting around,
but you don't wanna use that in any application.
This is purely to show things.
In reality, you can use a microcontroller,
like I mentioned earlier, we talked about it,
because you can create a pulse width modulated signal
using a PIC10F200 in one of the tutorials we did.
Even more easily, you could just use an Arduino,
just jump in, and I think it's something
where you write analogWrite().
You put in the percentage, and, voila, it makes it for you.
And then finally, if you want to set up
a 555 timer in Astable mode, you can do that.
So you have, basically, a hardware
versus a software generated pulse width modulated signal.
Very easy to create, very easy to understand,
very easy to use, these things are all over the place.
And I hope that this has been helpful for you to understand,
one, why we have them,
two, what's important about them,
and three, where you can use them.
And if it did prove to be helpful in any way,
please give this video a like, subscribe to our channel,
and we will catch you in the next one.
Take care.
(toaster creaking)