avatarGeorge Calin

Summary

The provided content is a technical guide on using General-Purpose Input/Output (GPIO) on the BeagleBone Black for digital output, including instructions for setting up and controlling GPIOs through shell scripting.

Abstract

The web content offers a comprehensive tutorial on understanding and utilizing the GPIO pins on the BeagleBone Black microcontroller. It explains the layout of the expansion headers, P8 and P9, and the numbering system for the GPIOs, which are organized into four banks of 32 pins each. The guide details the process of configuring a GPIO pin for digital output, including exporting the pin, setting its direction, and controlling its value to turn an LED on and off. It also provides sample shell scripts to automate the blinking of one or two LEDs connected to the BeagleBone Black. The tutorial is based on a custom Linux image built with the Yocto Project, tailored for embedded development.

Opinions

  • The author, Arnold the Crocodile, emphasizes the use of a custom Linux image built with the Yocto Project for better embedded functionality and hardware compatibility.
  • The guide suggests that the BeagleBone Black is capable of both digital and analog input/output, but the tutorial focuses specifically on digital output.
  • The author provides clear step-by-step instructions, indicating a preference for a methodical approach to working with GPIOs.
  • The use of shell scripting for controlling GPIOs is presented as an effective method for automating hardware interactions, such as blinking LEDs.
  • The author's inclusion of two script examples implies that scripting can be easily adapted for controlling multiple GPIOs simultaneously.

Understanding the Basics of General-Purpose Input/Outputs on the BeagleBone Black

Layout of the expansion header

Simply said, there are two expansion headers: the left-hand side is called in the BeagleBone Black literature as ‘P9’, while the right-hand side is called as ‘P8’.

Each pin of the expansion header has a certain significance.

In this tutorial I shall limit the examples and screenshots to Digital Output, whereas BeagleBone Black features both Digital and Analog, Input and Output.

Numbering of the GPIO

The AM335x processor has 4 banks (0,1,2,3) of 32 GPIOs each. GPIOs are numbered 0..31 each. This means that, example GPIO1_28 is GPIO 28 of 32 (numbered 0..31) on the second chip/bank (chips 0,1,2,3).

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
  • 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:

  1. 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.

#!/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 "'gpio60' directory already exists.there is no need to create it by writing to 'export'."
else
        echo "'gpio60' directory does not exist.there is need to create it by writing in  the 'export' file"
        echo 60 > /sys/class/gpio/export
fi

if [ -d "/sys/class/gpio/gpio48" ]
then
        echo "'gpio48' directory already exists.there is no need to create it by writing to 'export'."
else
        echo "'gpio48' directory does not exist.there is need to create it by writing in  the 'export' file"
        echo 48 > /sys/class/gpio/export
fi


echo "out" > /sys/class/gpio/gpio60/direction
echo "out" > /sys/class/gpio/gpio48/direction

while  true
 do
        sleep .5 # half a second
        echo  "entering the while.set the GPIO60 value to 1"
        echo 1 > /sys/class/gpio/gpio60/value
        echo "done"
        echo "set the GPIO48 value to 0"
        echo 0 > /sys/class/gpio/gpio48/value
        echo  "done"
        sleep .5

        echo "set the GPIO60  vslue to 0"
        echo 0 > /sys/class/gpio/gpio60/value
        echo "done"
        echo "set the GPIO48 value to 1"
        echo 1 >  /sys/class/gpio/gpio48/value
        echo "done"
done
Beaglebone Black
Gpio
Embedded
Embedded Linux
Embedded Training
Recommended from ReadMedium