572  PWM Control for Actuators

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
  • 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.

572.2 Interactive PWM Calculator

572.2.1 Calculated Values

572.2.2 ESP32 Code

572.3 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-2ms 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

572.4 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

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 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);
}

572.6 PWM Speed to Value Conversion

// 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)

572.7 Why Duty Cycle Matters for IoT

  1. Energy Efficiency: LED at 50% duty cycle uses approximately 50% power, extending battery life
  2. Precise Control: Achieve 256 speed levels (8-bit) or 1024 levels (10-bit) from a simple digital pin
  3. No Analog Hardware: Digital MCUs can control analog-like behavior without DAC chips
  4. 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.

Continue to Safety and Protection →