#BeagleBone Black C++ GPIO library for beginners

0. List of contents

1. Requirements

  • Beagle Bone Black
  • Basic C++ knowledge
  • Basic python knowledge

2. BBB GPIO Pins

If you have ever tried to connect some electronics (i.e sensors) to your BBB, you probably noticed that the names of pins differ. For instance GPIO 38 is under P8_03 head pin, some of pins are used by default and some are not.
It would be convenient to have a tool/library which takes pin head name and return the pin number (needed in order to use gpio sysfs).
With that library (BeagleBoneBlack-GPIO) your work will be a bit easier. Searching and handling on pins is automated.

Great! That’s not enough, you will also need to know to which port connect the cable 🙂
Below is presented schema of P8 & P9 in BBB (taken from here):
resizedimage600667-hwio-beaglebone-ports2

Library is mostly based on detailed specification made by derek molloy (good job!). Documents are available for download below (click on image P9, P8) or from here:

Beaglebone Black P9 HeaderTableBeaglebone Black P8 HeaderTable

3. Example: Switching on/off the led

Adafruit published an article how to control led via GPIO. As you will see they are using their own python library which does mostly the same as BeagleBoneBlack-GPIO.

Python example:

import Adafruit_BBIO.GPIO as GPIO
import time

GPIO.setup("P8_10", GPIO.OUT)

while True:
    GPIO.output("P8_10", GPIO.HIGH)
    time.sleep(0.5)
    GPIO.output("P8_10", GPIO.LOW)
    time.sleep(0.5)

C++ example:

#include
#include "GPIO/GPIOManager.h"
#include "GPIO/GPIOConst.h"

int main() {
  GPIO::GPIOManager* gp = GPIO::GPIOManager::getInstance();
  int pin = GPIO::GPIOConst::getInstance()->getGpioByKey("P8_10");

  gp->setDirection(pin, GPIO::OUTPUT);

  while(true){
	  gp->setValue(pin, GPIO::HIGH);
	  sleep(5);
	  gp->setValue(pin, GPIO::LOW);
	  sleep(5);
  }

  gp->~GPIOManager();

  return 0;
}

4. Important methods

1. Operations on names

    • GPIOConst::getGpioByKey – i.e “P8_10”
    • GPIOConst::getGpioByName – i.e “GPIO0_7”
    • GPIOConst::getGpioNameByPin – i.e “12”
    • GPIOConst::isPinAllocatedByDefault – refering to derek molloy docs

2. Operations on GPIO sysfs descriptors:

    • GPIOManager::exportPin – Export pin
      (equivalent to i.e echo “68” > /sys/class/gpio/export)
    • GPIOManager::unexportPin – Unexport pin
      (equivalent to i.e echo “68” > /sys/class/gpio/unexport)
    • GPIOManager::setDirection – Set direction
      (equivalent to i.e echo “in” > /sys/class/gpio68/direction)
    • GPIOManager::getDirection – Get direction
      (equivalent to i.e cat /sys/class/gpio68/direction)
    • GPIOManager::setValue – Set value
      (equivalent to i.e echo “1” > /sys/class/gpio68/value)
    • GPIOManager::getValue – Get value
      (equivalent to i.e cat /sys/class/gpio68/value)
    • GPIOManager::setEdge – Set edge
      (equivalent to i.e echo “rising” > /sys/class/gpio68/edge)
    • GPIOManager::getEdge – Get edge
      (equivalent to i.e cat /sys/class/gpio68/edge)
    • GPIOManager::waitForEdge – Wait for edge event
    • GPIOManager::countExportedPins – Count already exported pins
    • GPIOManager::clean – Unexport all of already exported pins

5. Download

The latest version is on github: https://github.com/mkaczanowski/BeagleBoneBlack-GPIO

7 Comments

  1. Nice work, and great that you’ve built on the EXCELLENT work by Derek Molloy. I was wondering though–have you ever tried exception handling on the BBB? The reason I ask is that in some of your functions it might be nice to at least range-check values…if not check them for the proper data type. For example, when you pass in an int variable “pins” to one of the functions. It would be nice if the library would throw something back to the caller in the event that a bad value gets sent somehow. Obviously it would be prudent for the caller to do it, however that isn’t always the case.

    • Hi,
      sorry for late response. Exceptions should be fine solution
      Nowadays I’m really busy, so I suffer lack of time. But feel free to leave pull request 🙂

  2. Thanks for the great library! Sadly I am having a little trouble making things work. I have written a quick test based on your example code, however see no hardware reaction when I run the program (the console messages show however). Here is the code, am I missing something?

    http://pastebin.com/1Lmmz9Nz

Leave a Comment.