DEV Community

Quy Nguyen
Quy Nguyen

Posted on

Test GPIO pins on BeagleBone Black by toggling high to low

Tutorial: Testing GPIO Pins on BeagleBone Black Using Bash
This tutorial explains how to write and execute a Bash script to test GPIO pins on the BeagleBone Black. The GPIO (General-Purpose Input/Output) pins are versatile and can be controlled programmatically. Here, we'll guide you step-by-step on using a simple script to toggle a GPIO pin.

Image description

  1. Prerequisites Before starting, ensure the following:

You have access to a BeagleBone Black board.
The board runs a Linux-based operating system.
You have root access (some operations require elevated privileges).
The GPIO pin numbering and mappings for your board are known.

  1. Understanding GPIO Pins GPIO pins are accessible through the Linux filesystem under /sys/class/gpio. Each GPIO pin is represented by a number that you can use to control its state. For example:

P8 Pin 12 corresponds to GPIO1_12, which is 44 in GPIO numbering.
The calculation for GPIO number is:
GPIO Pin = (Bank Number × 32) + Pin Number.
Refer to the BeagleBone Black GPIO Pinout for details.

  1. Script Overview The script performs the following:

Exports a GPIO pin for user control.
Sets the pin direction to output.
Toggles the pin state (HIGH/LOW) three times with 1-second intervals.
Unexports the pin after testing.

  1. The Bash Script Here’s the script:
#!/bin/bash

# Define the GPIO pin to test
# Examples of GPIO numbers:
# P8 Pin 11 -> GPIO1_13 = 45
# P8 Pin 12 -> GPIO1_12 = 44
# P8 Pin 14 -> GPIO0_26 = 26
# P8 Pin 16 -> GPIO1_14 = 46

TESTPIN=44  # Replace with your target GPIO pin number

# Export the GPIO pin to make it available
echo "$TESTPIN" > /sys/class/gpio/export

# Set the direction of the pin as output
echo "out" > /sys/class/gpio/gpio$TESTPIN/direction

# Toggle the pin value HIGH and LOW three times
for i in {1..3}; do
  echo "Setting gpio$TESTPIN HIGH"
  echo "1" > /sys/class/gpio/gpio$TESTPIN/value
  sleep 1  # Wait for 1 second

  echo "Setting gpio$TESTPIN LOW"
  echo "0" > /sys/class/gpio/gpio$TESTPIN/value
  sleep 1  # Wait for 1 second
done

# Unexport the GPIO pin after testing
echo "$TESTPIN" > /sys/class/gpio/unexport
Enter fullscreen mode Exit fullscreen mode

If you want more information, please visit GOVAPEMAKER

Top comments (0)