How can I control a Raspberry Pi GPIO from Bash-Sysfs?

Viewed 90

Using a Raspberry Pi with Raspberry Pi OS Trixie, explain how to configure and control a GPIO directly from the command line using Bash and Sysfs.

Consider a simple circuit consisting of:

  • Raspberry Pi
  • One GPIO pin
  • One LED
  • One current-limiting resistor

Your answer should describe the complete procedure required to make the LED turn ON and OFF from the terminal.

Please consider:

  • How the LED should be connected to the Raspberry Pi.
  • Which GPIO interface/tools are currently available in Raspberry Pi OS Trixie.
  • How to identify the correct GPIO pin.
  • The Bash commands required to configure and control the GPIO.
  • How to verify that the GPIO and LED are working correctly.

Objective: The procedure should be sufficiently clear that another student can reproduce the experiment on a Raspberry Pi running Trixie.

5 Answers

To begin the process of lighting an LED using a Raspberry Pi, we first need to specify which pin will output the signal. In our case, we will be using GPIO 4.

To locate GPIO 4, we refer to a pinout diagram showing the function of each pin on the Raspberry Pi.

1.png

The signal output is located on physical Pin 7 of the Raspberry Pi, while the circuit ground (GND) connects to physical Pin 9.

We start by logging into our Raspberry Pi using our terminal of choice—in our case, PowerShell.

To enter the commands required to turn on the LED, we use the command:
cat /sys/class/gpio/gpiochip*/base

This returns the base GPIO value for the Raspberry Pi chip. We then add our target GPIO pin number (4) to this base value.

Using this calculated GPIO number, we run the following commands to export the pin and output the signal needed to turn on the LED.

2.png

After entering the final command, the LED on GPIO 4 should turn on and be visibly lit on our breadboard setup, as shown below.

3.png

Finally, we run the following command to turn off the LED. You may need to re-enter your user password to grant the necessary permissions to turn off the signal.

4.png

5.png

I have an error during the first steps of the method. I am pasting the output of the bash:

┌─[gmarx][sagittarius][~]
└─▪ cat /sys/class/gpio/gpiochip*/base
512
┌─[gmarx][sagittarius][~]
└─▪ echo 516 > /sys/class/gpio/export
-bash: /sys/class/gpio/export: cannot overwrite existing file
─[gmarx][sagittarius][~]
└─▪ echo 516 > /sys/class/gpio/unexport
-bash: /sys/class/gpio/unexport: cannot overwrite existing file

Consider: the output pins
GPIO 9 (GND) and GPIO17 (signal)
Uploading...
Afterwards, connect the resistor followed by the cathode with the GPIO17 signal, then the anode with GND, use the following code:

sudo su
echo 17 > /sys/class/gpio/chip0/export
echo
echo out > /sys/class/gpio/gpio17/direction
echo 1 > /sys/class/gpio/gpio16/value

this should make the raspberry pi zero turn the LED on.

No image is attached; this is just a comment that says uploading. Following your instructions, I cannot find gpio17. I am pasting my bash output:

root@sagittarius:/home/gmarx# echo 17 > /sys/class/gpio/chip0/export
root@sagittarius:/home/gmarx# echo

root@sagittarius:/home/gmarx# echo out > /sys/class/gpio/gpio17/direction
bash: /sys/class/gpio/gpio17/direction: No such file or directory

output-image

First step: we need to choose a GPIO to wich we can connect our LED (in this case GPIO 17). To determine which one, we must identify the suitable ones. To do this, we look at the diagram for those marked as GPIO. As same with GND
6f32dd42322ed5bdf6996d14097ba736.jpg

Second step: we have to physically connect the GPIO as shown in the diagram, using a resistor between 800 and 2000 ohms.
Captura de pantalla 2026-09-09 203201.png

Third step: make a connection between your computer to raspberry, using "sudo su" for superuser access

Fourth step: change the direction using "cd /sys/class/gpio/chip0". Once there use "echo 17 > /export" command, this one enables GPIO17 to the user

Fifth step: change again the direction using "cd "/sys/class/gpio/chip0/gpio17". Here we have to use "echo out > direction" to change GPIO mode as out

Finale step: we can toggle GPIO value between high or low logic state with "echo 1 > value" o " echo 0 > value".

HOW CAN I CONTROL A RASPBERRY PI GPIO FROM BASH-SYSFS?

1. Required components and Circuit connection
Before running the commands, you must correctly assemble the electronic circuit to avoid damaging the components and the Raspberry Pi.

a) Materials:

  • A Raspberry Pi running Raspberry Pi OS Trixie.
  • An LED (any color).
  • A 220-ohm to 330-ohm resistor. This limits the current and prevents the LED from burning out.
  • A breadboard.
  • Jumper wires.
  • A suitable power supply for the Raspberry Pi (cable).
  • A microSD card with Raspberry Pi OS Trixie installed.

