Pulse width modulation, or PWM, is a way for a microcontroller to control power using a digital signal that switches rapidly between on and off. Instead of creating a true analog voltage, the controller changes the fraction of time the signal is high during each cycle. This matters in robotics because PWM can control motor speed, LED brightness, heaters, and servo positions using simple digital pins.
A PWM waveform looks like a square wave, but its timing carries useful control information.
Key Facts
- Duty cycle = on time / period x 100%
- Period T = 1 / frequency
- Average voltage for an ideal PWM signal is Vavg = duty cycle x Vhigh, where duty cycle is written as a decimal.
- For a DC motor driver, higher duty cycle usually means higher average motor voltage and higher speed.
- Many hobby servos use about 50 Hz PWM, with pulse widths near 1 ms, 1.5 ms, and 2 ms for different angles.
- PWM frequency should be chosen to match the device, driver circuit, and desired response.
Vocabulary
- PWM
- Pulse width modulation is a control method that changes the on time of a repeating digital signal.
- Duty cycle
- Duty cycle is the percentage of one period during which a PWM signal is high.
- Period
- Period is the time required for one complete cycle of a repeating waveform.
- Frequency
- Frequency is the number of waveform cycles that occur each second, measured in hertz.
- Servo pulse width
- Servo pulse width is the length of the high pulse that tells many servos what angle to move toward.
Common Mistakes to Avoid
- Confusing duty cycle with frequency, because duty cycle controls the fraction of time the signal is high while frequency controls how often the cycles repeat.
- Assuming PWM is a true analog voltage, because the pin still switches fully on and off even though the average effect can behave like a lower voltage.
- Driving a motor directly from a microcontroller pin, because motors often require more current and protection than a logic pin can safely provide.
- Using the same PWM settings for DC motors and servos, because DC motor speed is mainly affected by duty cycle while hobby servos usually respond to pulse width at a suitable repetition rate.
Practice Questions
- 1 A 5 V PWM signal has a duty cycle of 40%. What is the ideal average voltage?
- 2 A PWM signal has a frequency of 500 Hz. What is its period in milliseconds?
- 3 Explain why a DC motor and a hobby servo can both use PWM signals but interpret the signal in different ways.