Arduino - LED Library

This library is designed for Arduino, ESP32, ESP8266... to control LED: on, off, toggle, fade in/out, blink, blink the number of times, blink in a period of time. It is designed for not only beginners but also experienced users.

Arduino LED Library

Library Features

  • Turn on/off
  • Toggle between on and off
  • Fade in/out
  • Blink
  • Blink with the number of times
  • Blink in a period of time
  • Cancel the blinking or fading anytime
  • Support both control modes: CTRL_ANODE and CTRL_CATHODE
  • Get the on/off LED's states: LED_OFF, LED_ON
  • Get the operation LED's state: LED_IDLE, LED_DELAY, LED_FADING, LED_BLINKING
  • All functions are non-blocking (without using delay() function)
  • Easy to use with multiple LEDs

Available Functions

  • ezLED(int pin)
  • ezLED(int pin, int mode)
  • void turnON()
  • void turnON(unsigned long delayTime)
  • void turnOFF()
  • void turnOFF(unsigned long delayTime)
  • void toggle()
  • void toggle(unsigned long delayTime)
  • void fade(int fadeFrom, int fadeTo, unsigned long fadeTime)
  • void fade(int fadeFrom, int fadeTo, unsigned long fadeTime, unsigned long delayTime)
  • void blink(unsigned long onTime, unsigned long offTime)
  • void blink(unsigned long onTime, unsigned long offTime, unsigned long delayTime)
  • void blinkInPeriod(unsigned long onTime, unsigned long offTime, unsigned long blinkTime)
  • void blinkInPeriod(unsigned long onTime, unsigned long offTime, unsigned long blinkTime, unsigned long delayTime)
  • void blinkNumberOfTimes(unsigned long onTime, unsigned long offTime, unsigned int numberOfTimes)
  • void blinkNumberOfTimes(unsigned long onTime, unsigned long offTime, unsigned int numberOfTimes, unsigned long delayTime)
  • void cancel(void)
  • int getOnOff(void)
  • int getState(void)
  • void loop(void)

Available Examples

※ NOTE THAT:

If this library is useful for your work, you can say thanks to us by buying us a coffee: PayPal.Me/arduinogetstarted . We appreciate it.

How To Install Library

  • Navigate to the Libraries icon on the left bar of the Arduino IDE.
  • Search “ezLED”, then find the led library by ArduinoGetStarted
  • Click Install button to install ezLED library.
Arduino led library

Or you can download it on Github

References

ezLED()

Description

This function creates a new instance of the ezLED class that represents a particular led pin of your Arduino board.

Syntax

ezLED(int pin)
ezLED(int pin, int mode)

