Introduction
One of the most fascinating features of Arduino is its
ability to simulate analog signals using Pulse Width Modulation (PWM).
Although the Arduino Uno (and most microcontrollers) doesn’t have true analog
output pins, PWM allows us to control devices as if we had analog voltages
available.
In this post, we’ll explore how Pin 11 on Arduino Uno
works as a PWM pin, how to use it to control the brightness of a Red LED with a
330 Ω resistor, and dive into the theory and functions of PWM in detail.
By the end, you’ll understand not just how to wire and code, but also the inner
workings of PWM signals and why they’re so powerful in embedded systems.
Components Required
- Arduino
Uno (or compatible board)
- 1 ×
Red LED
- 1 ×
330 Ω resistor
- Breadboard
and jumper wires
Circuit Connection
- Connect
Pin 11 → 330 Ω resistor → LED anode (long leg).
- Connect
LED cathode (short leg) → GND.
This simple circuit ensures the LED is protected from excess current while allowing us to control its brightness.
Arduino Code
// PWM LED Brightness Control on Pin 11
#define outputAnalog 11
#define delay1 1000
void setup(){
pinMode(outputAnalog, OUTPUT);
analogWrite(outputAnalog, 0);
}
void loop(){
analogWrite(outputAnalog, 0);
delay(delay1);
analogWrite(outputAnalog, 50);
delay(delay1);
analogWrite(outputAnalog, 100);
delay(delay1);
analogWrite(outputAnalog, 150);
delay(delay1);
analogWrite(outputAnalog, 200);
delay(delay1);
analogWrite(outputAnalog, 255);
delay(delay1);
}
How Pin 11 Works as PWM
On Arduino Uno, Pin 11 is one of the six PWM-capable
pins (3, 5, 6, 9, 10, 11). These pins are marked with a tilde (~) symbol.
Internally, Pin 11 uses Timer2 to generate PWM
signals at a frequency of ~490 Hz. When you call analogWrite(11, value), the
Arduino sets the duty cycle of the PWM signal:
- value
= 0 → always LOW (LED OFF)
- value
= 255 → always HIGH (LED fully ON)
- Any
value in between → LED brightness proportional to duty cycle
Thus, Pin 11 acts like an analog output pin even
though it’s digital, thanks to PWM.
PWM in Detail
1. What is PWM?
PWM stands for Pulse Width Modulation. It’s a
technique where a digital pin rapidly switches between HIGH and LOW states. By
adjusting the width of the HIGH pulse (duty cycle), we control the average
voltage delivered to a device.
For example:
- At
25% duty cycle, the pin is HIGH for 25% of the time and LOW for 75%. The
average voltage is ~1.25V (on a 5V system).
- At
75% duty cycle, the average voltage is ~3.75V.
This average voltage is what devices like LEDs or motors
respond to, so it feels like a smooth analog output.
2. Duty Cycle
The duty cycle is the percentage of time the signal
stays HIGH in one cycle.
Duty Cycle = (Time HIGH / Total Period) x 100
- 0%
duty cycle → always LOW → 0V average
- 50%
duty cycle → half HIGH, half LOW → ~2.5V average
- 100%
duty cycle → always HIGH → 5V average
3. Frequency
PWM signals also have a frequency, which is how fast
the pin switches between HIGH and LOW.
On Arduino Uno:
- Pins
3, 9, 10, 11 → ~490 Hz
- Pins
5, 6 → ~980 Hz
This frequency is high enough that the human eye cannot
detect flicker, so LEDs appear to glow steadily.
4. Resolution
Arduino Uno uses 8-bit resolution for PWM. That means
duty cycle values range from 0 to 255.
- analogWrite(pin,
0) → 0% duty cycle
- analogWrite(pin,
127) → ~50% duty cycle
- analogWrite(pin,
255) → 100% duty cycle
This gives 256 possible brightness levels for an LED.
5. Why PWM Works as Analog Output
Even though PWM is digital, devices like LEDs and motors
respond to the average power delivered.
- LEDs
integrate the rapid ON/OFF switching into perceived brightness.
- Motors
integrate the pulses into torque and speed.
- Audio
circuits can even use PWM to approximate analog waveforms.
Thus, PWM is a clever way to simulate analog output without
needing a true DAC (Digital-to-Analog Converter).
Applications of PWM
PWM is everywhere in embedded systems. Some common uses
include:
- LED
Dimming → Smooth brightness control.
- Motor
Speed Control → Adjusting duty cycle changes motor speed.
- Servo
Control → Special PWM signals control servo position.
- Audio
Generation → PWM can approximate sound waves.
- Power
Regulation → Used in switching power supplies.
Advanced PWM Functions in Arduino
Arduino provides the simple analogWrite() function, but PWM
can be customized further:
- Changing
Frequency → By reprogramming timers, you can adjust PWM frequency for
specific applications (e.g., motor control needs higher frequency).
- Phase-Correct
PWM → Ensures symmetrical pulses, useful in motor drivers.
- Fast
PWM Mode → Provides higher resolution and faster updates.
- Timer
Interrupts → Allow precise control over PWM timing.
For most beginners, analogWrite() is sufficient, but
advanced users can dive into timer registers for fine-grained control.
Example: Breathing LED Effect
The code above creates a breathing LED effect. This
is achieved by gradually increasing and decreasing the duty cycle. The LED
appears to fade in and out smoothly, demonstrating how PWM simulates analog
brightness.
Key Notes
- Always
use a current-limiting resistor (330 Ω is ideal for a Red LED).
- Pin
11 uses Timer2, so changing timer settings can affect PWM behaviour.
- PWM
is not true analog, but for LEDs, motors, and many sensors, it works
perfectly.
- Other
PWM pins on Arduino Uno: 3, 5, 6, 9, 10, 11.
Conclusion
PWM is one of the most versatile tools in embedded systems.
By using Pin 11 on Arduino Uno, we can control the brightness of a Red
LED with just a few lines of code. More importantly, understanding how PWM
works — duty cycle, frequency, resolution, and average voltage — gives us the
foundation to control a wide range of devices.
From LED dimming to motor control, audio generation to power
regulation, PWM is the bridge between digital microcontrollers and the analog
world.
So next time you call analogWrite(11, value), remember:
you’re not just turning a pin ON or OFF — you’re simulating analog output with
precision and elegance.
