Home > BeagleBone > BeagleBone GPIO Programming

BeagleBone GPIO Programming

According to the BeagleBone System Resource Manual (SRM) the board has:

A maximum of 66 GPIO pins are accessible from the expansion header. All of these pins are 3.3V and can be configured as inputs or outputs. Any GPIO can be used as an interrupt and is limited to two interrupts per GPIO Bank for a maximum of eight pins as interrupts.

These pins are distributed across both expansion headers (P8 & P9) and their locations are well documented in the SRM. As described in a previous article, Linux provides a virtual file system called sysfs as a programming interface to system resources such as GPIO. The blink.js script that’s supplied with BeagleBoard’s Ångström’s Linux distribution will blink the User 3 LED and toggle GPIO1_6, which is located on the third pin of expansion header P8, high and low.

This article describes my experience getting an LED to blink using GPIO1_6.

Before I get into my LED project I want to note that I’m a typical software guy. I understand basic circuitry but my knowledge of electronics rapidly diminishes after that. So there are aspects of this journey that I’m discovering for the first time.  Most BeagleBone articles don’t detail the circuitry nor explain the electronics behind it. They assume the audience knows that stuff and focus on the BeagleBone part of things. I’m going to get into those details because I had to learn them before I could get this simple example to work.

LED Circuit

LED Circuit

An LED circuit is about a simple as it gets but still a bit of a learning experience for me.

  1. There are different kinds of LED’s. I picked up a standard red diffused 1 3/4 sized LED.
  2. The resistor is really important. Forget the resistor and you’ve created a short circuit. Not only will be burn out the resistor and your fingers it can damage the BeagleBone. The type of resistor you’ll need will depend on things like voltage and LED type. The resistor can be placed before or after the LED.
  3. An LED will only light with correct electrical polarity. You have to plug it in the right way.
GPIO Output to LED

GPIO Output to LED

I’m going to use GPIO1_6 as the power source for the LED circuit. When I set it high 3.3V will be supplied to the circuit and the LED will blink on. Setting GPIO1_6 low turns off the power the the LED will blink off.

Including a resistor in the circuit is a must. Knowing exactly what kind of resistor takes some testing. If you are in a hurry you can use 1k ohm (1/4 W – 5%) resistor. You’ll get a dim light but you’ll know it is safe.

Calculating the optimal type of resistor requires three values:

  • The source voltage. Since I’m using a GPIO pin it should be 3.3V.
  • The LED voltage. I used a multi-meter to get a value of 1.6V.
  • The LED current. Which after a lot of trial and error using a multi-meter comes in at 20mA.

Now to the formula.  The resistor value, R is given by: R = (VS – VL) / I

VS = supply voltage
VL = LED voltage
I = LED current

In my case this worked out to be 100ohm. There are all sorts of nifty calculators on the Internet. I used this one at LED Center to do the calculation for me.

This shows how I wired the circuit.

BeagleBone LED Circuit

BeagleBone LED Circuit

The red wire is plugged into GPIO1_6. That GPIO pin provides the power when it is set high. I’ve put a 100ohm resistor in front of the red LED. I could have put it after. You need wire the LED’s polarity correctly. If you wire it backwards the LED will not illuminate. The yellow wire runs to an expansion header GND pin and completes the circuit.

On the programming side we toggle GPIO1_6 on and off by using its sysfs interface. If I run blink.js both the User 3 LED and my red LED will blink. I wrote a shell script blink.sh to do the same thing.


echo 38 > /sys/class/gpio/export

while :
  echo 1 > /sys/class/leds/beaglebone::usr3/brightness
  echo high > /sys/class/gpio/gpio38/direction
  sleep 1
  echo 0 > /sys/class/leds/beaglebone::usr3/brightness
  echo low > /sys/class/gpio/gpio38/direction
  sleep 1

/sys/class/gpio/gpio38 does not exist by default. You create it by writing the GPIO port number to /sys/class/gpio/export. You can remove it by writing the same number to /sys/class/gpio/unexport.

Here’s a video of it in action.

Categories: BeagleBone Tags:
  1. vinay
    May 27, 2013 at 6:22 pm

    can anyone give c or c++ code for this???

  2. Exirion
    November 28, 2013 at 4:26 pm

    Why do you toggle the direction? You should keep the direction configured as output and toggle between 0 and 1 on value.

    • November 29, 2013 at 9:45 pm

      The example is blinking two different LED’s:

      /sys/class/leds/beaglebone::usr3/brightness – built-in LED on motherboard
      /sys/class/gpio/gpio38/direction – LED on the bread board

      One uses the commands “high & low” and the other “1 & 0”.

  1. No trackbacks yet.

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

%d bloggers like this: