🟢 LED

Build StatusHex.pmAPI docsHex DownloadsLast UpdatedGitHub stars

Control LEDs or relays. A simple library with some artful gimmicks.

{:ok, _pid} = LED.start_link()
LED.blink()

This library is a simple, convenient wrapper around Circuits.GPIO for controlling LEDs or relays. Beyond basic on/off control, it enables artful, experimental, and even random noise light effects through overlapping repeating patterns — offering creative flexibility. Use it with Nerves.

✨ Features

Installation

Add to your mix.exs:

def deps do
  [
    {:led, "~> 0.1.0"}
  ]
end

⚙️ Connect LED to Raspberry Pi (GPIO 22) example

┌─────┬────────────┬─────────────────────┐
│ Pin │ Use        │ Connection          │
├─────┼────────────┼─────────────────────┤
│  6    GND          ────────────────┐   │
│ 15    GPIO22       ───▶|───[330Ω]──┘   │
└─────┴────────────┴─────────────────────┘

🛠 Usage

Start an LED

{:ok, _pid} = LED.start_link()
{:ok, _pid} = LED.start_link(name: :green_led, gpio_pin: 23, initial_value: 0)

Turn it on or off

LED.on()
LED.off()
LED.on(:green_led)
LED.off(:green_led)

Toggle LED

LED.toggle()
LED.toggle(:green_led)

Set state directly

LED.set(0, :green_led)  # Off
LED.set(1, :green_led)  # On

⏰ Blink LED

LED.blink()
LED.blink(name: :green_led)
LED.blink(interval: 500, times: 10)
LED.blink(name: :green_led, interval: 500, times: 10)

Repeat blinking (overlapping allowed)

LED.repeat(interval: 300)
LED.repeat(name: :green_led, interval: 300)
LED.repeat(interval: 400, times: 5)
LED.repeat(interval: 700, times: 3)
LED.repeat(name: :green_led, interval: 400, times: 5)
LED.repeat(name: :green_led, interval: 700, times: 3)

Cancel blinking timers

Stop all blinking/repeating timers on default/:green_led:

LED.cancel_timers()
LED.cancel_timers(:green_led)

Using with Nerves

You can integrate the LED module into your Nerves application's supervision tree.
For example, to control a LED connected to GPIO pin 23 (default is 22), add the LED process like this:

children = [
  {LED, [gpio_pin: 23]}
]

This ensures the LED is supervised and ready to use with functions like LED.on/0 or LED.blink/1.

💡 Works great on Nerves-supported devices like Raspberry Pi or BeagleBone.

Disclaimer

Note on use with relay

When using a relay, check its datasheet for minimum switch times to avoid damage or malfunction.