From my Blog site: http://derekmolloy.ie/beaglebone-controlling-the-on-board-leds-using-c/
One of the first things you would like to do when you connect to the Beaglebone Black is see that you are having an impact on the hardware. In this short post I am going to look at how you can change the behaviour of the Beaglebone on-board LEDs – the four (blue on the BBB) LEDs in the corner near the reset button.
Now, the LEDs are there for a reason, and that reason is to give information about the Beaglebone state (from beaglebone.org):
We can change the behaviour of these LEDs, but obviously we will temporarily lose this valuable information. The heartbeat tells you that the BBB is alive, which is always useful to know. The others are fairly self explanatory (the eMMC is the solid state memory that you are booting from).
Sysfs is a virtual file system that is available under Linux that gives information and access to devices and drivers that would otherwise only be accessible in Kernel Space. It gives us a virtual file system in Linux User Space, which is convenient to use; however, it is not highly efficient.
At the command line, we can go to to directory /sys/class/leds:
You can see the four (green!) LEDs, usr0, usr1, usr2 and usr3 and their mappings.
We can go into the directory of the first LED:
Here you see various different sub-directories that give us further information and access to settings. We can find out the current status of the LED by:
You can see that the trigger is set up as a “heartbeat”
We can turn this off by:
And you will see (hopefully) that the LED stops flashing. So, let’s try a few different options:
Now that the trigger is off, we can turn on the LED using the brightness setting:
The first LED should go on.
We can turn off the LED using the brightness setting:
The first LED should go off.
We can use the timer trigger to make the LED flash, so do the following:
You have to set up the timer before writing to delay_on and delay_off. So the first line sets up a timer and on the second and third lines we set up the time for the first LED to be on and off (in milliseconds). So this will cause the first LED to flash 10 times per second (i.e. time on + time off = 100ms, so 10 flashes per second).
We can set it back to the way it was before we started:
It should go back to the heartbeat trigger.
Now that we have this working at the command line it would be useful if we could write programs that interface to the LEDs in a similar fashion. My C++ implementation code is below. It uses the C++ fstream and iostream libraries to read and write data to the input/outputs using sysfs.
Effectively, this code carries out the exact same steps as what I did in the first part of this post; however, now it is wrapped up as a command that we can add to our $PATH in order that it can be called from any location. The way you build the code on the Beaglebone Black is as follows:
I have made all of the code available via github, so you can download this by the following steps:
Now, build the code and execute it:
The way we can interact with GPIOs is not much different than this; however, we have complexities to deal with to do with the Device Tree, and we have to use Device Tree Overlays. See my post: GPIOs on the Beaglebone Black using the Device Tree Overlays on this topic.