Run this notebook

Use Livebook to open this notebook and explore new ideas.

It is easy to get started, on your machine or the cloud.

Click below to open and run it in your Livebook at .

(or change your Livebook location)

# Sys Class LEDs ## Introduction Depending on which board you're using, one or more LEDs are controllable using special files. In this exercise, you'll learn how to turn them on and off and then how to tell Linux to make them blink on their own. ## Try it out All of the LEDs are found in the `/sys/class/leds` directory, so lets set a variable so that we don't have to type that all the time. First, find out what LEDs you have on your board: ```elixir led_base_path = "/sys/class/leds" File.ls!(led_base_path) ``` Now, pick one by setting the variable below. We'll pick `"led0"`, but you'll need to replace that if you don't have it. ```elixir led = Path.join(led_base_path, "led0") ``` Reset the LED to a known state for this tutorial. Don't worry about this yet. If the LED was on, then it will be off after this runs. ```elixir File.write(Path.join(led, "trigger"), "none") File.write(Path.join(led, "brightness"), "0") ``` Let's see what files are in the LED directory: ```elixir File.ls!(led) ``` The `brightness` file lets you turn it on and off by writing a `"1"` or `"0"` to it. Try it out: ```elixir File.write(Path.join(led, "brightness"), "1") ``` ```elixir File.write(Path.join(led, "brightness"), "0") ``` Linux is happy controlling the LED on its own through the use of **triggers**. Triggers are quite flexible and can blink the LED on timers, CPU activity, disk activity and more. Here's how to make it blink at 2 Hz (250 ms on and then 250 ms off) using the timer trigger: ```elixir File.write(Path.join(led, "trigger"), "timer") File.write(Path.join(led, "delay_on"), "250") File.write(Path.join(led, "delay_off"), "250") ``` Set the trigger back to `"none"` to control the LED again using the `brightness` file. To see what other triggers are available, read the `trigger` file and try them out. ```elixir File.write(Path.join(led, "trigger"), "none") File.read!(Path.join(led, "trigger")) ```
See source

Have you already installed Livebook?

If you already installed Livebook, you can configure the default Livebook location where you want to open notebooks.
Livebook up Checking status We can't reach this Livebook (but we saved your preference anyway)
Run notebook

Not yet? Install Livebook in just a minute

Livebook is open source, free, and ready to run anywhere.

Run in the cloud

on select platforms

To run on Linux, Docker, embedded devices, or Elixir’s Mix, check our README.

PLATINUM SPONSORS
SPONSORS
Code navigation with go to definition of modules and functions Read More