Can I add an LED to /sys/class/leds and control it using nerves_leds?

I am learning IoT using Nerves and Raspberry Pi. I saw various ways to let LEDs blink, such as:

Is it viable to register to /sys/class/leds an LED that is connected to GPIO, and blink it using nerves-project/nerves_leds?

I have limited knowledge of C and Linux, but I did my best to find some related info:

2 Likes

Hi @mnishiguchi,

Both nerves_leds and circuits_gpio can control LEDs. It looks like you found the Linux documentation on how the two libraries are implemented. To summarize, the difference is that circuits_gpio is lower level and controls (or monitors) whether a microprocessor’s I/O pin is on or off. nerves_leds provides a higher level interface to an I/O pin with the assumption that the pin controls an LED or something similar. nerves_leds lets you tell the Linux kernel to do things like blink the LED, tie the LED to CPU activity, etc.

To use nerves_leds the Linux kernel needs to be told about the pin that controls the LED. That’s done through a device tree configuration file. Sometimes it is possible to adapt someone else’s device tree file to your needs, but the files can be intimidating. I’d recommend using circuits_gpio to control your own LEDs and any LEDs that aren’t already under /sys/class/leds.

The Elixir Circuits quickstart has a guide on controlling an LED. The other place to look is at the hello_leds and hello_gpio projects in the Nerves examples.

2 Likes