I have posted on how to do this exact thing using C++, so the first half is cut-and-pasted from Beaglebone: Controlling the on-board LEDs using C++
Introduction
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):
USR0 is configured at boot to blink in a heartbeat pattern
USR1 is configured at boot to light during microSD card accesses
USR2 is configured at boot to light during CPU activity
USR3 is configured at boot to light during eMMC accesses
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).
First – At the command line
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:
1 2 3 4 5 6 7 8 9 |
root@beaglebone:/sys/class/leds# ls -al total 0 drwxr-xr-x 2 root root 0 Jan 1 00:00 . drwxr-xr-x 48 root root 0 Jan 1 00:00 .. lrwxrwxrwx 1 root root 0 Jan 1 00:00 beaglebone:green:usr0 -> ../../devices/ocp.2/gpio-leds.7/leds/beaglebone:green:usr0 lrwxrwxrwx 1 root root 0 Jan 1 00:00 beaglebone:green:usr1 -> ../../devices/ocp.2/gpio-leds.7/leds/beaglebone:green:usr1 lrwxrwxrwx 1 root root 0 Jan 1 00:00 beaglebone:green:usr2 -> ../../devices/ocp.2/gpio-leds.7/leds/beaglebone:green:usr2 lrwxrwxrwx 1 root root 0 Jan 1 00:00 beaglebone:green:usr3 -> ../../devices/ocp.2/gpio-leds.7/leds/beaglebone:green:usr3 root@beaglebone:/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:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
root@beaglebone:/sys/class/leds# cd beaglebone:green:usr0 root@beaglebone:/sys/class/leds/beaglebone:green:usr0# ls -al total 0 drwxr-xr-x 3 root root 0 Jan 1 00:00 . drwxr-xr-x 6 root root 0 Jan 1 00:00 .. -rw-r--r-- 1 root root 4096 Jan 1 02:08 brightness lrwxrwxrwx 1 root root 0 Jan 1 02:08 device -> ../../../gpio-leds.7 -r--r--r-- 1 root root 4096 Jan 1 02:08 max_brightness drwxr-xr-x 2 root root 0 Jan 1 02:08 power lrwxrwxrwx 1 root root 0 Jan 1 02:08 subsystem -> ../../../../../class/leds -rw-r--r-- 1 root root 4096 Jan 1 02:08 trigger -rw-r--r-- 1 root root 4096 Jan 1 00:00 uevent root@beaglebone:/sys/class/leds/beaglebone:green:usr0# |
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:
1 2 |
root@beaglebone:/sys/class/leds/beaglebone:green:usr0# more trigger none nand-disk mmc0 mmc1 timer oneshot [heartbeat] backlight gpio cpu0 default-on transient |
You can see that the trigger is set up as a “heartbeat”
We can turn this off by:
1 |
root@beaglebone:/sys/class/leds/beaglebone:green:usr0# echo none > trigger |
And you will see (hopefully) that the LED stops flashing. So, let’s try a few different options:
Turning on the LED
Now that the trigger is off, we can turn on the LED using the brightness setting:
1 |
root@beaglebone:/sys/class/leds/beaglebone:green:usr0# echo 1 > brightness |
The first LED should go on.
Turning off the LED
We can turn off the LED using the brightness setting:
1 |
root@beaglebone:/sys/class/leds/beaglebone:green:usr0# echo 0 > brightness |
The first LED should go off.
Making the LED Flash
We can use the timer trigger to make the LED flash, so do the following:
1 2 3 |
root@beaglebone:/sys/class/leds/beaglebone:green:usr0# echo timer > trigger root@beaglebone:/sys/class/leds/beaglebone:green:usr0# echo 50 > delay_on root@beaglebone:/sys/class/leds/beaglebone:green:usr0# echo 50 > delay_off |
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).
Setting it Back to a Heartbeat
We can set it back to the way it was before we started:
1 |
root@beaglebone:/sys/class/leds/beaglebone:green:usr0# echo heartbeat > trigger |
It should go back to the heartbeat trigger.
Java Implementation
You need to have installed Java on the Beaglebone Black before you try this. Please follow the steps at this Running Java Applications on the Beaglebone Black or watch the following YouTube video:
[youtube id=”wpg61xG9Egc” width=”600″ height=”350″]
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 Java implementation code is below. It uses the Java.io libraries to read and write data to the input/outputs using sysfs.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
package ee402; import java.io.*; public class BasicLEDExample { private static String LED0_PATH = "/sys/class/leds/beaglebone:green:usr0"; public static void main(String[] args) { if(args.length!=1) { System.out.println("Incorrect Usage - use:\n\t BasicLEDExample On \n\t BasicLEDExample Off"); System.exit(0); } try{ if (args[0].equalsIgnoreCase("On") || args[0].equalsIgnoreCase("Off")){ BufferedWriter bw = new BufferedWriter ( new FileWriter (LED0_PATH+"/trigger")); bw.write("none"); bw.close(); bw = new BufferedWriter ( new FileWriter (LED0_PATH+"/brightness")); bw.write(args[0].equalsIgnoreCase("On")? "1":"0"); bw.close(); } else { System.out.println("Invalid command"); } } catch(IOException e){ System.out.println("Failed to access the Beaglebone LEDs"); } } } |
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. Build this code on your desktop computer or use the class file that I have distributed in the bin directory. The way you execute this code on the Beaglebone Black is to go to the directory below ee402 and type:
1 2 |
java ee402.BasicLEDExample On java ee402.BasicLEDExample Off |
I have made all of the code available via github, so you can download this by the following steps:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
root@beaglebone:~/temp# git clone git://github.com/derekmolloy/ee402 Cloning into ee402... remote: Counting objects: 52, done. remote: Compressing objects: 100% (43/43), done. remote: Total 52 (delta 19), reused 35 (delta 8) Receiving objects: 100% (52/52), 33.71 KiB, done. Resolving deltas: 100% (19/19), done. root@beaglebone:~/temp# ls ee402 root@beaglebone:~/temp# cd ee402/ root@beaglebone:~/temp/ee402# ls LEDcpp LICENSE README.md scripts testcpp testjava root@beaglebone:~/temp/ee402# cd LEDjava |
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.
Derek, I absolutely love your posts on the BeagleBone. Each one is extremely clear and helpful. I would really love it if you would write a post on connecting to the board via wifi. People including myself seem to run into a lot of problems with that.
Derek, I have been following your posts on beaglebone. Many thanks for your efforts. I am planning to build quadcopter and subsequently many other projects based on beaglebone platform. Could you please suggest me on which language to use (C++ or Java) assuming am good with both? Would there be performance difference during time sensitive operations as in control system for a quad? Thank you
Dear DerekMolley,
I am working on BeagleBone Black and having problem with PWM.
I am trying to use P8_13 and p8_19, I have enable both and when i am tryng to change the period i get the following error :
root@BBB:/sys/devices/ocp.2/pwm_test_P8_19.11# echo 10000000 > period
[ 1406.652632] ehrpwm 48304200.ehrpwm: Period value conflicts with channel 1
[ 1406.660047] pwm_test pwm_test_P8_19.11: pwm_config() failed
-sh: echo: write error: Invalid argument
If i do only one PWM i.e either P8_13 or P8_19 alone then the changes happens but fails on enabling the second.
Can you help me to fix this issue.
[ 644.005570] ehrpwm 48300200.ehrpwm: Period value conflicts with channel 1
[ 644.012919] pwm_test pwm_test_P9_22.17: pwm_config() failed
[ 659.117860] pwm_test pwm_test_P9_22.17: pwm_config() failed
[ 706.313906] pwm_test pwm_test_P9_14.14: pwm_config() failed
[ 756.757552] pwm_test pwm_test_P8_19.13: pwm_config() failed
[ 813.191564] ehrpwm 48304200.ehrpwm: Period value conflicts with channel 1
[ 813.198932] pwm_test pwm_test_P8_19.13: pwm_config() failed
[ 825.398016] pwm_test pwm_test_P8_19.13: pwm_config() failed
the same problem. kernel 3.8.16 – debian 7.4
dts are malformed.
i try to make a new dts my own. the same. no escape.
Derek,
Thankyou so much for this. Getting the BBB up would have been a lot harder without you.
Kudos to you, sir.
For those of you seeking an object oriented Java API for the Beaglebone Black, visit http://libbulldog.org
Derek, it’d be cool if you wrote a blog post about it.
Is there a way to import Bonescript? Its probably a noob question, but it seems like if this LED function and others are already built in it would be great to be able to call them and not have to reinvent the wheel.
Hi Justin, do you mean into Java? No, not that I know of. There is an open-source implementation of JavaScript in Java, but I don’t think there is anything that will allow you to use node.js within Java (without system calls). If you find something, please let me know, Derek.
please! please i want somebody write java code of advertisement board with LED