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
Understand PWM timing for servo control
Optimize PWM frequency for different actuator types
572.1 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.
// Convert angle to pulse widthint 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 widthmyServo.write(90);// Move to 90 degrees
572.5 ESP32 PWM Configuration
// ESP32 has 16 PWM channels (0-15)// Each channel can have different frequency and resolution// Motor control - high frequency for silent operationconstint motorChannel =0;constint motorFreq =20000;// 20 kHz (above human hearing)constint motorResolution =8;// 0-255// LED control - lower frequency acceptableconstint ledChannel =1;constint ledFreq =5000;// 5 kHzconstint ledResolution =10;// 0-1023 (higher resolution for smooth fades)// Servo control - must be 50 Hzconstint servoChannel =2;constint servoFreq =50;// 50 Hz (20ms period)constint servoResolution =16;// High resolution for precise anglesvoid 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);}
572.6 PWM Speed to Value Conversion
// Convert percentage speed to PWM valueint speedToPWM(int speedPercent,int resolution){int maxValue =(1<< resolution)-1;// 2^resolution - 1return 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)
572.7 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!
572.8 Quick Reference: PWM Formulas
Duty Cycle (%) = (ON time / Period) x 100
Duty Cycle (%) = (PWM value / Max value) x 100
Examples (8-bit PWM, 0-255):
- PWM = 0 = 0% duty = 0V average (OFF)
- PWM = 64 = 25% duty = 1.25V @ 5V supply
- PWM = 128 = 50% duty = 2.5V @ 5V supply
- PWM = 191 = 75% duty = 3.75V @ 5V supply
- PWM = 255 = 100% duty = 5V average (FULL ON)
Average Voltage = Supply Voltage x (PWM Value / Max Value)
572.9 What’s Next?
Now that you understand PWM control, you’re ready to learn about safety and protection for actuator systems.