Understanding PWM outputs on Arduino unlocks a world of analog-like control using digital pins, allowing you to simulate voltages between the standard 0 and 5 volts or 3.3 volts. Instead of a steady state, Pulse Width Modulation works by rapidly switching a digital pin on and off, varying the ratio of on-time to off-time to effectively control the average power delivered to a device. This technique is fundamental for a wide array of projects, from smoothly dimming an LED to controlling the speed of a DC motor or the position of a servo.
How PWM Works on Arduino Hardware
At the hardware level, Arduino boards utilize built-in timers to generate the PWM signal. These timers count clock cycles and compare their value against a set register, which determines when the output pin should be toggled. The frequency of this switching, often in the range of 490 Hz or 980 Hz on most Uno pins, is usually fixed, while the duty cycle—the percentage of time the signal is high—is what you manipulate to set the output level. On the Uno and Nano, specific pins labeled with a tilde (~) are capable of this functionality, leveraging registers like `OCR0A`, `OCR0B`, `OCR1A`, and `OCR1B` to fine-tune the timing.
Controlling LED Brightness
The most common application of PWM is LED brightness control. Connecting an LED to a PWM-capable pin and using the `analogWrite()` function allows you to create a smooth fading effect that is imperceptible to the human eye flicker. By passing a value from 0 to 255 to `analogWrite()`, where 0 is always off and 255 is always on, you directly set the duty cycle. This method is far more efficient than using a variable resistor, as it draws minimal current from the Arduino’s microcontroller while providing a wide range of light adjustment.
Motor Speed and Direction Control
For robotics and automation, PWM is essential for managing motor speed and direction. By applying a PWM signal to a motor driver circuit, such as an H-bridge or an L298N module, you can dictate how fast the motor spins without the electrical noise associated with trying to vary voltage linearly. An H-bridge uses two channels of PWM, often in opposite phases, to control both forward and reverse rotation. This allows for precise throttle control, making it ideal for vehicles, drones, and automated machinery where consistent torque is required at different speeds.
Generating Analog Voltages
Because PWM is a digital signal, you often need to convert it into a true analog voltage for sensors or audio applications. This is easily achieved with a simple low-pass filter circuit consisting of a resistor and a capacitor, which smooths the square wave into a steady DC voltage proportional to the duty cycle. This method provides a flexible way to generate reference voltages or interface with legacy analog components, effectively turning any PWM pin into a programmable power source that can be adjusted with precision.
Servo Motor Positioning
Standard hobby servos are driven by PWM signals where the pulse width, rather than the duty cycle percentage, defines the angular position. The control signal typically repeats every 20 milliseconds, with a 1.5ms pulse centering the shaft at 90 degrees. Arduino libraries like `Servo.h` abstract the complex timing required, allowing you to specify an angle from 0 to 180 degrees. This reliability makes PWM the preferred method for robotic arms, camera gimbals, and any project requiring precise mechanical actuation.