LED Dimming with analogWrite

Learn how to control LED brightness using PWM.

ARDUINO LESSONSProgrammingLED ProjectsPWM
S
Shaun Sosi
How to Dim an LED with Arduino's analogWrite

Understanding analogWrite and PWM

While analogWrite might suggest it produces an analog signal, it actually uses a clever technique called PWM (Pulse Width Modulation) to simulate varying voltages.

PWM LED Dimming Demonstration
Visualizing PWM signals and LED brightness control

Think of PWM Like This:

Imagine a light switch being flicked on and off so fast that instead of seeing flickers, you perceive different levels of brightness based on how long the light stays on versus off.

Get Your Arduino Kit

To follow along with this LED dimming tutorial:

How PWM Works

PWM Basics:

PWM creates a digital signal that switches between HIGH (5V) and LOW (0V) very rapidly. The ratio of "on time" to "off time" determines the perceived brightness.

  • Rapid Switching - Signal switches between HIGH and LOW at high frequency
  • Duty Cycle - Percentage of time the signal stays HIGH in each cycle
  • Perception - Our eyes see the rapid switching as varying brightness
  • Efficiency - More energy-efficient than true analog voltage control

Understanding Duty Cycle

The duty cycle determines the LED's brightness:

  • 0% Duty Cycle - LED completely off (analogWrite(0))
  • 50% Duty Cycle - LED at half brightness (analogWrite(127))
  • 100% Duty Cycle - LED at full brightness (analogWrite(255))

8-Bit Resolution:

  • Value Range - 0 to 255 (256 different levels)
  • Binary - 8 bits can represent numbers from 0 to 255
  • Precision - Allows for fine control over LED brightness

Using analogWrite

Here's how to implement LED dimming:

LEDDimming.ino
const int ledPin = 9; // Must be a PWM pin (~) void setup() { pinMode(ledPin, OUTPUT); // Set LED pin as output } void loop() { // Gradually increase brightness for(int brightness = 0; brightness <= 255; brightness++) { analogWrite(ledPin, brightness); delay(10); // Small delay for visible effect } // Gradually decrease brightness for(int brightness = 255; brightness >= 0; brightness--) { analogWrite(ledPin, brightness); delay(10); } }

    Important Considerations:

    • PWM Pins - Only certain pins support PWM (marked with ~)
    • Frequency - PWM frequency varies by pin on Arduino boards
    • Current Limits - Still respect the maximum current ratings
    • Resistor Required - Always use a current-limiting resistor

    Common Applications

    PWM and analogWrite are used for:

    • LED Effects - Create smooth fading and breathing effects
    • Motor Control - Adjust motor speed smoothly
    • Sound Generation - Create simple tones and music
    • Display Dimming - Control backlight brightness
    BreathingLED.ino
    const int ledPin = 9; void setup() { pinMode(ledPin, OUTPUT); } void loop() { // Breathing effect using sine wave float val = (exp(sin(millis()/2000.0*PI)) - 0.36787944)*108.0; analogWrite(ledPin, val); }

      Troubleshooting Tips

      Common issues and solutions:

      • LED Not Dimming - Verify you're using a PWM-capable pin (~)
      • Flickering - Check if your PWM frequency is appropriate
      • No Response - Ensure proper resistor and connections
      • Uneven Dimming - Consider using logarithmic scaling for more natural brightness changes

      Pro Tip:

      For smoother dimming effects, use more steps in your brightness transitions and consider the human eye's logarithmic perception of brightness.

      Looking Ahead

      Now that you understand PWM and LED dimming:

      • Advanced Effects - Create complex lighting patterns
      • Multiple LEDs - Control multiple LEDs with different brightness levels
      • RGB Control - Apply PWM to create custom colors
      • Interactive Projects - Use sensors to control LED brightness

      Coming Up Next:

      In our next episode, we'll explore more advanced Arduino programming concepts and create even more exciting projects!

      Connect With Me

      Support Our Work

      Help us create more amazing content

      $

      Your contribution helps us create more amazing content. Thank you! 💖