Beaglebone Black: Controlling On-board LEDs

Beaglebone Black On-board LEDs

In this post, I flashed a LED connected to one of the Beaglebone Black's GPIO pins. Here I will flash the user or on-board LEDs on the device using the same method we did on the external LED.

User LEDs

The Beaglebone Black has four blue LEDs, USR0 to USR3, in the corner near the reset button. You would see them immediately because they flash when you turn on the device. The purpose of the LEDs is to describe the current state of the Beaglebone Black:

USR0 is the one that blinks in a heartbeat pattern; 

USR1 lights when accessing the microSD card; 

USR2 lights during CPU activity and

US3 lights when the eMMC is accessed.

Accessing the LEDs

We can access the user LEDs via /sys/class/leds

root@arm:/sys/class/leds# ls -al
total 0
drwxr-xr-x 2 root root 0 Jan 1 2000 .
drwxr-xr-x 57 root root 0 Jan 1 2000 ..
lrwxrwxrwx 1 root root 0 Jan 1 2000 beaglebone:green:usr0 -> ../../devices/platform/leds/leds/beaglebone:green:usr0
lrwxrwxrwx 1 root root 0 Jan 1 2000 beaglebone:green:usr1 -> ../../devices/platform/leds/leds/beaglebone:green:usr1
lrwxrwxrwx 1 root root 0 Jan 1 2000 beaglebone:green:usr2 -> ../../devices/platform/leds/leds/beaglebone:green:usr2
lrwxrwxrwx 1 root root 0 Jan 1 2000 beaglebone:green:usr3 -> ../../devices/platform/leds/leds/beaglebone:green:usr3

ere we can see all the user LEDs (why is it named green?). Try opening an individual LED:

root@arm:/sys/class/leds/beaglebone:green:usr2# ls -al
total 0
drwxr-xr-x 3 root root 0 Jan 1 2000 .
drwxr-xr-x 6 root root 0 Jan 1 2000 ..
-rw-r--r-- 1 root root 4096 Dec 30 14:36 brightness
lrwxrwxrwx 1 root root 0 Dec 30 14:36 device -> ../../../leds
-r--r--r-- 1 root root 4096 Dec 30 14:36 max_brightness
drwxr-xr-x 2 root root 0 Dec 30 14:36 power
lrwxrwxrwx 1 root root 0 Jan 1 2000 subsystem -> ../../../../../class/leds
-rw-r--r-- 1 root root 4096 Dec 30 14:36 trigger
-rw-r--r-- 1 root root 4096 Jan 1 2000 uevent

LED Brightness

I wonder what the current brightness of USR2 is... You can get that information by doing this:

root@arm:/sys/class/leds/beaglebone:green:usr2# more brightness

It's 0 because it's turned off! To turn it on, echo 1 to brightness like this:

root@arm:/sys/class/leds/beaglebone:green:usr2# echo 1 > brightness

The USR2 LED (second closest to the LAN port) should light up.


You can also set when the user led acts via the trigger parameter. To know all the triggers for the USR2 LED, do this:

root@arm:/sys/class/leds/beaglebone:green:usr2# more trigger
[none] rc-feedback kbd-scrollock kbd-numlock kbd-capslock kbd-kanalock kbd-shift
lock kbd-altgrlock kbd-ctrllock kbd-altlock kbd-shiftllock kbd-shiftrlock kbd-ct
rlllock kbd-ctrlrlock nand-disk usb-gadget usb-host mmc0 mmc1 timer oneshot hear
tbeat backlight gpio default-on

That's a lot of trigger events. There's trigger everytime a keyboard key is hit (kbd-capslock, kbd-ctrllock, etc), and when devices are accessed (usb-gadget, usb-host, mmc0, etc.). For now, let's try the timer trigger.  Let's say we want USR2 to turn on for a second then turn off. Here's how to do that:

root@arm:/sys/class/leds/beaglebone:green:usr2# echo timer > trigger
root@arm:/sys/class/leds/beaglebone:green:usr2# echo 1000 > delay_off

If you do this again:

root@arm:/sys/class/leds/beaglebone:green:usr2# more trigger
none rc-feedback kbd-scrollock kbd-numlock kbd-capslock kbd-kanalock kbd-shiftlock kbd-altgrlo
ck kbd-ctrllock kbd-altlock kbd-shiftllock kbd-shiftrlock kbd-ctrlllock kbd-ctrlrlock nand-dis
k usb-gadget usb-host mmc0 mmc1 [timer] oneshot heartbeat backlight gpio default-on

You will notice that timer is now enclosed in square brackets. This indicates the current trigger setting.

Try the heartbeat trigger to make it flash like USR0 or the default-on trigger to make it stay on!

C++ Code

Here's the C++ code for the Beaglebone Black to make USR2 turn on and off for a second for about four times:

#include <unistd.h>
#include <stdio.h>
using namespace std;

int main()
   FILE *export_file = NULL; //declare pointers
   FILE *IO_direction = NULL;
   char noneTrig[] = "none";
   char delayVal[] = "1000";
   char timerTrig[] = "timer";
   //set trigger to none first, just in case
   export_file = fopen ("/sys/class/leds/beaglebone:green:usr2/trigger", "w");
   fwrite (noneTrig, 1, sizeof(noneTrig), export_file);
   fclose (export_file);
   //set trigger to timer
   IO_direction = fopen ("/sys/class/leds/beaglebone:green:usr2/trigger", "w");
   fwrite (timerTrig, 1, sizeof(timerTrig), IO_direction);
   fclose (IO_direction);
   //set timer delay to 1 second and then let the device sleep for 5 seconds (flashes the LED four times)
   export_file = fopen ("/sys/class/leds/beaglebone:green:usr2/delay_off", "w");
   fwrite (delayVal, 1, sizeof(delayVal), export_file);
   fclose (export_file);
   usleep (5000000);
   //set trigger back to none
   IO_direction = fopen ("/sys/class/leds/beaglebone:green:usr2/trigger", "w");
   fwrite (noneTrig, 1, sizeof(noneTrig), IO_direction); //set the pin to LOW
   fclose (IO_direction);

Have any questions? Write a comment below! Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *