Monday, July 28, 2014

On 9:02 AM by Richard St-Pierre   7 comments


Would you like to get started programming the BeagleBone Black with "C/C++" ?
The Linux distributions come with a built-in c-compiler (gcc) and c++ compiler (g++).
So let's take the first few steps together, in creating a very simple program.

What You Will Need


First we will connect to the BeagleBone  using ssh/putty, and log in as 'root'.

We can then create a file 'myprog.cpp' using the nano editor (or vim, or emacs - if you prefer)

STEP 1- Source Code

Example program from

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

 int main(){
 cout << "LED Flash Start" << endl;
 FILE *LEDHandle = NULL;
 const char *LEDBrightness="/sys/class/leds/beaglebone:green:usr0/brightness";

 for(int i=0; i<10; i++){
  if((LEDHandle = fopen(LEDBrightness, "r+")) != NULL){
   fwrite("1", sizeof(char), 1, LEDHandle);

  if((LEDHandle = fopen(LEDBrightness, "r+")) != NULL){
   fwrite("0", sizeof(char), 1, LEDHandle);
  cout << "LED Flash End" << endl;

Program Overview

In this simple example program,  we flash the built-in LED USR0 ten times.  We start out by sending a message to 'cout' (console output) to let us know that the program has started. The << is a redirection syntax  that sends the message to 'cout'. In Linux 'everything' is treated as a file.  To control the LED pin, we will assign a file handle and associate it to the LED. The LED brightness (in user space) will be controlled to turn the LED ON|OFF.

A 'for' loop will count up from 0 to 9 (10 values).  First we open the 'LED file'  write the value '0' to the brightness setting (OFF). Next we wait/sleep 1 million microseconds (1 second). Now we write the value '1' to turn ON the LED to full brightness, and once again wait 1 second.  We repeat the loop 10 times.

Finally, a simple message is sent to indicate that we are done is sent to the console. Simple, but useful to get started.

STEP 2- Compiling

g++ myprog.cpp -o myprog 

This command calls the C++ compiler with the source file myprog.cpp and outputs (-o) the file myprog.

For more complicated programs we can create a 'makefile' to simplify the compiling process.
This will be covered in further tutorials.

STEP 3- Running The Program


The program will flash the built-in USR0 LED ten times,

 and display a message at the start and the end.

This simple example program should get help you started. "C" where you can go from here.


  1. You should check out libsoc ( for more helpful hints about programming the hardware in C using Linux. Also, you can use the built-in Cloud9 IDE, rather than installing Putty/SSH, unless you already have it installed.

  2. Jason.
    Thank you for your input, suggestions, and support of the Open BeagleBone Community.
    We will be adding additional tutorials, project and examples that will go into more depth .
    This will be a great start in that direction. Thanks for contributing to

  3. Excellent starting point.

  4. very fabulous...................

  5. Hi everyone, how to write a sleep function in C language for beaglebone black ?

  6. Hello gang....
    great article. I'm using the cloud9 IDE enter/edit the above cpp program. I'm opening a terminal window to run g++. for some reason the compiler is not finding the namespace std. any thoughts/ideas ???

  7. any one please help me with a c coding to read a qr code with beagle bone,could i send that read details to specific mail id