Pulse width modulation (PWM) explained – how to control electronics with pulses

Veröffentlicht von

Computers speak in simple terms: zeros and ones, on and off. This black and white simplicity may be comforting, but the real world exists in varying shades of gray―or, for humans, intensities of red, green, and blue. As we've discussed, it's possible to create a digital-to-analog converter that varies actual voltage levels based on binary values. When that's not available, another option is to use pulse-width modulation (PWM) to control different types of electronic functions.

Pulse Width Modulation Basics

PWM simulates different voltage outputs via a repeating series of pulses. These pulses occur at a steady rate, but the length of each pulse varies depending on the simulated voltage. We express this variance in pulse length as a PWM 'duty cycle,' which we can establish as a percentage using the following calculation:

pulse time/time period between the start of each pulse

If each pulse time is .005 seconds, and a new pulse starts every .01 seconds, the duty cycle works out to 50%, simulating half the original input voltage level.

Consider, however, that not all PWM is created equal, even within the same controller. For example, Arduino Uno boards have six hardware PWM pins that run at two different frequencies by default. Above a certain threshold, this doesn't matter in many applications. Consider it, though, if you're controlling light for high-speed photography applications, or computer vision where a short blink can throw your measurement off. Interestingly, some animals can resolve individual frames of light faster than humans, so those who work in veterinary sciences or with wildlife populations may be able to find some fascinating PWM applications.

Applications of PWM: Lighting Control

Perhaps the most common usage for PWM is to control lighting. When the human eye sees images moving faster than about 20 Hz, our minds resolve them as one moving image. However, you may be able to see strange artifacts when you move your head or the light quickly, or you may notice flickering when dimmable overhead lighting turns down. From my experimentation, on/off flashes at about 30Hz are noticeable, so this solution isn't entirely perfect.

The film industry has settled on 24 FPS, and TV in the US is generally broadcast at 30 FPS, though whether or not we can use information at higher framerates is an open and sometimes heated debate—especially within the gaming community.


For example, Arduino Uno Boards feature default PWM speedsof 490.20 Hz on Pins D3, D9, D10, and D11; and 976.56 Hz on D5 and D6. This default speed won't make a difference in applications, but here's one situation in which it might:

Image: Jeremy S. Cook

Compare how in both the first image in this article and the one directly above, all the LEDs look the same. Light is sourced, from left to right, at Arduino Uno's D5, 6, 9, and 10 pins at a brightness of 127. This application uses half the intensity in the 0-255 scale. Now look at the image below: two of these lights shine in the same manner with the camera panning rapidly up and down.

The higher-frequency pin lights look like dots, while the lower-frequency pins look like short lines. In theory, if you knew the camera exposure time, you'd be able to calculate the time period of each pulse. Without that specific information, you can still assume that these are PWM lights operating at two different frequencies.

Image: Jeremy S. Cook

Pulse Width Modulation & Video: Use in High-Speed Video Applications

High-speed video (shot at 240 FPS) also reveals evidence of PWM, with a massive rolling shutter effect on the two slower lights (490.2 Hz) to the right, while the faster lights on the left (976.56 Hz) remain stable. Note how the lights on the right appear to pulse much more brightly than those on the left at times. The difference is due to the number of light pulses per frame, something humans—or a regular camera—would not normally pick up.

Image: Jeremy S. Cook

Custom Pulse Width Modulation Frequencies

Now let's try setting PWM frequencies to custom values. The LEDs flash at 30.64, 61.04, and 122.55 Hz from left to right. Each light's pulsing is clearly visible at 240 FPS, and high-speed computer vision applications would need to account for it. As we mentioned earlier, 30.64 Hz was discernible to me with the naked eye.

What, I wondered, would happen if we put a capacitor across the 30.64 Hz LED? One might expect it to shine at the same intensity with less variation between pulses as a more constant light source. Using a 10µF capacitor makes only a slight difference, 'rounding off' the edges of the square wave input. A 2200µF cap evens the voltage out significantly, but the LED appears significantly dimmer.

While this larger capacitor tends to average out the instantaneous voltage, the lower standard deviation in the voltage (i.e., not fully on and fully off) means that the diode's voltage drop allows less total current to flow. We could use a capacitor as a form of analog to digital conversion, but that results in less light.

Perhaps the lesson here is that while PWM generally works well, it's not a one-size-fits-all solution. This becomes especially true when you're dealing with something as complex as human (or computer) vision.

Pulse Width Modulation DC Motor Controller

Image: Jeremy S. Cook, DC motor control via PWM and PID via a driver board.

Another excellent use for PWM is to control DC electric motors. The principle is much the same as with LED lighting, in that this technique uses a PWM duty cycle to simulates different voltage levels to a motor.

Because of the relatively high currents involved, you usually can't control a motor directly with a microcontroller or single-board computer. Instead, you'll need to rely on a motor driver with some sort of transistor setup. Additionally, if you want to control a motor's speed, you'll need to employ proportional, integral, derivative control (PID). PID uses several factors to speed up or slow down your motor efficiently, and you can explore executing PID-PWM control for an electric motor.

Servo 'PWM' Control

Image: Jeremy S. Cook, Even the humble ATtiny85 is capable of hobby servo control

We also use PWM to control servos, such as the ones in remote control vehicles and amateur robotics. However, the method for hobby servo control works much differently than lights or DC motors. In this case, the pulse happens at a set 50 Hz cycle rate, with the duty cycle varying between five percent and ten percent—signifying -90° and +90° settings. This works out to be a 20ms period, with between 1000µs and 2000µs pulses. Servo control may actually be the result of 'PWM protocol,' not PWM as it appears in other applications.

Pulse Width Modulation: Excellent Control for a Myriad of Electronic Applications

Some situations don't call for PWM, but pulse-width modulation works well in many applications. The next time you need to control a motor's speed or light intensity, try jumping in with an analog output command to chop up your signal into discrete chunks.

On a related note, if PWM isn't the right solution for your application, a true DAC, or digital to analog conversion setup, might be what you need. Learn more about the DAC technique, as well as how you can build your own R-2R DAC from 'scratch.' 


Neue Beiträge

Leider ergab Ihre Suche kein Ergebnis

Aktuelles über Elektronikkomponenten­

Wir haben unsere Datenschutzbestimmungen aktualisiert. Bitte nehmen Sie sich einen Moment Zeit, diese Änderungen zu überprüfen. Mit einem Klick auf "Ich stimme zu", stimmen Sie den Datenschutz- und Nutzungsbedingungen von Arrow Electronics zu.

Wir verwenden Cookies, um den Anwendernutzen zu vergrößern und unsere Webseite zu optimieren. Mehr über Cookies und wie man sie abschaltet finden Sie hier. Cookies und tracking Technologien können für Marketingzwecke verwendet werden.
Durch Klicken von „RICHTLINIEN AKZEPTIEREN“ stimmen Sie der Verwendung von Cookies auf Ihrem Endgerät und der Verwendung von tracking Technologien zu. Klicken Sie auf „MEHR INFORMATIONEN“ unten für mehr Informationen und Anleitungen wie man Cookies und tracking Technologien abschaltet. Das Akzeptieren von Cookies und tracking Technologien ist zwar freiwillig, das Blockieren kann aber eine korrekte Ausführung unserer Website verhindern, und bestimmte Werbung könnte für Sie weniger relevant sein.
Ihr Datenschutz ist uns wichtig. Lesen Sie mehr über unsere Datenschutzrichtlinien hier.