Arduino UNO R4 - Fade LED

This tutorial instructs you how to program Arduino UNO R4 to fade and dim LED. In detail, we will learn:

Arduino UNO R4 Fade LED

Hardware Preparation

1×Arduino UNO R4 WiFi
1×Arduino UNO R4 Minima (Alternatively)
1×USB Cable Type-C
1×LED
1×220 ohm resistor
1×Breadboard
1×Jumper Wires
1×(Recommended) Screw Terminal Block Shield for Arduino UNO R4
1×(Recommended) Breadboard Shield For Arduino UNO R4
1×(Recommended) Enclosure For Arduino UNO R4

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

Pinout

LED has two pins:

  • Cathode(-) pin: should be connected to GND (0V)
  • Anode(+) pin: is used to operate the LED's state
LED Pinout

How It Works

After connecting the negative side (cathode) to the ground (GND):

  • If you connect the ground (GND) to the positive side (anode) of the LED, it turns off.
  • If you connect the power supply (VCC) to the positive side (anode) of the LED, it turns on.
  • If you send a Pulse Width Modulation (PWM) signal to the positive side (anode) of the LED, you can change the brightness. The PWM value can be between 0 and 255. A higher PWM value makes the LED brighter, and a lower PWM value makes it dimmer.
    • If the PWM value is 0, it acts like connecting to GND, so the LED turns off.
    • If the PWM value is 255, it acts like connecting to VCC, so the LED is fully on.
    How LED Works

    ※ NOTE THAT:

    For most LEDs, you need to connect a resistor between the positive terminal (anode) and the power supply (VCC). The resistor value varies depending on the LED specifications.

    Arduino UNO R4 - fade LED

    We can make an LED fade/dim using some of the Arduino UNO R4 pins which can create a PWM signal. First, connect the positive pin (+) of the LED to one of the Arduino UNO R4's pins. Then, connect the negative pin (-) of the LED to the ground (GND). You can then set up the chosen Arduino pin to produce a PWM signal.

Wiring Diagram

The wiring diagram between Arduino UNO R4 LED

This image is created using Fritzing. Click to enlarge image

How To Program

  • Set the pin mode of an Arduino UNO R4 to digital output using the pinMode() function. For instance, for pin 9:
pinMode(9, OUTPUT);
  • Adjust the LED brightness by using the analogWrite() function to create the appropriate PWM signal.
analogWrite(9, brightness);

The brightness can range from 0 to 255.

Arduino UNO R4 Code - Fade Example from Arduino IDE

/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-fade-led */ #define LED_PIN 9 // The Arduino UNO R4 pin connected to the LED int brightness = 0; // how bright the LED is int fade_step = 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_PIN, OUTPUT); } // the loop routine runs over and over again forever: 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

Follow these instructions step by step:

  • If this is your first time using the Arduino Uno R4 WiFi/Minima, refer to the tutorial on setting up the environment for Arduino Uno R4 WiFi/Minima in the Arduino IDE.
  • Connect LED to the Arduino Uno R4 according to the provided diagram.
  • Connect the Arduino Uno R4 board to your computer using a USB cable.
  • Launch the Arduino IDE on your computer.
  • Select the appropriate Arduino Uno R4 board (e.g., Arduino Uno R4 WiFi) and COM port.
  • Copy the above code and paste it into the Arduino IDE.
  • Click the Upload button in Arduino IDE to send code to Arduino UNO R4.
Arduino IDE Upload Code
  • Look at how bright the LED is.

Code Explanation

The explanation is in the comments of the Arduino code above.

※ NOTE THAT:

In the example above, we use a function called delay() to slowly make the light brighter and dimmer. However, using delay() makes the light change less smoothly and stops other parts of the program from working while it waits. In the next part, we will learn how to make the light change smoothly without stopping the rest of the program by using the millis() function.

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

/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-fade-led */ #define LED_PIN 9 // The Arduino UNO R4 pin connected to the LED #define FADE_PEDIOD 3000 // fade time is 3 seconds unsigned long fade_start_ms; // the setup routine runs once when you press reset void setup() { pinMode(LED_PIN, OUTPUT); // declare pin 9 to be an output fade_start_ms = millis(); } // fade-in in loop, and restart after finishing void loop() { unsigned long progress = millis() - fade_start_ms; if (progress <= FADE_PEDIOD) { long brightness = map(progress, 0, FADE_PEDIOD, 0, 255); analogWrite(LED_PIN, brightness); } else { fade_start_ms = millis(); // restart fade again } }

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

/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-fade-led */ #define LED_PIN 9 // The Arduino UNO R4 pin connected to the LED #define FADE_PEDIOD 3000 // fade time is 3 seconds unsigned long fade_start_ms; // the setup routine runs once when you press reset void setup() { pinMode(LED_PIN, OUTPUT); // declare pin 9 to be an output fade_start_ms = millis(); } // fade-in in loop, and restart after finishing void loop() { unsigned long progress = millis() - fade_start_ms; if (progress <= FADE_PEDIOD) { long brightness = map(progress, 0, FADE_PEDIOD, 0, 255); analogWrite(LED_PIN, brightness); } else { fade_start_ms = millis(); // restart fade again } }

Video Tutorial

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!