Parameters

  • pin: Arduino's pin that connects to LED.
  • mode: (Optional)The controlling mode. There are two mode:
    • CTRL_ANODE: The LED's Anode is connected to Arduino Pin, The LED's Cathode is connected to GND. This is default mode if mode is not set
    • CTRL_CATHODE: The LED's Cathode is connected to Arduino Pin, The LED's Anode is connected to VCC.

    Return

    A new instance of the ezLED class.

    Example

    ezLED led(7);

    turnON()

    Description

    This function turns LED on.

    Syntax

    void turnON()
    void turnON(unsigned long delayTime)

    Parameters

    • delayTime: (Optional) The time (in ms) delay before turning LED ON. If not set, LED is turned ON immediately. This function is non-blocking even if the delayTime is set.

    Return

    None

    Example

    #include <ezLED.h> // ezLED library ezLED led(9); // create ezLED object that attach to pin 9 void setup() { led.turnON(); } void loop() { led.loop(); }

    turnOFF()

    Description

    This function turns LED OFF.

    Syntax

    void turnOFF()
    void turnOFF(unsigned long delayTime)

    Parameters

    • delayTime: (Optional) The time (in ms) delay before turning LED OFF. If not set, LED is turned OFF immediately. This function is non-blocking even if the delayTime is set.

    Return

    None

    Example

    #include <ezLED.h> // ezLED library ezLED led(9); // create ezLED object that attach to pin 9 void setup() { led.turnOFF(); } void loop() { led.loop(); }

    toggle()

    Description

    This function toggles the state of LED. If the currect state is ON, turn LED off, and vice versa.

    Syntax

    void toggle()
    void toggle(unsigned long delayTime)

    Parameters

    • delayTime: (Optional) The time (in ms) delay before toggling LED . If not set, LED is toggled immediately. This function is non-blocking even if the delayTime is set.

    Return

    None

    Example

    #include <ezLED.h> // ezLED library ezLED led(9); // create ezLED object that attach to pin 9 void setup() { led.toggle(); } void loop() { led.loop(); }

    fade()

    Description

    This function fades in/out LED.

    Syntax

    void fade(int fadeFrom, int fadeTo, unsigned long fadeTime)
    void fade(int fadeFrom, int fadeTo, unsigned long fadeTime, unsigned long delayTime)

    Parameters

    • fadeFrom: the value that LED start to fade (0 to 255).
    • fadeTo: the value that LED fade to (0 to 255).
    • fadeTime: The duration (in ms) that LED fades.
    • delayTime: (Optional) The time (in ms) delay before fading LED . If not set, LED is faded immediately. This function is non-blocking even if the delayTime is set.

    If fadeFrom < fadeTo, LED is faded in

    If fadeFrom > fadeTo, LED is faded out

    Return

    None

    Example

    See LED Fade In Fade Out example

    ※ NOTE THAT:

    • The fading can be canceled immediately by calling led.cancel() function
    • To check if the fading is finished or not, call led.getState() function. If the state is LED_IDLE, the fading is finished

    blink()

    Description

    This function blinks LED forever. The blinking can be canceled immediately by calling led.cancel() function

    Syntax

    void blink(unsigned long onTime, unsigned long offTime)
    void blink(unsigned long onTime, unsigned long offTime, unsigned long delayTime = 0)

    Parameters

    • onTime: the time (in ms) LED is on in a blink cycle.
    • offTime: the time (in ms) LED is on in a blink cycle.
    • delayTime: (Optional) The time (in ms) delay before blinking LED . If not set, LED is blinked immediately. This function is non-blocking even if the delayTime is set.

    Return

    None

    Example

    See LED Blink example

    blinkInPeriod()

    Description

    This function blinks LED in a period of time.

    Syntax

    void blinkInPeriod(unsigned long onTime, unsigned long offTime, unsigned long blinkTime)
    void blinkInPeriod(unsigned long onTime, unsigned long offTime, unsigned long blinkTime, unsigned long delayTime)

    Parameters

    • onTime: the time (in ms) LED is on in a blink cycle.
    • offTime: the time (in ms) LED is on in a blink cycle.
    • blinkTime: the time period (in ms) that LED is blinking. After this time is passed, the blinking is stopped and LED is set to OFF
    • delayTime: (Optional) The time (in ms) delay before blinking LED . If not set, LED is blinked immediately. This function is non-blocking even if the delayTime is set.

    Return

    None

    Example

    See LED Blink In Period example

    ※ NOTE THAT:

    • The blinking can be canceled immediately by calling led.cancel() function
    • To check if the blinking is finished or not, call led.getState() function. If the state is LED_IDLE, the blinking is finished

    blinkNumberOfTimes()

    Description

    This function blinks LED in a number of times.

    Syntax

    void blinkNumberOfTimes(unsigned long onTime, unsigned long offTime, unsigned int numberOfTimes)
    void blinkNumberOfTimes(unsigned long onTime, unsigned long offTime, unsigned int numberOfTimes, unsigned long delayTime)

    Parameters

    • onTime: the time (in ms) LED is on in a blink cycle.
    • offTime: the time (in ms) LED is on in a blink cycle.
    • blinkTime: the time period (in ms) that LED is blinking. After this time is passed, the blinking is stopped and LED is set to OFF
    • delayTime: (Optional) The time (in ms) delay before blinking LED . If not set, LED is blinked immediately. This function is non-blocking even if the delayTime is set.

    Return

    None

    Example

    See LED Blink Number Of Times example

    ※ NOTE THAT:

    • The blinking can be canceled immediately by calling led.cancel() function
    • To check if the blinking is finished or not, call led.getState() function. If the state is LED_IDLE, the blinking is finished

    low()

    Description

    This function cancels any ongoing operation and set LED to OFF.

    Syntax

    void cancel()

    Parameters

    None

    Return

    None

    Example

    #include <ezLED.h> // ezLED library ezLED led(9); // create ezLED object that attach to pin 9 void setup() { led.blink(500, 500); } void loop() { led.loop(); led.cancel(); }

    getOnOff()

    Description

    This function returns the current ON/OFF state of LED.

    Syntax

    int getOnOff()

    Parameters

    None

    Return

    • LED_OFF: If the LED is off
    • LED_ON: If the LED is on

    Example

    See LED On Off example.

    getState()

    Description

    This function returns the current operation state of LED.

    Syntax

    int getState()

    Parameters

    None

    Return

    • LED_IDLE: If the LED is not in any operation. The LED can be ON or OFF in this operation state
    • LED_DELAY: If the LED is in delay time
    • LED_FADING: If the LED is fading
    • LED_BLINKING: If the LED is blinking

    Example

    See LED Blink Number Of Times example.

    loop()

    loop()

    Description

    This function does tasks such as checking delay, fading, and blinking on the background. Call this function as many as possible.

    Syntax

    led.loop();

    Parameters

    None

    Return

    None

    Example

    See:

The Best Arduino Starter Kit

See Also

※ OUR MESSAGES