How to check GPIO status

The GPIO (General Purpose Input/Output) pins of the Raspberry Pi are how the device can interface with external hardware components. It is how things like sensors and actuators plug into the Raspberry Pi, and allow users to control such devices from the Raspberry Pi itself. Something as simple as turning an LED on or off will require setting the pin to which it is plugged in to either true or false, which changes the current status of that pin.

When working with the GPIO pins and the hardware that utilize them, you will be activating devices by turning certain pins on or off. Unless you are physically in front of the device, you will also need a way to check the current status to see if the pin is set to true or false. Going back to the LED example, you will not know the current status unless you are close enough to actually see the light. Fortunately, we can also get this information from Python on the command line. In this tutorial, we will show you how to check the current GPIO status on a Raspberry Pi.

In this tutorial you will learn:

  • How to install or update Python3 and pip
  • How to download the RPi.GPIO Python library
  • How to turn a GPIO pin on and off
  • How to check the status of a GPIO pin
How to Check GPIO Status
How to Check GPIO Status
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Raspberry Pi
Software Python, pip, RPi.GPIO library
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Check GPIO Status on Raspberry Pi step by step

In order to check the GPIO status on Raspberry Pi, we will be working with Python. The RPi.GPIO Python library will allow us to interface with the GPIO pins in order to turn them on or off, or check their status as you will see below.

There are several other Python libraries available which can also do the job. You can also use other programming languages like Perl, C#, and even the Bash shell to check status. However, Python and the RPi.GPIO library is one of the most popular ways to manipulate GPIO status on Raspberry Pi.
  1. Python and pip should be installed by default, but just in case, execute the following commands to install or update them:
    $ sudo apt update
    $ sudo apt install python3 pip
  2. Next, let’s download the RPi.GPIO Python library with pip:
    $ pip install RPi.GPIO
  3. Load the Python interpreter by executing:
    $ python3
  4. Next step is to import the RPi.GPIO library that we downloaded:
    >>> import RPi.GPIO as GPIO
  5. Assuming that you want to use the BCM numbering scheme, we set the GPIO mode accordingly with the following command:
    >>> GPIO.setmode(GPIO.BCM) 
  6. For this example, we will work with pin number 17 to check its status. Let’s first set it up as output:
    >>> GPIO.setup(17,GPIO.OUT)
  7. Then, switch pin 17 on:

    >>> GPIO.output(17,True)
  8. Now we can read the state of pin 17 with the following little script. We will need to read the input of the pin in order to get its status, then simply print whether the pin is ‘on’ or ‘off’:
    state = GPIO.input(17)
    if state:
  9. Totally optional, but if you want to turn the pin back off and get rid of the configuration you have applied by executing the commands above, we can type:
    >>> GPIO.output(17,False)
    >>> GPIO.cleanup()

Closing Thoughts

In this tutorial, we saw how to check the status of a GPIO pin on the Raspberry Pi. We accomplished this by importing the RPi.GPIO Python library, and then using Python commands to interact with a GPIO pin and check its current status. This is only one of the many ways available, but certainly one of the most common, since a large amount of users choose to script in Python for interfacing with external hardware components. Using the method shown above, we can build a simple Python script that lets us know the status of any or all GPIO pins.

Comments and Discussions
Linux Forum