Sub topic 13
Choosing a PWM frequency
This chapter assumes you can already drive a motor with analogWrite and an H-bridge, as covered in the PWM speed control and Arduino control chapters. You should know what duty cycle means and have a driver wired with a shared ground.
What the frequency decides
Duty cycle sets the average voltage the motor sees. Frequency sets how fast the driver switches on and off to produce that average. Two PWM signals with the same 50 percent duty cycle can behave very differently on a motor if one runs at 490 Hz and the other at 20 kHz.
A motor winding is an inductor in series with a resistor. When the driver switches the supply on, current rises along an exponential curve with time constant L/R. When the driver switches off, current decays along the same kind of curve through the flyback path. The frequency decides how much of that rise and fall fits inside one PWM period.
- If the period is long compared with L/R, current swings widely. It may fall to zero between pulses, so torque arrives in jerky bursts.
- If the period is short compared with L/R, the winding smooths the pulses. Current stays nearly constant and torque is steady.
The motor therefore acts as a low-pass filter. Higher frequency gives a smoother current, but each switching edge costs the driver some energy.
Why the default is often a poor choice
On an Arduino Uno or Nano (ATmega328P), analogWrite produces roughly 490 Hz on pins 3, 9, 10 and 11, and roughly 980 Hz on pins 5 and 6. Both fall in the range where human hearing is most sensitive. Many small motors respond to this with a clear whine or buzz, because the windings and the rotor vibrate at the switching frequency.
The default is not always a problem. A motor that moves a robot across a noisy floor may not need anything better. Change it when you hear noise you dislike, when the motor stutters at low duty, or when current ripple causes trouble elsewhere in the circuit.
Effects of going too low
At low frequencies, three things tend to appear.
- Audible noise. Anything between about 20 Hz and 15 to 20 kHz can be heard. Many adults hear less above 15 kHz, but children and pets often hear more.
- Torque ripple. The current rises and falls with each pulse. At low duty the motor may get a short push, coast, and get another push. This shows as a stutter.
- Current peaks. With a large ripple, peak current is higher than average current. The driver and supply must handle the peak, and the motor heats according to the RMS current, which exceeds the average.
Fast-decay switching, where the motor is coasting during the off phase, produces more ripple than slow-decay switching, where the motor is braked during the off phase. The brake and coast chapter covers this difference. Slow decay lets you use a lower frequency for the same ripple.
Effects of going too high
High frequency is not free.
- Switching loss. A transistor dissipates the most power while it moves between on and off, when it has both voltage across it and current through it. Each switch event costs a small amount of energy, so loss rises in proportion to frequency.
- Driver limits. Many older bipolar drivers switch slowly. The L298N and L293D datasheets list limits in the low kilohertz for good behavior, and the L298N becomes inefficient well before 20 kHz. MOSFET drivers handle 20 to 100 kHz or more, and the datasheet states the maximum.
- Minimum pulse width. At 30 kHz the period is about 33 microseconds. A 2 percent duty cycle is then under 1 microsecond, shorter than the driver's turn-on delay. The motor sees no pulse at all, which creates a dead zone at the bottom of the duty range.
- Bootstrap limits. Some drivers with high-side N-channel MOSFETs use a bootstrap capacitor to power the gate. These often require a minimum off time to recharge, and a duty cycle near 100 percent may be unavailable.
- Electrical noise. Fast edges radiate more interference and can disturb nearby sensors or radio receivers.
Dead zone and low-speed behavior
A brushed motor does not turn until the torque from the average current exceeds friction. The duty cycle at which the motor starts to move is its start threshold. It is often 15 to 30 percent of full duty for small gearmotors.
Frequency affects how that threshold feels:
- At low frequency, the motor may twitch with each pulse at duties below the threshold, instead of staying still.
- At high frequency, the motor sits still until the average torque passes friction, then moves smoothly.
If you measure that threshold, you can map your control range onto it. For example, if the motor starts at a value of 40 out of 255, a command of 1 can map to 40 so the lowest nonzero command always moves the motor.
Changing the frequency on an ATmega328P
Arduino's analogWrite has no frequency argument. On AVR boards you change it by editing the timer prescaler. This changes timer behavior and does not apply to other boards.
The Uno and Nano use three timers.
| Timer | Pins | Default PWM frequency | Notes |
|---|---|---|---|
| Timer0 | 5, 6 | about 980 Hz | Also drives `millis()`, `delay()` and `micros()` |
| Timer1 | 9, 10 | about 490 Hz | 16-bit timer, used by the Servo library |
| Timer2 | 3, 11 | about 490 Hz | Used by `tone()` |
Setting the prescaler bits to a different value changes the frequency. For Timer1 and Timer2, the low three bits of the control register B select the prescaler.
void setup() {
// Arduino Uno / Nano (ATmega328P): pins 9 and 10 use Timer1.
// Prescaler 1 raises PWM from about 490 Hz to about 31 kHz.
TCCR1B = (TCCR1B & 0xF8) | 0x01;
pinMode(9, OUTPUT);
}
void loop() {
analogWrite(9, 128); // about 50 percent duty at the higher frequency
}The expression (TCCR1B & 0xF8) | 0x01 keeps the upper five bits and replaces the three prescaler bits. The value 0x01 selects a prescaler of 1, which gives about 31 kHz. Other values for Timer1 give about 3.9 kHz (0x02), 490 Hz (0x03, the default) and lower.
Changing Timer1 stops the Servo library from working. Changing Timer2 stops tone() from working. Choose the timer that your sketch does not need, and put the motor enable on a pin that belongs to it.
Before you set 31 kHz, check that your driver tolerates it. An L298N on a 31 kHz enable signal may run hot and lose torque, because the transistors cannot switch cleanly. In that case choose an intermediate setting or replace the driver.
Choosing a value
There is no single right answer. Work through these steps.
- Read the maximum PWM frequency in the driver's datasheet. This is the upper bound.
- Start with the default. Run the motor at low, middle and high duty and listen.
- If the whine bothers you, move above 18 to 20 kHz if the driver allows it.
- If the driver is a slow bipolar type, stay between about 1 and 5 kHz and accept some noise.
- Check the driver temperature after several minutes at the higher frequency. If it is warmer than before, you are paying switching loss.
- Test at the lowest speed you need. If the motor stalls or the pulse is lost, lower the frequency a little.
| Situation | Reasonable choice |
|---|---|
| L298N or L293D module | 1 to 5 kHz |
| Small MOSFET driver, quiet room | 20 to 25 kHz |
| Large motor, large driver, heat is a concern | 10 to 20 kHz |
| Motor with high inductance (large winding) | Lower frequency is acceptable |
| Motor with low inductance (coreless) | Higher frequency needed to limit ripple |
Coreless and small ironless motors have very low inductance, so their current ripple is large at ordinary frequencies. They often need 20 kHz or more to avoid overheating from ripple current alone.
Common mistakes
- Raising the frequency on a slow driver. The motor gets weaker and the driver hot. The cause is the driver, not the code.
- Altering Timer0. Timing functions break and the cause is hard to find.
- Forgetting the library conflict. Servo or
tone()stops working after a timer change. - Testing only at high duty. Problems such as stutter and dead zones show up only near the bottom of the range.
- Assuming silence means success. A silent motor can still run hot because of switching loss.
Exercise
A project uses an L298N module with a small gearmotor. The motor whines at the default 490 Hz, so you set Timer1 to a prescaler of 1 (about 31 kHz) on pin 9. The whine disappears, but the motor is weaker and the L298N is hotter. Explain the cause and suggest a fix.
Show answer
The L298N uses slow bipolar transistors and is not designed for frequencies this high. At 31 kHz the transistors spend a large fraction of each period in transition, so switching loss rises and heat builds up. Very short pulses at low duty may also be lost, which reduces torque. The fix is to choose a lower frequency, for example the 3.9 kHz setting (Timer1 prescaler bits 0x02), and accept some audible noise, or replace the driver with a MOSFET driver rated for 20 kHz or higher.
Exercise
You set Timer2 to a faster PWM frequency to quiet a motor on pin 11. Afterward, a sketch that calls tone() on another pin produces the wrong pitch or no sound. Why, and what can you change?
Show answer
On the ATmega328P, tone() uses Timer2. Changing the Timer2 prescaler alters the timing that tone() depends on, so its output is wrong. You can move the motor enable to a Timer1 pin (9 or 10) and change Timer1 instead, as long as the sketch does not use the Servo library. Alternatively, generate the tone another way or use a board with more timers.