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

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay