PLEASE NOTE: This site is no longer being actively maintained and will be taken offline on January 1st, 2016. For BeagleBone resources please visit*

Monday, July 28, 2014

On 9:02 AM by Richard St-Pierre


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.