PCB Punks Uncategorized Arduino non-blocking LED fader

Arduino non-blocking LED fader

0 Comments 10:18 am

Do you want to create a smooth fading effect on your Arduino’s LED, but without using delay()? You’re in the right place! In this article, we’ll show you how to build a non-blocking LED fader for Arduino. This technique lets you fade an LED up and down without pausing your main loop—perfect for responsive projects or when you need to handle multiple tasks at once.


Why Use a Non-Blocking LED Fader?

Using delay() in Arduino projects is easy, but it stops your whole program while it waits! If you want your Arduino to do more than just fade an LED (like reading sensors, handling buttons, or controlling other outputs), non-blocking code is the way to go.

A non-blocking LED fader uses millis() to track time, so your Arduino remains responsive. It can check buttons, communicate, or even fade multiple LEDs at once.

Arduino Non-Blocking LED Fader Code

Here’s a simple, effective example of a non-blocking LED fader for Arduino:

const int ledPin = 13;

int direction = 10;
bool needToFade = true;
int fadeValue = 0;

void fader();

void setup() {
  Serial.begin(9600);
}

void loop() {
  static uint32_t prevMillis = 0;
  if(millis() - prevMillis > 100 && needToFade) {
    prevMillis = millis();
    fader();  
  }
}

void fader() {
  if(fadeValue > 255 || fadeValue < 0) {
    direction *= -1;
  }
  analogWrite(ledPin, fadeValue);
  fadeValue += direction;
  Serial.print("Debug fade: ");
  Serial.println(fadeValue);
}

How the Non-Blocking Fader Works

Let’s break down how this Arduino non-blocking LED fader works:

  • No delay(): Instead of stopping everything with delay(), this code checks if 100 milliseconds have passed using millis(). If so, it updates the LED brightness.
  • Fading Logic: The fader() function increases or decreases the brightness (from 0 to 255 and back) by adjusting fadeValue. When it hits the maximum or minimum, the direction reverses, creating a smooth fade in and fade out effect.
  • Stay Responsive: Because the main loop() keeps running, you can add more code—sensor reads, button presses, communication, etc.—without interrupting the fade.

Benefits of Non-Blocking LED Fading

  • More Features: Run other code (like sensors or displays) at the same time as your fade effect.
  • No Freezing: The Arduino never “freezes” or gets stuck waiting.
  • Scalable: Easily expand to multiple LEDs or effects using similar logic.

How to Use This Code

  1. Connect an LED to pin 13 (with a resistor).
  2. Upload the code to your Arduino.
  3. Watch as the LED smoothly fades in and out—while your Arduino stays fully functional and ready for more tasks!

Tip: You can change ledPin to another PWM-capable pin if needed (like 3, 5, 6, 9, 10, or 11 on most Arduinos).

Conclusion

A non-blocking LED fader is a must-have skill for any Arduino maker who wants to keep their projects responsive and flexible. By avoiding delay() and using millis(), you unlock the full power of your Arduino and can build more complex, interactive projects.

Have questions or want to learn more about non-blocking code? Leave a comment below!

Leave a Reply

Your email address will not be published. Required fields are marked *