Arduino MKR WiFi 1010 - LED - Fade

Create smooth LED fading effects with your Arduino MKR WiFi 1010! Learn to control LED brightness using PWM (Pulse Width Modulation), creating professional-looking fade-in and fade-out animations. Master both blocking delay() and non-blocking millis() techniques for smooth, responsive LED control.

Arduino MKR WiFi 1010 fades LED

What You'll Learn:

Real-World Applications:

Hardware Preparation

1×Arduino MKR WiFi 1010
1×Micro USB Cable
1×LED Kit
1×LED (red)
1×LED Module
1×220Ω Resistor
1×Breadboard
1×Jumper Wires
1×Optionally, DC Power Jack

Or you can buy the following 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 .

Buy Note: Use the LED Module for easier wiring. It includes an integrated resistor.

Overview of LED

LED Pinout

An LED has two metal legs:

  • Cathode (-) pin: Plug this pin into the ground (0V).
  • Anode (+) pin: Use this pin to turn the LED on or off.
LED Pinout

How LED Works

After connecting the negative (-) side to the ground:

  • Connect GND (ground) to the positive (anode) side, and the LED will be off.
  • Connect VCC (power) to the positive side, and the LED will be on.
  • Apply a PWM (Pulse Width Modulation) signal to the positive side, and the LED's brightness will change depending on the PWM duty cycle. The duty cycle can be from 0 to 255—the higher the number, the brighter the LED.
    • A PWM value of 0 acts like connecting to GND, so the LED stays off.
    • A PWM value of 255 acts like connecting to VCC, so the LED shines at full brightness.
    How LED Works

    ※ NOTE THAT:

    Most of the time, you need a resistor to keep the LED from burning out. The resistor’s value depends on the LED's specifications.

    Arduino MKR WiFi 1010 - fade LED

    The digital output pins on the Arduino MKR WiFi 1010 can be set to create a PWM signal. By connecting the positive side of an LED to a pin on the Arduino MKR WiFi 1010 and the negative side to the ground, and programming the Arduino to change the PWM duty cycle, you can make the LED slowly turn on and off.

Wiring Diagram between LED and Arduino MKR WiFi 1010

The wiring diagram between Arduino MKR WiFi 1010 LED

This image is created using Fritzing. Click to enlarge image

How To Program LED Fading

Step 1: Configure Pin as Output

Set up an Arduino MKR WiFi 1010 pin as an output using pinMode():

pinMode(5, OUTPUT); // Configure pin D5 as output

Step 2: Control LED Brightness with PWM

Use analogWrite() to set LED brightness (0-255):

analogWrite(5, brightness); // brightness: 0 (off) to 255 (max)

Creating Fade Effects:

Method 1 - Fade In (using delay()):

