Monday, May 27, 2013

Blink an LED on the BeagleBone Black using Python

Using Command Terminal


Probably the most basic example you could do but its a great start for using digital output on your device. Before I attempted this in Python, I turned and LED on simply through the command terminal. This begins with defining the pin you are going to use. This creates a directory by executing the following command

cd /sys/class/gpio
echo ## export

The ## being which pin you will be using as the digital pin on the BBB

This will create the directory for the pin you have specified. Next is to define it as an INPUT or an OUTPUT pin.

This pin will be an "out" as we are turning off and on and LED controlled by the device.

echo out > direction


Using Python

The same concepts are utilized when executing the commands in python. Rather than using "echo" to write values to the pin files, you use python to write the commands to the files on the BeagleBone.

The link to the code is located at Blink.py

I will explain it in segments,

1. The first setup is to define which pin will be used, just as we did before.
2. The second setup is to define whether it is an INPUT or an OUTPUT, as we did before
3. We then open the value file so we can write to it. Because this is a digital output we will write a "1" for on and a "0" for off.

We then just create a loop that turns the LED off and on in 1 second intervals.

The except is for when we want to end the program. You have to unexport the pin that was being used so you execute the command

/sys/class/gpio/unexport

This will occur when the user uses Ctrl+C in the terminal window to end the python program.


This is a great start to understanding how to use digital input and output on the BeagleBone Black. From here you can use this to turn on much more than just and LED.



2 comments:

  1. I tried the terminal commands without success. I wasn't sure if the processor pin or header pin number should be used, and if the syntax should be like "13" or "GPIO_13".

    ReplyDelete
    Replies
    1. It is the actual GPIO assigned pin not the physical pin number.

      For example if I wanted to use GPIO 44 I'd do the commands

      cd /sys/class/gpio
      echo 44 > export
      cd gpio44
      echo out > direction

      and so on...

      In the other post

      http://yetticave.blogspot.com/2013/06/beaglebone-18v-analog-logic.html

      I have a small cheat sheet on the pin assignments of GPIO and physical you can reference.

      Delete