Sub topic 13
Choosing a PWM Frequency
This chapter assumes you know basic Arduino functions such as analogWrite(), understand duty cycle, and can read a motor-driver data sheet. You should also know that a motor driver switches the motor current, while the Arduino supplies only low-power control signals.
What PWM frequency controls
Pulse-width modulation (PWM) turns the driver on and off rapidly. The duty cycle is the percentage of each cycle that the signal stays on. A 50% duty cycle gives the motor roughly half of the available average voltage, although speed also depends on load, friction, and motor construction.
The PWM frequency is how many complete cycles occur each second. It is measured in hertz (Hz). Frequency changes the behavior of the motor and driver even when the duty cycle stays the same.
The main trade-offs
A low frequency makes each pulse longer. The motor current rises and falls more during each cycle, which can cause vibration, audible whining, or uneven motion at low speed. A low frequency also reduces switching losses in some drivers.
A higher frequency makes the pulses shorter and usually moves the switching sound above human hearing. The motor inductance smooths the current, so torque can feel more consistent. However, every transition consumes energy. Excessive frequency can heat a driver, increase electromagnetic interference, and reduce the useful duty-cycle resolution.
Many small motor systems start between 10 kHz and 20 kHz. The correct value depends on the driver, motor, supply voltage, and mechanical load. Always check the driver data sheet. Some integrated drivers specify a minimum or maximum PWM frequency.
| Frequency choice | Typical result | Main risk |
|---|---|---|
| Low, such as 500 Hz | Efficient and widely supported | Audible noise and rough low-speed motion |
| Medium, such as 10–20 kHz | Quieter operation and smoother current | More switching loss |
| High, above 30 kHz | Usually inaudible | Driver heating, interference, or reduced control range |
Changing PWM frequency on an Arduino Uno
On an Arduino Uno or Nano using an ATmega328P, pins 9 and 10 use Timer1. The following sketch changes their PWM frequency from the usual roughly 490 Hz to about 31 kHz. This example is specific to those boards and pins.
const uint8_t ENABLE_PIN = 9;
void setup() {
pinMode(ENABLE_PIN, OUTPUT);
// ATmega328P Timer1: prescaler 1.
// Pins 9 and 10 become approximately 31 kHz PWM.
TCCR1B = (TCCR1B & 0b11111000) | 0b00000001;
}
void loop() {
analogWrite(ENABLE_PIN, 128); // Approximately 50% duty cycle
}Changing a timer can affect other features that use that timer. Timer1 is also used by some libraries and timing functions. Board families such as the Uno R4, Mega, and many ARM boards use different timer hardware, so do not copy these register settings without checking the board documentation.
Choosing a practical value
Start with the driver’s recommended range. If the motor whines, increase the frequency gradually and compare driver temperature and low-speed torque. If the motor becomes weak, stutters, or the driver overheats, reduce the frequency. Keep the wiring short where possible, add the driver’s recommended supply capacitors, and separate motor-current paths from sensitive signal wiring.
Exercise
A motor driver works at 490 Hz but produces an annoying tone. What should you try first, and what must you monitor?
Show answer
Try a higher frequency within the driver’s specified range, such as 10–20 kHz. Monitor driver temperature, low-speed torque, and any resets or interference.