Sign in to save

Bookmark this page so you can find it later.

Sign in to save

Bookmark this page so you can find it later.

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.

Understanding Robotics: PWM Signal Control

A PWM pin usually does not power a motor by itself. The pin sends a control signal to a driver, which uses transistors as fast electronic switches. In a common motor circuit, a transistor connects the motor to the battery for part of each cycle.

When the switch opens, the motor current needs a safe path because the motor windings resist sudden changes in current. A flyback diode or a driver circuit provides that path.

Without this protection, voltage spikes can damage the transistor or microcontroller. This is why wiring a motor directly to a digital pin is a common beginner mistake.

Motor speed does not follow the command perfectly. Motor torque depends mainly on current, while speed is affected by load, friction, battery voltage, and the motor's own back voltage. At a very low duty cycle, a motor may receive short bursts that are too weak to overcome static friction.

It can buzz, shake, or draw high current without turning. A robot carrying a heavier object needs more duty cycle than the same robot on a smooth floor. Battery voltage falls as a battery discharges, so a fixed command can produce slower motion later.

Encoders help a robot measure actual wheel movement. The program can then adjust the PWM command to hold a target speed.

Servo control uses timing in a different way from DC motor speed control. A standard hobby servo has electronics, a small motor, gears, and a position sensor inside its case. The incoming pulse tells its controller where to move.

The controller compares the requested position with the sensor reading, then drives the internal motor until the difference is small. The pulse is therefore a command signal, not a direct supply of power to the servo motor. A servo needs a separate power supply that can provide enough current.

The control wire and the power system must share a common ground, or the servo cannot read the pulse reliably. Continuous rotation servos use similar pulses, but the pulse usually commands direction and speed rather than a fixed angle.

A microcontroller creates PWM with hardware timers. The timer counts at a steady rate, then changes the output when it reaches selected count values. The number of available counts sets the resolution.

More counts allow smaller command changes, which can make LED fading smoother or motor control finer. Frequency creates tradeoffs. Low frequencies can cause visible LED flicker or an audible motor whine.

Very high frequencies can increase switching losses and make some drivers run hotter. A multimeter may show an average value that hides fast timing errors, so an oscilloscope or logic analyzer is useful when debugging. Students should check the signal frequency, pulse timing, supply voltage, driver temperature, and shared ground before blaming the code.

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. 1 A 5 V PWM signal has a duty cycle of 40%. What is the ideal average voltage?
  2. 2 A PWM signal has a frequency of 500 Hz. What is its period in milliseconds?
  3. 3 Explain why a DC motor and a hobby servo can both use PWM signals but interpret the signal in different ways.