Arduino Nano - LED - Fade

This tutorial instructs you how to program Arduino Nano to fade LED. We will go through three examples and compare the differences between them:

Hardware Preparation

1×Arduino Nano
1×USB A to Mini-B USB cable
1×LED
1×220 ohm resistor
1×Breadboard
1×Jumper Wires
1×(Recommended) Screw Terminal Expansion Board for Arduino Nano
1×(Recommended) Breakout Expansion Board for Arduino Nano

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of LED

The LED Pinout

LED has two pins:

  • Cathode(-) pin: must be attached to GND (0V)
  • Anode(+) pin: used for controlling LED's state
LED pinout

How It Works

Once the cathode(-) is connected to GND:

  • If GND is connected to the anode(+), the LED will be OFF.
  • If VCC is connected to the anode(+), the LED will be ON.
  • If a PWM signal is applied to the anode(+), the brightness of the LED will be adjusted according to the PWM value, which ranges from 0 to 255. A larger PWM value will result in a brighter LED, while a smaller PWM value will result in a darker LED.
    • When the PWM value is 0, it is equivalent to GND, thus the LED will be OFF.
    • When the PWM value is 255, it is equivalent to VCC, thus the LED will be fully ON.
    How LED Works

    ※ NOTE THAT:

    For the majority of LEDs, a resistor must be placed between the anode(+) and VCC. The resistor's value is dependent on the LED's specifications.

    Arduino Nano - fade LED

    Some of the pins on an Arduino Nano can be programmed to generate a PWM signal. To fade an LED, we can connect the anode (+) pin of the LED to an Arduino Nano pin, the cathode (-) to GND, and program the Arduino Nano pin to generate PWM.

Wiring Diagram

The wiring diagram between Arduino Nano and LED

This image is created using Fritzing. Click to enlarge image

How To Program

  • Set up a digital output on an Arduino's pin by using the pinMode() function.
  • For example, this could be done for pin 5:
pinMode(5, OUTPUT);
  • Adjust the brightness of the LED by creating a PWM signal with the analogWrite() function:
analogWrite(5, brightness);

The brightness can range from 0 to 255.

Arduino Nano Code - Fade LED

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-led-fade */ #define LED_PIN 5 // The Arduino PWM pin the LED is attached to int brightness = 0; // how bright the LED is int fade_step = 5; // how many points to fade the LED by // The setup function runs once on reset or power-up void setup() { // declare pin 9 to be an output: pinMode(LED_PIN, OUTPUT); } // The loop function repeats indefinitely. void loop() { // set the brightness of pin 9: analogWrite(LED_PIN, brightness); // change the brightness for next time through the loop: brightness = brightness + fade_step; // reverse the direction of the fading at the ends of the fade: if (brightness <= 0 || brightness >= 255) { fade_step = -fade_step; } // wait for 30 milliseconds to see the dimming effect delay(30); }

Detailed Instructions

  • Attach Arduino Nano to the computer using a USB cable.
  • Open the Arduino IDE, select the correct board and port.
  • Copy the above code and open it in the Arduino IDE.
  • Click the Upload button on Arduino IDE to compile and upload the code to the Arduino Nano board.
Arduino IDE Upload Code
  • Check out the luminosity of the LED.

Code Explanation

Check out the line-by-line explanation contained in the comments of the source code!

※ NOTE THAT:

The example above makes use of the delay() function to create a fade-in and fade-out effect. However, this method is not very smooth and prevents other code from running. In the upcoming sections, we will learn how to achieve a smooth fade-in and fade-out without blocking other code by using the millis() function.

How to fade-in LED in a period without using delay()

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-led-fade */ #define LED_PIN 5 // the Arduino PWM pin the LED is attached to #define FADE_PEDIOD 3000 // fade time is 3 seconds unsigned long start_time; // The setup function runs once on reset or power-up void setup() { pinMode(LED_PIN, OUTPUT); // declare pin 5 to be an output start_time = millis(); } // fade-in in loop, and restart after finishing void loop() { unsigned long progress = millis() - start_time; if (progress <= FADE_PEDIOD) { long brightness = map(progress, 0, FADE_PEDIOD, 0, 255); analogWrite(LED_PIN, brightness); } else { start_time = millis(); // restart fade again } }

How to fade-out LED in a period without using delay()

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-led-fade */ #define LED_PIN 5 // the Arduino PWM pin the LED is attached to #define FADE_PEDIOD 3000 // fade time is 3 seconds unsigned long start_time; // The setup function runs once on reset or power-up void setup() { pinMode(LED_PIN, OUTPUT); // declare pin 5 to be an output start_time = millis(); } // fade-out in loop, and restart after finishing void loop() { unsigned long progress = millis() - start_time; if (progress <= FADE_PEDIOD) { long brightness = 255 - map(progress, 0, FADE_PEDIOD, 0, 255); analogWrite(LED_PIN, brightness); } else { start_time = millis(); // restart fade again } }

Video Tutorial

Challenge Yourself

Adjust the brightness of the LED with a potentiometer. Hint: Have a look at Arduino Nano - Potentiometer for more information.

Additional Knowledge

  • The analogWrite() function in Arduino Nano generates a PWM signal which causes a LED to fade. However, if one has the advanced knowledge to create a custom function that produces a low-frequency PWM signal, the LED will be blinked instead of faded.
  • Summary: PWM signals generated by Arduino Nano can be used for a variety of purposes, such as controlling servo motors, DC motors, producing sound with a piezo buzzer, fading LEDs, and blinking LEDs.

Function References

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!