Achieving PWM with a Microcontroller in Your PCB Design
Understand what pulse width modulation (PWM) is.
Learn how to send PWM signals from a microcontroller.
Explore EMI concerns with PWM.
Making the perfect hard-boiled egg remains elusive for most of us—the secret is timing.
If you don’t get the timing right, you’ll either end up with a soft-boiled egg or an overboiled egg. A soft-boiled egg is still palatable but the sulfurous odor and flaky texture of an over-boiled yolk can be unpleasant.
Knowing this, I’ve concluded—after a few failed attempts—that 20 minutes is the perfect amount of time to boil an egg. This delicate timing reminds me of how pulse width modulation (PWM) with microcontrollers works to achieve the right duration for digital signals.
What Is PWM?
PWM is a technique that alters a continuous digital signal into a series of pulses. PWM is commonly used as a way to generate an analog signal that correlates to a digital value. You can find PWM being used to control servo motors, dim LEDs, drive a buzzer, encode communication signals, and more.
PWM waveform with various duty cycles.
To understand PWM, you’ll need to be familiar with a few terms: amplitude, period, pulse width, and duty cycle.
Amplitude of a PWM signal is usually the Vcc of the circuit.
Period is defined by the time it takes from one rising edge to the subsequent one.
Pulse width is the lapse in time between rising and falling edges of an individual pulse.
Duty cycle is the percentage of the logic 1 pulse width, compared to the period of the waveform.
PWM operates by adjusting the duty cycle of a waveform according to the configured digital value. By doing so, it can produce an analog signal from a digital output. For example, a 75% duty cycle PWM with a 5V amplitude will result in 3.75V. This principle can be used to dim an LED or the speed of a DC motor.
Altering the duty cycle of the PWM signal is also a means to control servo motors. A servo motor changes its angle corresponding to the duty cycle of the PWM signal. For example, a 1 ms pulse width of a 20 ms pulse will set the servo positions at 0°. Sending a 1.5 ms will move the servo to 90°.
How to Send PWM Signals With A Microcontroller
Use the built-in module to send PWM signals from a microcontroller.
The easiest way to generate PWM signals is via a microcontroller. Modern microcontrollers, whether 8-bit, 16-bit, or 32-bit are equipped with a PWM module. The PWM module works by setting the voltage level of the digital pin, incrementing the timer to preset pulse width, and toggling the pin for the remaining cycle.
When configuring PWM functionalities on an MCU, it is important to determine that the frequency and resolution of the signal are adequate. For example, if you’re dimming an LED with PWM, you may need the prescaler if the clock frequency is too high to start with, otherwise the dimming effect will be barely noticeable.
The resolution gives you finer control of the pulse width. An 8-bit microcontroller will limit you to 8-bit of the resolution, or 256 steps. A servo motor’s maximum position is usually at a 10% duty cycle of the PWM signal. This means that you’ll only have 25 steps of servo motor position with an 8-bit microcontroller.
Once you’ve configured the parameters, activating the PWM module and feeding the duty cycle value is quite straightforward.
EMI Considerations for Microcontroller PWM
Beware of EMI when controlling high-frequency, high-power devices with PWM.
There isn’t much concern from EMI if the PWM signals are low frequency in the regions of tens of kilohertz. You will need to be wary of EMI radiation when sending high-frequency, high-current PWM signals, for example, when using PWM to control high-powered motors or for sign-speed communications.
In such cases, it’s important to keep the PWM signal away from other sensitive components, particularly analog ones. Also, ensure that the return path of the PWM signal is as close to the signal trace as to prevent noise coupling to the ground of other components.
You can optimize designs for microcontroller PWM if you’re using an analytical-ready PCB software. OrCAD Sigrity technology helps to discover potential return path issues, particularly for high-frequency signals.
If you’re looking to learn more about how Cadence has the solution for you, talk to us and our team of experts.