Translate

Arduino PWM: How to Fade an LED (Beginner Guide)

 Fading an LED is a step up from a simple "Blink" project. Instead of just turning the light ON or OFF, we use Pulse Width Modulation (PWM) to control the brightness levels smoothly.

What You Will Need

  • Arduino Uno (or any compatible board)

  • One LED (any color)

  • 220-ohm Resistor

  • Breadboard and Jumper Wires

The Circuit Diagram and Connections 



Connect your LED to Digital Pin 9. We use Pin 9 because it is one of the specific pins on the Arduino Uno that supports PWM (marked with a ~ symbol).

Wiring Tip: Connect the longer leg (Anode) of the LED to Pin 9 through the resistor, and the shorter leg (Cathode) to the GND pin.

The Arduino Code

Copy and paste this code into your Arduino IDE to see the fading effect:

int led = 9; // the PWM pin the LED is attached to
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by

void setup() {
   pinMode(led, OUTPUT);
}

void loop() {
  analogWrite(led, brightness); // set the brightness of pin 9
  brightness = brightness + fadeAmount;

  // Reverse the direction of the fading at the ends of the fade:
  if (brightness <= 0 || brightness >= 255) {
    fadeAmount = -fadeAmount;
  }
  delay(30); // Wait for 30 milliseconds to see the dimming effect
}

How it Works

The analogWrite() function tells the Arduino to send a specific "duty cycle" to the LED. By changing the brightness variable in small steps (fadeAmount), we create a smooth transition from dark to fully bright and back again.

തുടക്കക്കാർക്കായി ഇലക്ട്രോണിക്സ് ലളിതമായി പഠിക്കാം.

Empowering students in Kerala with hands-on technical skills.