Arduino - LED - Fade

Hardware Required

1×Arduino UNO or Genuino UNO
1×USB 2.0 cable type A/B
1×220 ohm resistor
2×Jumper Wires
Please note: These are affiliate links. If you buy the components through these links, We may get a commission at no extra cost to you. We appreciate it.

About LED


LED includes two pins:

  • Cathode(-) pin needs to be connected to GND (0V)
  • Anode(+) pin is used to control LED's state
  • LED Pinout

How It Works

After connecting the cathode(-) to GND:

  • If connecting GND to the anode(+), LED is OFF.
  • If connecting VCC to the anode(+), LED is ON.
  • If generating a PWM signal to the anode(+), the brightness of LED is changed according to PWM value. PWM value varies from 0 to 255. The bigger PWM value is, the brighter LED is. The smaller PWM value is, the darker LED is.
    • If PWM value is 0, it is equivalent to GND, therefore, LED is OFF
    • If PWM value is 255, it is equivalent to VCC, therefore, LED is fully ON
    • How LED Works


For most of LED, it needs to use a resistor between the anode(+) and VCC. The value of the resistor depends on the specification of LED.

Arduino - fade LED

Some of Arduino pins can be programmed to generate PWM signal. We can fade LED by connecting LED's anode(+) pin to an Arduino's pin, LED's cathode(-) to GND, and programming generate PWM on the Arduino's pin.

Wiring Diagram

Arduino LED Wiring Diagram

Image is developed using Fritzing. Click to enlarge image

How To Program

  • Configure an Arduino's pin to the digital output mode by using pinMode() function. For example, pin 9:
pinMode(9, OUTPUT);
  • Set brightness of LED by generating the corresponding PWM signal by using analogWrite() function:
analogWrite(9, brightness);

Where brightness is a value from 0 to 255.

Arduino Code

Quick Steps

  • Connect Arduino to PC via USB cable
  • Open Arduino IDE, select the right board and port
  • On Arduino IDE, Go to File Examples 01.Basics Fade example
/* Fade This example shows how to fade an LED on pin 9 using the analogWrite() function. The analogWrite() function uses PWM, so if you want to change the pin you're using, be sure to use another PWM capable pin. On most Arduino, the PWM pins are identified with a "~" sign, like ~3, ~5, ~6, ~9, ~10 and ~11. This example code is in the public domain. */ 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 // the setup routine runs once when you press reset: void setup() { // declare pin 9 to be an output: pinMode(led, OUTPUT); } // the loop routine runs over and over again forever: void loop() { // set the brightness of pin 9: analogWrite(led, brightness); // change the brightness for next time through the loop: brightness = brightness + fadeAmount; // reverse the direction of the fading at the ends of the fade: if (brightness <= 0 || brightness >= 255) { fadeAmount = -fadeAmount; } // wait for 30 milliseconds to see the dimming effect delay(30); }
  • Click Upload button on Arduino IDE to upload code to Arduino
  • Arduino IDE Upload Code
  • See the brightness of LED

Code Explanation

Read the line-by-line explanation in comment lines of code!

Video Tutorial

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.

Challenge Yourself

Change the brightness of LED using potentiometer . Hint: Refer to Arduino - Potentiometer .

Additional Knowledge

  • PWM signal generated by analogWrite() function fades a LED. That is because it's high-frequency PWM. If we create a customized function (required advanced knowledge), which generates low-frequency PWM signal, LED will be blinked instead of faded.
  • Summary: PWM signal can be used in Arduino to: control servo motor, control DC motor, make sound using a piezo buzzer, fade LED, blink LED ...

LED on Commercial Products

Small LEDs usually are used to indicate the status of devices. For examples:

Big LEDs usually are used for lighting. They can be combined into groups. For examples:

The Best Arduino Starter Kit