Understanding PWM pins on Arduino Uno unlocks a world of analog simulation using a digital device. Pulse Width Modulation is a technique that creates a square wave signal, varying the duty cycle to effectively control average voltage. This allows you to regulate the speed of a motor, the brightness of an LED, or the position of a servo without complex circuitry. The Uno’s built-in functionality makes this method energy efficient, as the component is either fully on or fully off, minimizing power loss as heat.
Hardware PWM Capabilities
The Arduino Uno is based on the ATmega328P microcontroller, which includes three dedicated hardware PWM timers. These timers run independently of the main code, ensuring precise timing without software overhead. Unlike bit-banged solutions, hardware PWM generates a stable signal even when the processor is busy with other tasks. This reliability is crucial for applications requiring consistent performance, such as motor control or audio synthesis.
Pins and Timer Allocation
On the Uno board, specific pins are designated for PWM output, clearly marked with a tilde (~) symbol. Pins 3 and 11 are managed by Timer 2, while pins 5, 6, 9, and 10 are handled by Timer 0 and Timer 1. This allocation means that certain pins share the same timer; consequently, changing the frequency on one pin affects all pins driven by that same timer. The standard frequency for these pins is approximately 490 Hz or 980 Hz, depending on the timer configuration.
Uno PWM Pin | Timer | Default Frequency
3, 11 | Timer 2 | 980 Hz
5, 6, 9, 10 | Timer 0 & Timer 1 | 490 Hz
Adjusting Duty Cycle and Brightness
To set the intensity of an LED, you use the analogWrite function, which accepts a value between 0 and 255. A value of 0 corresponds to a 0% duty cycle, turning the pin completely off, while 255 represents 100% duty cycle, applying maximum voltage continuously. Values like 128 provide roughly 50% duty cycle, resulting in half the perceived brightness. This simple interface abstracts the underlying timer registers, allowing for quick prototyping without deep register-level knowledge.
Motor Control and Servo Management
Beyond lighting, PWM is essential for controlling DC motors and servo motors. For motors, an H-bridge circuit is typically used to handle direction and speed, while the PWM signal dictates the velocity. Smooth operation requires a frequency high enough to avoid audible buzzing in the motor coils, which the Uno’s default settings generally satisfy. For servos, the library generates a specific 50 Hz signal where the pulse width (between 1 ms and 2 ms) determines the angle, making precise positioning straightforward via the Servo library.
Frequency and Resolution Considerations
Advanced users may wish to manipulate the timer registers directly to alter the PWM frequency or bit resolution. Increasing the frequency can eliminate motor whine or flicker in lighting applications, but it reduces the available resolution for the duty cycle. Conversely, lowering the frequency increases the granularity of the analog-like output, allowing for finer control of slow processes. These configurations require careful calculation to balance timing and precision, offering a powerful level of control for demanding projects.