PWM Timing & Actuator Interfaces: the short answer
A digital pin is either on or off, but by switching it very fast you can control the average power. That is PWM, pulse-width modulation.
- Level
- Intermediate
- Time
- About 5 min
- Topic
- Microcontrollers
Use PWM to control LED brightness and understand duty cycle and average voltage.
Before this: Digital Inputs and Outputs
A digital pin is either on or off, but by switching it very fast you can control the average power. That is PWM, pulse-width modulation. The duty cycle is the share of time it is on: 25 % duty on 5 V averages 1.25 V.
LEDs and motors respond to the average, so PWM dims an LED or slows a motor. On an Uno, analogWrite(pin, 0 to 255) gives 0 to 100 % duty at about 490 Hz on most pins. On an ESP32 you choose the frequency and resolution with the LEDC functions.
Servos are different: they read the pulse width, not the average. A 1 to 2 ms pulse repeated every 20 ms (50 Hz) sets the angle. Use the Servo library for that instead of analogWrite.
For motors use a driver or MOSFET between pin and load, and pick a frequency above about 20 kHz if the whine bothers you.
Duty cycle sets the average
PWM switches an output fully on and fully off, fast. The fraction of time spent on is the duty cycle. A 5 V output at 40 percent duty has an average of 2 V, and a load that smooths the pulses, such as an LED seen by the eye or a motor with inertia, responds to that average.
Because the transistor is either fully on or fully off, little power is wasted in it. That is why PWM is used for dimming and speed control instead of a variable resistor.
Choosing the frequency
Too low and you see flicker or hear the motor stutter; around 1 kHz is enough for LEDs. A motor driven in the audible range whines, so many projects move to 20 kHz or higher. Very high frequencies waste energy in switching and may exceed what the driver supports.
On an Arduino Uno, analogWrite uses about 490 Hz on most pins and about 980 Hz on pins 5 and 6. The timers are shared, so changing a frequency can affect other functions such as delay-based timing or the Servo library.
Resolution and what PWM is not
An 8-bit PWM has 256 steps. A smooth fade at low brightness needs finer steps because the eye is more sensitive there. A PWM pin is not an analog output: add a resistor and capacitor filter if you need a real voltage, and expect some ripple.
- Using a frequency in the audible range and hearing the motor whine.
- Driving a motor straight from the pin instead of through a transistor or driver.
- Treating PWM as a clean analog voltage without a filter.
- T = 1 ÷ f
- period
- Ton = D × T
- on time
- Vavg = D × Vs
- average voltage
Three cycles of your PWM signal
Work it with your own numbers: PWM timing · PWM average
Practise this lesson with Maker
The explanation above is free for everyone. The practice for Intermediate lessons in every domain are part of the Maker plan at ₹499 a month, and Pro includes them too.
With Maker you also get: a worked example, a hands-on task, a 3-question check.
