Introduction
The Celtic Engineer is a weekly newsletter produced by Celtic Engineering Solutions. We hope you enjoy it. If you have any suggestions for topics, would like to give feedback or want your email added to the distribution list please send an email to [email protected].
The Information Age
Sometimes the information age can overload you. Every answer is just a click away and yet we are still flooded with TLA’s, that is Three Letter Acronyms. PWM or Persistent-Widespread-Mischief, no wait, that isn’t right. Perfectly Wonderful Mead, oh while that sounds delicious, that isn’t it either. Rather we are talking about a signal referred to as Pulse Width Modulation.
This signal can do several things. It can convey information by encoding the information into the pulse width, but usually it is used to control the average power sent to a load.
What is my motivation here?
PWM can be used in many applications. It can be used to control the temperature of your oven and thus cook a delicious holiday meal. It can be used to control the current through an LED and so dim the lights for a romantic evening with your significant other. It can be used to position the angle of a servo motor which controls the gas supplied to an engine and so controls the speed of your autonomous car. I could go on and on. There are so many uses for this type of signal.
Just how many variables are we talking about?
As with any technique, there are just so many things, or variables, that can be controlled. In PWM, there are three. They are the amplitude, the frequency and the duty cycle.
Let’s start with frequency. This is like saying, how often are we going to update the information. If you are roasting potatoes in the oven, it is not necessary to update the temperature 5000 times a second because the system is slow and does not respond to such high frequencies. You could still do it, it just doesn’t make sense. More likely you will want to update the oven temperature several times a minute.
On the other hand, if we are going to control the LED’s in your floor lamp, turning them on and off 4 times a minute would be enough to drive you crazy. Instead you might choose 120 Hz. This is fast enough that the humans enjoying their dinner will not actually see the flicker as the light turns on and off. Instead it will be perceived as a lowering of the light level.
If you intend to control the speed of a motor you might choose to update it at a few kilohertz or a few tens of kHz. The frequency of the signal must match the physical system in which you intend to use it.
Its your turn to work
Duty cycle is expressed as a percentage. It refers to the amount of time in a single period when the signal is high (pulse width divided by period, times 100 percent). If something has a duty cycle of 0% it is off, 100% it is full on. Anything else is just a percentage of the full-on power.
In a motor controller, the larger the duty cycle the faster a motor will turn. That is assuming a constant load. If your load varies, you will need to change the duty cycle just to maintain a constant rotational speed. This is a good indication that you need your PWM to be part of a feedback control system.
In a servo motor, like the one that controls your RC plane, or your autonomous car, the pulse width is mapped to a rotational position rather than a rotational speed. While I say rotational, you could easily control a linear actuator using PWM.
Amplitude shmamplitude
The most boring parameter of the PWM is the amplitude. It is crucial to the proper working of a system, but once you figure out the proper amplitude it does not change. So, you could, and often do, produce a PWM signal using a microcontroller. The output of the MCU could be 3.3V, 5V or 2.8V. It might send that information to another MCU, or to the electronics inside of an RC servomotor, or it might be used to turn a transistor on and off so that you can change the amplitude of the signal or change it from a voltage to a current. Actually, that is starting to sound pretty interesting. OK, let’s say it is at least as interesting as the frequency but Duty Cycle is definitely the star of the show.
Just how does one PWM?
There are too many ways to create a PWM signal. By far the easiest way is to use a microcontroller. You can use any kind you would like, even an Arduino will do it for you. Once you choose your MCU you have some options inside of the code. The nicest way is if your MCU has a PWM peripheral built in, and many of them do. In that case, there will be registers that will allow you to set up the frequency and the duty cycle. To change the duty cycle, once it is running, all you do is write a new value to the appropriate register.
If you are stuck with an MCU that does not have such niceties, you can make use of the timer peripheral. Many of these will allow you to flip a bit when the timer counts down (or up) to some limit. It is more work because you have to calculate the time to the next bit flip which changes depending on if you are high or low and what the duty cycle is to be. You might even call an interrupt set for the system frequency and then set the on time of the bit. Have it go off when a timer counts down. This is particularly inviting if you have multiple PWM’s using the same frequency that must be updated.
Don’t like MCU’s? Well you can use a 556 timer, that’s two 555 timers in a single chip. You can also use a ripple counter and an oscillator. Today they even have PWM standalone chips. They have analog, I2C or SPI input controls.
Final thoughts
This newsletter is sponsored by Celtic Engineering Solutions LLC, a design engineering firm based out of West Jordan, Utah, which can be found on the web at: www.celticengineeringsolutions.com. You can find the newsletter on the company blog, LinkedIn or in your inbox by subscribing. Send your emails to The Celtic Engineer at: [email protected], with the subject line SUBSCRIBE.