LED (led v0.1.1)
View SourceControl 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
- ⚡ Control single or multiple LEDs or relays on GPIO pins
- ✔ Easy on/off control
- ⏰ Blink and repeat patterns with configurable intervals and counts
- 💡 Toggle LED state
- 🎛️ Optional named LED processes for multiple devices
- Supports overlapping repeat patterns for creative effects
- Defaults to GPIO pin
22
with LED initially on
Installation
Add to your mix.exs
:
def deps do
[
{:led, "~> 0.1.0"}
]
end
⚙️ Connect LED to Raspberry Pi (GPIO 22) example
- Anode (long leg) → GPIO pin 22
- Cathode (short leg) → 330Ω resistor → GND
┌─────┬──────────┬─────────────────────┐
│ Pin │ Use │ Connection │
├─────┼──────────┼─────────────────────┤
│ 6 GND ────────────────┐ │
│ 15 GPIO22 ───▶|───[330Ω]──┘ │
└─────┴──────────┴─────────────────────┘
🛠 Usage
Start an LED
- Default (gpio_pin: 22, initially on):
{:ok, _pid} = LED.start_link()
- Named (gpio_pin: 23, initially off):
{:ok, _pid} = LED.start_link(name: :green_led, gpio_pin: 23, initial_value: 0)
Turn it on or off
- Default:
LED.on()
LED.off()
- Named:
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
- Blink indefinitely at 2 Hz (250ms interval):
LED.blink()
LED.blink(name: :green_led)
- Blink LED on default/
:green_led
10 times, toggling every 500 ms:
LED.blink(interval: 500, times: 10)
LED.blink(name: :green_led, interval: 500, times: 10)
Repeat blinking (overlapping allowed)
- Start a repeating blink pattern on default/
:green_led
every 300 ms indefinitely:
LED.repeat(interval: 300)
LED.repeat(name: :green_led, interval: 300)
- Overlay multiple blinking patterns (polyrhythmic effect):
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.
Summary
Functions
Starts or updates regular blinking for the LED.
Cancels all active timers associated with the LED process.
Returns a specification to start this module under a supervisor.
Returns true
if the LED is currently on (state == 1
), otherwise returns false
.
Returns true
if the LED is currently on (state == 1
), otherwise returns false
.
Turns the LED off by setting its GPIO state to 0
.
Turns the LED on by setting its GPIO state to 1
.
Starts a repeating blink pattern on the LED.
Sets the LED to a specific state: 1
for on, 0
for off.
Starts the LED GenServer.
Starts a blinking timer that toggles the LED at a given interval and for a given number of times.
Toggles the LED: turns it off if on, and on if off.
Functions
@spec blink(keyword()) :: :ok
Starts or updates regular blinking for the LED.
Accepts keyword options:
:name
– GenServer name of the LED. Defaults to the module name.:interval
– (integer) Blink interval in milliseconds. Default is250
(≈2 Hz).:times
– (integer) Number of blinks. Default is-1
for continuous blinking.
This will cancel any existing blinking or repeat timers before starting.
Use repeat/1
instead for experimental or overlapping blink patterns.
Examples
iex> LED.blink()
:ok
iex> LED.blink(name: :green_led, interval: 500, times: 10)
:ok
Cancels all active timers associated with the LED process.
This stops any ongoing blink/1
or repeat/1
patterns by clearing all stored
timer_refs
. Use this to reset the LED's timing behavior before issuing new
commands.
name
– (optional) the GenServer name of the LED; defaults to the module.
Examples
iex> LED.cancel_timers()
iex> LED.cancel_timers(:led_green)
Returns a specification to start this module under a supervisor.
See Supervisor
.
Returns true
if the LED is currently on (state == 1
), otherwise returns false
.
Accepts an optional name
argument for the GenServer process name set via start_link/1
.
Defaults to the module name.
Examples
iex> {:ok, _pid} = LED.start_link()
iex> LED.on()
iex> LED.is_lit?()
true
iex> LED.off()
iex> LED.is_lit?()
false
iex> {:ok, _pid} = LED.start_link(gpio_pin: 23, name: :led_pink)
iex> LED.on(:led_pink)
iex> LED.is_lit?(:led_pink)
true
iex> LED.off(:led_pink)
iex> LED.is_lit?(:led_pink)
false
Returns true
if the LED is currently on (state == 1
), otherwise returns false
.
Accepts an optional name
argument for the GenServer process name set via start_link/1
.
Defaults to the module name.
Examples
iex> {:ok, _pid} = LED.start_link()
iex> LED.on()
iex> LED.lit?()
true
iex> LED.off()
iex> LED.lit?()
false
iex> {:ok, _pid} = LED.start_link(gpio_pin: 23, name: :led_pink)
iex> LED.on(:led_pink)
iex> LED.lit?(:led_pink)
true
iex> LED.off(:led_pink)
iex> LED.lit?(:led_pink)
false
Turns the LED off by setting its GPIO state to 0
.
Accepts an optional name
argument to control a specific LED process started
with start_link/1
. If no name
is given, the default module name is used.
Calling this will cancel any active blinking or repeat timers before switching the LED off.
Examples
Turn off the default LED (GPIO pin 22):
iex> LED.off()
Start and turn off a named LED on GPIO pin 23:
iex> {:ok, _pid} = LED.start_link(gpio_pin: 23, name: :led_yellow)
iex> LED.off(:led_yellow)
Turns the LED on by setting its GPIO state to 1
.
Accepts an optional name
argument to control a specific LED process started
with start_link/1
. If no name
is given, the default module name is used.
Calling this will cancel any active blinking or repeat timers before switching the LED on.
Examples
Turn on the default LED (GPIO pin 22):
iex> LED.on()
Start and turn on a named LED on GPIO pin 23:
iex> {:ok, _pid} = LED.start_link(gpio_pin: 23, name: :led_green)
iex> LED.on(:led_green)
@spec repeat(keyword()) :: :ok
Starts a repeating blink pattern on the LED.
Unlike blink/1
, this function allows multiple overlapping patterns,
enabling experimental or polyrhythmic effects. Each call creates a new
timer without cancelling existing ones.
Can be manually canceled using cancel_timers/1
.
Useful for creative setups or layered visual rhythms. For best visibility, use intervals greater than 10ms.
Options
:name
– GenServer name of the LED. Defaults to the module name.:interval
– (integer) Interval in milliseconds between toggles. Default:250
(≈2 Hz).:times
– (integer) Number of blinks.-1
means infinite. Default:-1
.
If you want predictable, single-pattern blinking, use blink/1
instead.
Examples
iex> LED.repeat()
:ok
iex> LED.repeat(interval: 969)
:ok
iex> LED.cancel_timers()
iex> LED.repeat(name: :green_led, interval: 900)
:ok
iex> LED.repeat(name: :green_led, interval: 690)
:ok
iex> LED.cancel_timers(:green_led)
Sets the LED to a specific state: 1
for on, 0
for off.
Any active blinking or repeat timers will be canceled before setting the state, ensuring full manual control.
Accepts an optional name
argument to target a specific LED GenServer.
If omitted, it uses the default module name.
Examples
Turn off and on the default LED:
iex> LED.set(0)
iex> LED.set(1)
Control a named LED:
iex> {:ok, _pid} = LED.start_link(gpio_pin: 23, name: :led_red)
iex> LED.set(0, :led_red)
iex> LED.set(1, :led_red)
Starts the LED GenServer.
Accepts keyword arguments in init_args
to configure the LED:
:name
– Optional name for the GenServer (used for referencing multiple LEDs):gpio_pin
– (integer) GPIO pin to control; defaults to22
:initial_value
– (0 or 1) Initial LED state;0
= off,1
= on (default:1
)
Examples
Start the default LED:
iex> {:ok, _pid} = LED.start_link()
Start a named LED on pin 23, initially off. You can control it with:
iex> {:ok, _pid} = LED.start_link(name: :green_led, gpio_pin: 23, initial_value: 0)
iex> LED.on(:green_led)
iex> LED.is_lit?(:green_led)
true
@spec timer_start( integer(), integer(), atom() | pid() | {atom(), any()} | {:via, atom(), any()} ) :: :ok
Starts a blinking timer that toggles the LED at a given interval and for a given number of times.
interval
– blinking interval in milliseconds.times
– how often to toggle the LED. Use-1
for infinite blinking.
Use cancel_timers/1
to stop the blinking manually.
Examples
Blink the default LED continuously every 200ms:
iex> LED.timer_start(200, -1)
Blink a named LED 5 times every 300ms:
iex> LED.timer_start(300, 5, :led_blue)
Toggles the LED: turns it off if on, and on if off.
Examples
iex> {:ok, _pid} = LED.start_link()
iex> LED.on()
iex> LED.toggle()
iex> LED.is_lit?()
false
iex> {:ok, _pid} = LED.start_link(gpio_pin: 23, name: :green_led)
iex> LED.off(:green_led)
iex> LED.toggle(:green_led)
iex> LED.is_lit?(:green_led)
true