8 PWM Control: Signals and Duty-Cycle Behaviour
8.1 Start With the Decision
A motor sees pulses, but its load responds to average energy and current ripple. Duty cycle links the digital command to physical motion.
8.2 Route Overview
This is part 1 of 2. Continue with PWM Control: Frequency and Driver Decisions.
8.3 Part Objectives
- Compute PWM duty cycle and average drive level.
- Explain how load response changes across a PWM cycle.
8.4 Start With the Story
Prove the Physical Output at Three Settings
Picture a small fan that must run gently at normal temperature and harder during a heat rise. A changing command number is not enough; the reviewer needs the drive timing, supply condition, and measured fan result.
An actuator is a device that turns an electrical command into physical action. A microcontroller is a small computer built to read inputs and control outputs. Modulation means changing a signal over time to carry a setting. Duty cycle means the share of each switching period spent on.
Command the low, middle, and high settings. Stall the fan briefly within a safe test, lower the supply, repeat a setting, and restart the controller. Record command, timing, current, and physical response together, and keep any urgent heat action local.
This runway does not prove motor life or make one switching rate safe for every load. The deeper sections cover waveform timing, driver stages, resolution, filtering, load limits, and measurement.
Picture a fan that should start gently, run quietly, and still move enough air on a hot afternoon. PWM is the controller’s way to ask for less or more effort, but the physical result depends on frequency, duty cycle, driver losses, motor inertia, and the load.
Use PWM as an actuator command with evidence around it. The right setting is not just a number from 0 to 255; it is a duty, frequency, ramp, and feedback choice that keeps motion, brightness, heat, or position under control.
- PWM (Pulse Width Modulation): A technique for controlling average power by rapidly switching a signal between HIGH and LOW; duty cycle = (time HIGH) / (period) x 100%; a 50% duty cycle delivers 50% of the supply voltage as an average
- Duty Cycle: The percentage of one PWM period during which the signal is HIGH; 0% = always off, 100% = always on; controls LED brightness, motor speed, heater temperature, and servo position (within a specific pulse width range)
- PWM Frequency: The number of complete PWM cycles per second; motor control: use 20 kHz+ to avoid audible noise; servo control: 50 Hz standard (20 ms period); LED dimming: any frequency above 200 Hz to prevent visible flicker
- PWM Resolution: The number of discrete duty cycle steps; ESP32 LEDC peripheral: 1-16 bit configurable; Arduino Uno: 8-bit (256 steps); higher resolution provides finer control but requires more timer bits
- analogWrite() / ledcWrite(): Arduino function (analogWrite) and ESP32-specific function (ledcWrite) for setting PWM duty cycle; Arduino Uno/Nano output 8-bit 490 Hz PWM on most pins; ESP32 uses the LEDC peripheral requiring channel configuration before use
- Servo Pulse Width: Servo motors use PWM with a 20 ms period (50 Hz); pulse width controls position: 1 ms = 0 degrees (minimum), 1.5 ms = 90 degrees (center), 2 ms = 180 degrees (maximum); use the Servo.h library which handles this automatically
- Dead Time: A brief delay inserted between turning one switch off and its complementary switch on in an H-bridge or inverter; prevents shoot-through (both high and low side switches on simultaneously), which would create a short circuit across the power supply
- Soft Start / Ramp Control: Gradually increasing PWM duty cycle from 0 to operating value at startup rather than switching immediately to full power; reduces inrush current, mechanical shock, and EMI during motor startup
Learning Objectives
After completing this chapter, you will be able to:
- Calculate PWM duty cycles for various actuator applications
- Configure ESP32 PWM channels for different frequencies and resolutions
- Use the interactive PWM calculator for design decisions
- Analyze PWM timing parameters for servo position control
- Optimize PWM frequency for different actuator types
- Start With the Story
- In 60 Seconds
- Key Concepts
- For Beginners: PWM Control
- How It Works: PWM Creates “Fake” Analog Voltage
- PWM Fundamentals
- Putting Numbers to It
- Phoebe’s Field Notes: Why PWM Becomes Smooth Motion
- Try It: PWM Motor Speed Control
- Concept Check: PWM Fundamentals
- Checkpoint: Average Command
- Interactive PWM Calculator
- PWM Applications by Frequency
- Servo PWM Timing
- ESP32 PWM Configuration
- PWM Speed to Value Conversion
- Checkpoint: Timer Values
- Why Duty Cycle Matters for IoT
- Worked Example: Smart Greenhouse Ventilation Fan
Imagine flicking a light switch on and off very rapidly — so fast that the light appears to be dimmed rather than blinking. That is essentially what PWM (Pulse Width Modulation) does. By controlling how long the power stays on versus off in each cycle, you can smoothly adjust a motor’s speed or an LED’s brightness using just a simple digital on/off signal.
Imagine you’re pushing a child on a swing. You can’t push continuously (analog), but you can push in pulses (digital). The faster and longer you push each time, the higher the swing goes.
PWM works the same way:
- Microcontroller limitation: GPIO pins output only 0V (OFF) or 3.3V/5V (ON) - nothing in between
- The PWM trick: Switch ON and OFF thousands of times per second
- Motor/LED response: Physical inertia smooths the pulses - the device “sees” the average voltage
Example: 50% duty cycle = ON for 0.5 ms, OFF for 0.5 ms, repeat at 1kHz = motor sees ~2.5V average from a 5V supply
The key insight: By varying the ON time (duty cycle), you create any “analog” voltage from 0% (always OFF) to 100% (always ON).
8.5 PWM Fundamentals
PWM (Pulse Width Modulation) is a digital technique that simulates analog control by rapidly switching power ON and OFF:
- Duty cycle = Percentage of time the signal is HIGH (ON)
- Frequency = How many times per second the signal repeats
- Average power = Supply voltage x Duty cycle percentage
Why use PWM? Digital microcontrollers can’t produce true analog voltages (like 2.5V from a 5V supply). Instead, they rapidly switch between 0V and 5V. A motor or LED responds to the average voltage over time.
At 5kHz PWM with 50% duty cycle, each period is μs. The signal is HIGH for μs and LOW for 100 μs. A motor with mechanical time constant ms cannot respond to individual 200 μs pulses ( cycles per time constant), so it sees only the average voltage: V. This is why PWM “fakes” analog — the actuator’s inertia performs the averaging.
8.6 Try It: PWM Motor Speed Control
Use the simulator as an evidence sequence. Begin with zero load and a moderate switching frequency, raise duty cycle while watching average voltage and speed, then add inertia and load to expose current demand. Reverse the H-bridge only after the shaft response is understood, and finally sweep frequency to distinguish audible whine from excessive switching loss. The useful result is a bounded operating region, not one attractive speed reading.
Checkpoint: Average Command
You now know:
- Duty cycle is the percent of each period spent HIGH; at 50% duty and a 5 V supply, the average actuator voltage is 2.5 V.
- Frequency sets how often the periods repeat; at 5kHz, each period is 200 microseconds, not a new voltage level.
- The 75% duty quiz uses the same rule on a 12V motor:
12V x 0.75 = 9V.
With the command meaning settled, the next step is turning a percentage into the actual timer value the ESP32 writes.
8.7 Interactive PWM Calculator
8.7.1 Calculated Values
8.7.2 ESP32 Code
8.8 PWM Applications by Frequency
Different actuators require different PWM frequencies:
| Application | Typical Frequency | Duty Cycle Range | IoT Example |
|---|---|---|---|
| LED dimming | 500-1000 Hz | 0-100% | Smart bulb brightness |
| DC motor speed | 1-20 kHz | 0-100% | Ceiling fan control |
| Servo position | 50 Hz | 5-10% (1-2 ms pulse) | Robotic arm joints |
| Buzzer/speaker | 20Hz-20kHz | 50% (square wave) | Doorbell tones |
| Heating element | 0.5-2 Hz | 0-100% | 3D printer bed |
| Solenoid valve | 100-500 Hz | 0-100% | Water flow control |
8.9 Servo PWM Timing
Servo motors are a special case - they use pulse width for position:
Standard Servo:
- 1000 microseconds (1ms) = 0 degrees (5% duty cycle)
- 1500 microseconds (1.5ms) = 90 degrees (7.5% duty cycle, center)
- 2000 microseconds (2ms) = 180 degrees (10% duty cycle)
- Period: 20ms (50 Hz)
// Convert angle to pulse width
int angleToPulseWidth(int angle) {
return map(angle, 0, 180, 1000, 2000); // 1000-2000 microseconds
}
// Using ESP32Servo library (handles timing automatically)
#include <ESP32Servo.h>
Servo myServo;
myServo.attach(pin, 1000, 2000); // min/max pulse width
myServo.write(90); // Move to 90 degrees
8.10 ESP32 PWM Configuration
// ESP32 has 16 PWM channels (0-15)
// Each channel can have different frequency and resolution
// Motor control - high frequency for silent operation
const int motorChannel = 0;
const int motorFreq = 20000; // 20 kHz (above human hearing)
const int motorResolution = 8; // 0-255
// LED control - lower frequency acceptable
const int ledChannel = 1;
const int ledFreq = 5000; // 5 kHz
const int ledResolution = 10; // 0-1023 (higher resolution for smooth fades)
// Servo control - must be 50 Hz
const int servoChannel = 2;
const int servoFreq = 50; // 50 Hz (20ms period)
const int servoResolution = 16; // High resolution for precise angles
void setup() {
// Motor PWM
ledcSetup(motorChannel, motorFreq, motorResolution);
ledcAttachPin(MOTOR_PIN, motorChannel);
// LED PWM
ledcSetup(ledChannel, ledFreq, ledResolution);
ledcAttachPin(LED_PIN, ledChannel);
// Servo PWM (use ESP32Servo library instead for easier control)
ledcSetup(servoChannel, servoFreq, servoResolution);
ledcAttachPin(SERVO_PIN, servoChannel);
}
8.11 PWM Speed to Value Conversion
Translate the requested percentage in two stages: first compute the maximum register value from resolution, then map the bounded 0-100% request into that integer range. Compare the 8-, 10-, and 12-bit examples in order; all represent the same 75% command, but finer resolution supplies more selectable codes. The conversion changes command granularity, not supply voltage, motor torque margin, or proof of achieved speed.
// Convert percentage speed to PWM value
int speedToPWM(int speedPercent, int resolution) {
int maxValue = (1 << resolution) - 1; // 2^resolution - 1
return map(speedPercent, 0, 100, 0, maxValue);
}
// Examples:
// 75% speed at 8-bit resolution:
int pwm8 = speedToPWM(75, 8); // = 191 (out of 255)
// 75% speed at 10-bit resolution:
int pwm10 = speedToPWM(75, 10); // = 767 (out of 1023)
// 75% speed at 12-bit resolution:
int pwm12 = speedToPWM(75, 12); // = 3071 (out of 4095)
Checkpoint: Timer Values
You now know:
- ESP32 PWM setup chooses a channel, frequency, and resolution before
ledcWrite()can set the output. - At 75% command, the chapter’s examples map to 191 on 8-bit, 767 on 10-bit, and 3071 on 12-bit PWM.
- Servo control is the exception to the “average voltage” mental model: a 50 Hz frame carries 1 ms, 1.5 ms, or 2 ms position pulses.
Now use those timer settings only after the load tells you what switching frequency it can tolerate.
8.12 Why Duty Cycle Matters for IoT
- Energy Efficiency: LED at 50% duty cycle uses approximately 50% power, extending battery life
- Precise Control: Achieve 256 speed levels (8-bit) or 1024 levels (10-bit) from a simple digital pin
- No Analog Hardware: Digital MCUs can control analog-like behavior without DAC chips
- Thermal Management: Lower duty cycles reduce heat in motors and drivers
Key insight: The actuator “sees” the average voltage, smoothed by its physical response time. A 1kHz PWM signal switching between 0V and 5V at 50% duty looks like steady 2.5V to a motor!
8.13 Worked Example: Smart Greenhouse Ventilation Fan
A greenhouse uses a 12V DC fan (rated 0.5A at full speed) controlled by an ESP32 via a MOSFET driver. The system adjusts fan speed based on temperature.
Step 1: Power at each operating point
| Temperature | Fan Speed | Duty Cycle | Average Current | Power Consumed |
|---|---|---|---|---|
| Below 22C | Off | 0% | 0 A | 0 W |
| 22-25C | Low | 30% | 0.15 A | 1.8 W |
| 25-30C | Medium | 60% | 0.30 A | 3.6 W |
| 30-35C | High | 85% | 0.425 A | 5.1 W |
| Above 35C | Full | 100% | 0.50 A | 6.0 W |
Power = Voltage x Average Current = 12V x (Duty Cycle x 0.5A)
Step 2: Daily energy consumption
Assume a typical summer day: 6 hours off (night), 8 hours at 30%, 6 hours at 60%, 4 hours at 85%.
- Energy = (6 x 0) + (8 x 1.8) + (6 x 3.6) + (4 x 5.1) = 0 + 14.4 + 21.6 + 20.4 = 56.4 Wh/day
Compare to running the fan at full speed 24 hours: 6.0W x 24h = 144 Wh/day. PWM-based variable speed saves 61% of the daily energy.
Step 3: Why 20 kHz for this fan?
At lower PWM frequencies, the fan motor produces audible hum. A DC motor’s coils vibrate at the PWM frequency, and humans hear 20 Hz to 20 kHz. Choosing 20 kHz pushes the hum above hearing range, making the fan appear to run silently at any speed. The trade-off: higher PWM frequencies increase switching losses in the MOSFET driver (typically adding 0.1-0.3W at 20 kHz versus 1 kHz), but the acoustic benefit outweighs the small efficiency loss in an occupied greenhouse.
8.14 Continue to the Next Part
Carry this evidence into PWM Control: Frequency and Driver Decisions, which begins with Choosing PWM Frequency: Decision Factors.
