What Is PWM?
PWM, or Pulse Width Modulation, is a technique used to generate analog-like signals by varying the width of digital pulses. This allows microcontrollers to control devices like servo motors with precision.
How PWM Controls Servo Motors
Servo motors respond to PWM signals with a specific duty cycle (percentage of the pulse duration in a cycle) to set their position:
- A pulse width of ~1ms moves the servo to 0°.
- A pulse width of ~1.5ms sets the servo to 90°.
- A pulse width of ~2ms moves the servo to 180°.
PWM Example with Arduino
#include
Servo myServo;
void setup() {
myServo.attach(9); // Attach servo to pin 9
}
void loop() {
myServo.write(90); // Move to 90 degrees
delay(1000);
myServo.write(0); // Move to 0 degrees
delay(1000);
myServo.write(180); // Move to 180 degrees
delay(1000);
}