Beagleboard:C/C++ Programming

Jump to: navigation, search

Blinking USR0 on the BeagleBone

1) Connect your BeagleBone to your computer using a Mini-USB Data Cable.
2) Using a terminal shell, such as PuTTy, serial connect into your BeagleBone and log in as 'root'
3) Now, we will create a simple C program that turns an on-board LED on and off ten times. Type this into your terminal shell.

nano example.cpp

4) Copy and paste this code into the "nano" text editor.

 #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;

5) Next, press Ctrl+X, type "Y" to save changes, and press enter.
6) Now, we need to compile the program and make an executable file so you can run it. Type the following into your terminal shell.

g++ example.cpp -o example

7) Finally, run the program with this command.

8) When you run the program, it will output "LED Flash Start" and will turn the USR0 light on the BeagleBone on and off 10 times. When it has finished, it will output "LED Flash END" on your terminal shell.
C Programming.png

9) If you would like more information on this, check out Derek Molloy's video. His video also includes instructions on how to install a C/C++ IDE on your BeagleBone.