for (int brightness = 0; brightness <= 255; brightness++) { analogWrite(5, brightness); // Increase brightness delay(10); // Wait 10ms between steps } // Total fade-in time: 255 steps × 10ms = 2.55 seconds

Method 2 - Fade Out (using delay()):

for (int brightness = 255; brightness >= 0; brightness--) { analogWrite(5, brightness); // Decrease brightness delay(10); // Wait 10ms between steps }

Method 3 - Non-Blocking Fade (using millis()):

See advanced examples below for multitasking-friendly fading that doesn't block other code.

Key Parameters:

  • Brightness range: 0-255 (8-bit resolution)
  • Step size: Larger steps = faster but choppier fade (try 1, 5, or 10)
  • Delay time: Larger delays = slower fade (try 5ms, 10ms, 20ms)

Arduino MKR WiFi 1010 Code - Simple Fade Example

Detailed Instructions

New to Arduino MKR WiFi 1010? Complete our Getting Started with Arduino MKR WiFi 1010 tutorial first to set up your development environment.

  • Connect the components to the Arduino MKR WiFi 1010 board as depicted in the diagram
  • Plug your Arduino MKR WiFi 1010 into your computer's USB port
  • Launch the Arduino IDE on your computer
  • Select the Arduino MKR WiFi 1010 board and its COM port
  • Copy the code below and paste it in the Arduino program.
/* * This Arduino MKR WiFi 1010 code was developed by newbiely.com * * This Arduino MKR WiFi 1010 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mkr/arduino-mkr-wifi-1010-led-fade */ #define LED_PIN 5 // The Arduino Nano 33 IoT pin D5 connected to LED int brightness = 0; // how bright the LED is int fade_amount = 5; // how many points to fade the LED by // The setup function runs once on reset or power-up void setup() { // configure the Arduino Nano 33 IoT pin as an output: pinMode(LED_PIN, OUTPUT); } // The loop function repeats indefinitely. void loop() { // set the brightness analogWrite(LED_PIN, brightness); // change the brightness for next time through the loop: brightness = brightness + fade_amount; // reverse the direction of the fading at the ends of the fade: if (brightness <= 0 || brightness >= 255) { fade_amount = -fade_amount; } // wait for 30 milliseconds to see the dimming effect delay(30); }
  • Compile your code and send it to your Arduino MKR WiFi 1010 board by clicking the Upload button in the Arduino software.
Arduino IDE Upload Code
  • Look at how bright the LED is.

Line-by-line Code Explanation

The Arduino MKR WiFi 1010 code above has an explanation for each line. Please check the notes in the code!

※ NOTE THAT:

The example above uses the delay() function to slowly turn the LED on and off. However, delay() does not make the fading smooth and stops other parts of your program from running at the same time. In the next sections, we will learn how to create a smooth fade with the LED without stopping other code, by using the millis() function.

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

/* * This Arduino MKR WiFi 1010 code was developed by newbiely.com * * This Arduino MKR WiFi 1010 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mkr/arduino-mkr-wifi-1010-led-fade */ #define LED_PIN 5 // The Arduino Nano 33 IoT pin D5 connected to LED #define FADE_PEDIOD 3000 // fade time is 3 seconds unsigned long start_time; // The setup routine runs once when you press reset void setup() { pinMode(LED_PIN, OUTPUT); // configure the Arduino Nano 33 IoT pin as 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 MKR WiFi 1010 code was developed by newbiely.com * * This Arduino MKR WiFi 1010 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mkr/arduino-mkr-wifi-1010-led-fade */ #define LED_PIN 5 // The Arduino Nano 33 IoT pin D5 connected to LED #define FADE_PEDIOD 3000 // fade time is 3 seconds unsigned long start_time; // The setup routine runs once when you press reset void setup() { pinMode(LED_PIN, OUTPUT); // configure the Arduino Nano 33 IoT pin as 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 } }

Code Explanation - Non-Blocking Fade

The Problem with delay():

Using delay() pauses the entire program. During fading, your Arduino can't:

  • Read sensors
  • Respond to buttons
  • Communicate via Serial
  • Control other components

The millis() Solution:

The millis() function returns milliseconds since Arduino started. By checking elapsed time without blocking, you can fade LEDs while doing other tasks simultaneously.

Key Concept - Elapsed Time Tracking:

unsigned long previousMillis = 0; // Store last update time const long FADE_DURATION = 3000; // 3 seconds fade time void loop() { unsigned long currentMillis = millis(); unsigned long elapsed = currentMillis - previousMillis; // Map elapsed time (0-3000ms) to brightness (0-255) int brightness = map(elapsed, 0, FADE_DURATION, 0, 255); if (elapsed >= FADE_DURATION) { previousMillis = currentMillis; // Reset timer for next fade } }

Advantages of millis() Approach:

  • Non-blocking: Code continues running
  • Precise timing: Accurate fade duration
  • Multitasking: Handle multiple fades, sensors, buttons simultaneously
  • Professional: Industry-standard technique

Challenge Yourself

Ready to explore more LED fading with your Arduino MKR WiFi 1010? Try these creative experiments:

  1. Rainbow Breathing: Connect RGB LED (3 PWM pins). Create smooth color-changing breathing effect cycling through rainbow hues.
  2. Heartbeat Pattern: Make LED pulse like a heartbeat—two quick pulses, then pause. Adjust timing for realistic effect.
  3. Button-Controlled Fade Speed: Use potentiometer to control fade speed in real-time (map 0-1023 to delay values).
  4. Multi-LED Wave: Connect 5 LEDs. Create wave effect where fade travels from LED 1→5→repeat. Use millis() for smooth animation.
  5. Sensor-Responsive Brightness: Use light sensor (LDR)—LED brightness inversely proportional to ambient light (dim room = bright LED).
  6. Morse Code Fade: Instead of sharp blinks, use smooth fades for dots and dashes. Spell "SOS" or your name.
  7. Music Visualizer: Connect microphone module—LED brightness follows sound amplitude. Create audio-reactive lighting.
  8. Sunrise Simulator: Create 15-minute sunrise effect (very slow fade from off to bright). Wake-up light alarm clock.

Video Tutorial

Language 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!