BeagleBone Black – Controlling user LEDs using C/C++

The $45 BeagleBone Black is here! With all its GPIO ports, it can be a great tool for robotics projects. It ships with javascript example code (in the Cloud9 IDE). There are many examples on the internet showing pin manipulation with javascript and python. There are lots of examples on pin manipulation from the terminal. Unfortunately for a new BBB owner, I could only find suggestions on how to do this on C/C++. I could not find a complete, short, and plain C/C++ example which I could just copy and use as a starting point…something like the traditional “blink” example. Some people have posted their own C++ libraries for pin manipulation, but I find that confusing without understanding the basic concepts.

After a lot of digging, ssh-ing, and g++-ing, I finally got a “blink” example up and running on C++. Here it is. Hopefully, for all the new BBB users out there, this can serve as a starting point in understanding how to use the GPIO ports through C/C++.


/*******************************************************
 * Example code for user LEDs on the new beaglebone
 * black running the Linux Angstrom distribution
 ********************************************************
 * Instructions:
 * -Compile and run from the root directory
 * -For the older (white) beaglebone, change
 *  "beaglebone:green:usr0" to "beaglebone::usr0"
 *
 * Code adapted from:
 * - Derek Molloy, "Beaglebone: C/C++ Programming
 * Introduction for ARM Embedded Linux Development
 * using Eclipse CDT" video tutorial,
 * (link: www.youtube.com/watch?v=vFv_-ykLppo)
 * -  Mark A. Yoder, EBC Exercise 10 Flashing an LED
 * (link: "elinux.org/EBC_Exercise_10_Flashing_an_LED)
 *******************************************************/

#include <stdio.h>
#include <unistd.h>

using namespace std;

int main(int argc, char** argv) {

  FILE *LEDHandle = NULL;
  char *LEDBrightness = "/sys/class/leds/beaglebone:green:usr0/brightness";
  printf("\nStarting simple LED blink program\n");

  while(1){

    if((LEDHandle = fopen(LEDBrightness, "r+")) != NULL){
      fwrite("1", sizeof(char), 1, LEDHandle);
      fclose(LEDHandle);
    }

    sleep(1);

    if((LEDHandle = fopen(LEDBrightness, "r+")) != NULL){
      fwrite("0", sizeof(char), 1, LEDHandle);
      fclose(LEDHandle);
    }

    sleep(1);
  }
  return 0;

}
Advertisements

13 thoughts on “BeagleBone Black – Controlling user LEDs using C/C++

  1. The LED just continuously blinks. I’m new to C Programming and am not certain how to get it to stop besides Ctrl + C, which then leaves the LED on. From there I can turn it off with the command, I would just rather the program do it. Any suggestions?

    • Sounds like depending on when you quit the program, the LED remains at the last state. You want to make your program wait for an event and then switch off the LED. The easiest event to use is a count. Have the program blink while it counts upto a number. Once the count is reached, exit the loop, switch off the led, then exit the program. You could use something like this:

      int counter = 0;
      while(counter < 5) {

      // blinking code goes here

      }

      // code to switch off the LED goes here

  2. I have a new Beaglebone Black Rev C with debian (Linux beaglebone 3.8.13-bone47 #1 SMP Fri Apr 11 01:36:09 UTC 2014 armv7l GNU/Linux). It has the heartbeat on LED USR0, so I am trying to use LED USR3. No matter which LED I try to use, LEDHandle is always NULL. It’s like fopen() can’t access “/sys/class/leds/beaglebone:green:usr0/brightness”. If I issue “echo 1 > /sys/class/leds/beaglebone:green:usr3/brightness” from the command line, I get “Permission denied”. I am able to toggle the LED by using “sudo sh -c “echo 1 > /sys/class/leds/beaglebone:green:usr3/brightness”” and “sudo sh -c “echo 0 > /sys/class/leds/beaglebone:green:usr3/brightness””. So, if sudo access is required at the command line, is there a way to get fopen() to return a handle?

    • Sorry for the late reply. I’ haven’t used a BBB in 3 years. You have hopefully resolved your issue by now. In any case, the code on this page is obsolete. They use device trees now. I’ve posted some useful links in response to Raul’s comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s