b) Circuit connection:
1. Place the LED: Connect the anode (long leg) of the LED to GPIO17, which corresponds to physical pin 11.
2. Connect the resistor: Connect the cathode (short leg) of the LED to a 220–330 Ω resistor.
3. Close the circuit to ground: Connect the other end of the resistor to GND—for example, to physical pin 6.
4. Insert the microSD card into the Raspberry Pi.
5. Connect the USB-to-micro-USB cable to the computer and the Raspberry Pi. The cable should be connected to the second port on the Raspberry Pi, right where it says "USB."
6. Wait for the Raspberry Pi OS Trixie operating system to fully boot up before opening the terminal.

pines.png

conexion2.png

2. GPIO Interfaces available in Raspberry Pi OS Trixie

Raspberry Pi OS Trixie features modern tools for controlling GPIO via the GPIO Character Device interface, utilizing libgpiod tools such as: gpioinfo, gpioget, gpioset, gpiomon, and gpiofind. Similarly, there is the Sysfs interface located at /sys/class/gpio, which allows for exporting GPIOs via the export and unexport files and controlling their direction and value via the direction and value files. However, this interface is currently considered obsolete by the Linux kernel, and using the Character Device interface is recommended.

This project specifically uses the Sysfs interface, as the goal is to learn how to control a GPIO directly from Bash using system files.

3. Identifying GPIO pins

The Raspberry Pi labels its pins in two different ways. The first is the physical pin number, which refers to the pin's actual position on the 40-pin header. The second is the GPIO number (BCM), which is the internal identifier used by the Broadcom processor.

To check the pin layout from the terminal at any time, run the following command:

 pinout

This command displays a graphical layout of the board on the screen, confirming which physical pin corresponds to a specific GPIO. In this case, it will show that physical pin 11 corresponds to GPIO17.

4. Step-by-step procedure via the terminal (Bash-Sysfs)

1. Next, open the terminal. The Sysfs interface for GPIO is typically located at:

/sys/class/gpio

2. Verify that the directory exists:

ls /sys/class/gpio

ls lists the contents of a directory. The two files we are interested in seeing are export and unexport. export allows you to request access to a pin, while unexport allows you to release it later.

3. Enter the directory. To enter the directory, use the cd (change directory) command:

cd /sys/class/gpio

4. To see which directory you are currently in, run pwd; the following should appear:

/sys/class/gpio

5. Export GPIO17. Before you can control a GPIO via Sysfs, you must export it:

echo 17 | sudo tee export

Note: The | symbol is called a pipe; it passes the output of the first command to the next one. tee writes the received value into the export file, and sudo performs the operation with administrator privileges.

After running this command, a directory named the following should appear:

gpio17

Run ls to verify this.

6. Enter the gpio17 directory. To enter the directory, run:

cd gpio17

Run ls to verify; the files direction (which controls whether the GPIO acts as an input or output) and value (which controls the logic state) should appear.

7. Check the current direction.

cat direction

cat displays the contents of a file. If in appears, it means the GPIO is configured as an input.

8. Configure GPIO17 as an output. Since we want to control an LED, GPIO17 must be configured as an output. To do this, run the following line of code:

echo out | sudo tee direction

Run cat direction to verify; out should now appear.

9. Check the initial state of the GPIO. Run:

cat value

The result should be the number 0, indicating a low level—meaning the LED is off.

10. Turn on the LED. To turn on the LED, execute the following line of code:

echo 1 | sudo tee value

Here, the number 1 signifies that the LED is on (high level). Observe the LED on the breadboard turning on.

11. Check the status. To verify the stored value, execute the following code:

cat value

A 1 should appear here. Therefore, GPIO17 = 1, meaning the LED is on.

12. Turn off the LED. To turn off the LED, type:

echo 0 | sudo tee value

13. Check the LED status again. Execute:

cat value

A 0 should now appear (LED off).

14. Turn the LED on and off multiple times. To do this, repeat steps 10 through 13 to verify operation.

15. Automatic test. If you want the LED to turn on and off automatically, execute the following code, where sleep specifies the number of seconds to wait between turning the LED on and off.

echo 1 | sudo tee value
sleep 2
echo 0 | sudo tee value

16. Return to the directory. When the exercise is finished, exit the GPIO17 directory. To do this, enter:

cd ..

After executing this, you should be back in /sys/class/gpio. To verify, execute pwd; it should display /sys/class/gpio.

17. Release GPIO17. To release the GPIO, type:

echo 17 | sudo tee unexport

18. Verify that GPIO17 was released. Using the ls command, you should see the following:

export
unexport

Commands does not work. I recommend to test your answer previously.
This is my output:

┌─[gmarx][sagittarius][~]
└─▪ cd /sys/class/gpio
┌─[gmarx][sagittarius][.../class/gpio]
└─▪ echo 17 | sudo tee export
[sudo] password for gmarx:
17
tee: export: Invalid argument
┌─[gmarx][sagittarius][.../class/gpio]
└─▪
Related