• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
KaliTut

KaliTut

Kali Linux tutorial and Linux system tips

  • Home
  • Raspberry Pi
  • Privacy Policy
  • About us

Control Raspberry Pi GPIO with sysfs

Last Updated on July 19, 2020 by Kalitut 1 Comment

Control and program Raspberry Pi GPIO with “sysfs” (shell/bash)

raspberry pi gpio sysfs

The GPIOs are represented on Raspberry Pi as files and can be found in the directory tree under “/ sys / class / gpio”. This is called “sysfs”. If you look at the contents of the files, you get the state out. This is either “1” or “0”.

ls /sys/class/gpio

When controlling and programming you have to make sure that you do not choose the pin number, but the BCM number of the GPIO. In the following example, we configure the GPIO 17 (pin 11) as the output and the GPIO 18 (pin 12) as the input.

  • Raspberry Pi GPIO assignment

Note: Programming the GPIOs via “sysfs” is not recommended. The recommended way is with the command line tool “pigpio”.

  • Control Raspberry Pi GPIO with pigpio

Configure GPIO as input or output
To configure a GPIO you have to create it first. Then you decide on the direction. So either as input or output. First, we turn on the “GPIO 17”.

echo 17> /sys/class/gpio/export

If the GPIO is to be configured as an input, the following command applies.

echo in> /sys/class/gpio/gpio17/direction

If the GPIO is to be configured as an output, the following command applies.

echo out> /sys/class/gpio/gpio17/direction

A GPIO configured as an output should then be given a status, ie “high” or “low”.

State of a GPIO output set
Basically you can only set outputs. To set the state of a GPIO output to “high”, the following command is sufficient.

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

To set the state of a GPIO output to “low” the following command is sufficient.

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

Determine GPIO status
To determine the state, ie “high” or “low”, at a GPIO, the following command is sufficient. The GPIO can be both an input and an output.

cat /sys/class/gpio/gpio17/value

The output is “1” for “high” or “0” for “low”.

Disable GPIO
If you no longer need GPIOs, then you should disable them. The following commands will cause the files and directories of the GPIOs to disappear. When restarting, the GPIOs are automatically reset.

echo 17> /sys/class/gpio/unexport

Filed Under: Raspberry Pi Tagged With: Raspberry Pi GPIO

Comments

  1. jpmh says

    December 11, 2022 at 10:40 pm

    Probably the best and most concise posting I have ever seen.

    I needed to control pins and you showed me how. TY

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Follow us

  • Facebook
  • Twitter
  • YouTube

Categories

  • Android pentesting tools
  • Arduino
  • Books
  • Darknet
  • database
  • General
  • Github Tools
  • Hacking
  • Kali Linux
  • Linux
  • Linux Commands
  • Network Administrator
  • Penetration Testing
  • Penetration Testing Tools
  • PowerShell
  • Raspberry Pi
  • resources
  • Review
  • Termux
  • Tutorials
  • Ubuntu
  • Uncategorized
  • Video Tutorials
  • vmware
  • WiFi Adapter
  • WiFi Pentesting
  • Wireless Router
  • Wireshark

Recent Posts

  • Hijacked Wi-Fi? Thorough explanation of hacking techniques
  • Windows PowerShell tutorial for beginners
  • Learn to Hack Steps from Beginner to Hacker
  • PowerShell Tutorial – GUIDE introduction with basics
  • Top Hacking Tools
  • Home
  • About us
  • Privacy Policy
  • Affiliate disclaimer

Copyright © 2023