Understanding the basics of General-Purpose Input/Outputs on the BeagleBone Black
By: George Calin
Tags: Embedded, Gpio, Embedded Training, Embedded Linux, Beaglebone Black

As there are 32 GPIOs for each chip, the internal GPIO number for GPIO1_28 is calculated as follows:

1*32+28=60

The total range available for the 4 chips ranges from :

GPIO0_0(GPIO number 0*32+0=0 ) to

) to GPIO3_31(GPIO number 3*32+31=127 ), but not all AM335x GPIO numbers are available for all headers of BeagleBoards.

GPIO Digital Output

Actually this is about the manner you can use a GPIO to turn an electric circuit On/Off.

Setting of a GPIO for Digital Output

Note: In the example I am using a custom Linux Image built with Yocto Project. Simpler said, it is a Linux Operating System built with the embedded functionality and destination hardware in mind.

Yocto Project uses such things as recipes, where one can configure details specific to the utility the Linux Image to be built and the machine where it is going to run. Screenshots are taken from a “core-image-minimal” Linux image, which is a Linux stripped off non-essentials.

Poky (Yocto Project Reference Distro) 3.1.1 beaglebone-yocto /dev/ttyS0

So , step by step:

login as the the desired user and go to the directory /sys/class/gpio

root@beaglebone-yocto:~# cd /sys/class/gpio/

This directory hosts things of interest regarding GPIO.

root@beaglebone-yocto:/sys/class/gpio# ls

export gpiochip0 gpiochip32 gpiochip64 gpiochip96 unexport

2. Open the desired GPIO by writing the the file ‘export’ file

root@beaglebone-yocto:/sys/class/gpio# echo 60 > export

Note: pick the number depending upon the number of the GPIO you intend to use , in this example 60.

Conversely, if you want to unload the GPIO, then write to ‘unexport’ file:

echo 60 > unexport

3. Content of the gpio directory after opening the GPIO

root@beaglebone-yocto:/sys/class/gpio# ls

export gpio60 gpiochip0 gpiochip32 gpiochip64 gpiochip96 unexport

4. Switch the directory /gpio

root@beaglebone-yocto:/sys/class/gpio# cd gpio60

5. List the properties of that GPIO

root@beaglebone-yocto:/sys/devices/platform/ocp/48000000.interconnect/48000000.interconnect:segment@0/4804c000.target-module/4804c000.gpio/gpiochip1/gpio/gpio60# ls

active_low device direction edge power subsystem uevent value

6. Write the direction of the flow for that GPIO (‘out’)

root@beaglebone-yocto:/sys/devices/platform/ocp/48000000.interconnect/48000000.interconnect:segment@0/4804c000.target-module/4804c000.gpio/gpiochip1/gpio/gpio60# echo 'out' > direction

7. Set the value of the GPIO to 1 (On) :

root@beaglebone-yocto:/sys/devices/platform/ocp/48000000.interconnect/48000000.interconnect:segment@0/4804c000.target-module/4804c000.gpio/gpiochip1/gpio/gpio60# echo 1 > value

This switches on the LED as an example.

8. Set the value of the GPIO to 0 (Off)

root@beaglebone-yocto:/sys/devices/platform/ocp/48000000.interconnect/48000000.interconnect:segment@0/4804c000.target-module/4804c000.gpio/gpiochip1/gpio/gpio60# echo 0 > value

This switches off the LED as an example.

Next, I shall suggest you two examples of scripts that emulate the blinking of 1 and 2 LEDs.

Sample .sh script to flash one LED

#!/bin/bash

# A script to flash a LED connected to a GPIO of the BeagleBone Black using Bash shell scripting

# By Arnold the Crocodile



# Verify if the needed gpio directory already exists, and if not, create it with the export file

if [-d "/sys/class/gpio/gpio60"]

then

echo "'gpio' directory already exists.there is no need to create it by writing to 'export'."

else

echo "'gpio' directory does not exist.there is need to create it by writing in the 'export' file"

echo 60 > /sys/class/gpio/export

fi





while true

do

sleep .5

echo "entering the while.set the GPIO value to 1"

echo 1 > /sys/class/gpio/gpio60/value

echo "done. GPIO set to 1"

more /sys/class/gpio/gpio60/value

sleep .5



echo "set the GPIO value to 0"

echo 0 > /sys/class/gpio/gpio60/value

more /sys/class/gpio/gpio60/value

echo "done set to 0"

done

Sample .sh script to flash alternately 2 LEDs

Note: Script uses GPIO60 and GPIO48 on the BeagleBone Black.