Skip to main content
Chemistry LibreTexts

2: GPIO zero

  • Page ID
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

    ( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\kernel}{\mathrm{null}\,}\)

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    Note: GPIO Zero is not a standard library on most Python installations, but it is installed with the Raspberry PI OS because the GPIO pins are such an integral part of the Raspberry Pi.  The goal of this section is not so much to teach you how to use the features of the GPIO Zero Python Library but to use it as an example to understand Python libraries. So you must go to the documentation at and we will discuss the sections of the document here, but you must learn how to use the document to learn how to use the library in your coding.

    Installing GPIO Zero

    Most Python Libraries can be found at the Python Package Index ( and utilize resources of GitHub ( and Read The Docs (, and a search of any of these resources will provide information on GPIO Zero (they all link to each other). If you are using Thonny you can find these resources through tools/manage packages/install from PyPi packages and search for gpiozero.  It will provide you with links to the above resources, and the option to directly install it from the Thonny IDE, but we will discuss the command line technique using Linux. Please follow this in, where you are given several different commands for installing this library. Lets look at the following Linux code:

    pi@raspberrypi:~$ sudo pip3 install gpiozero
    • pi@rasberrypi:~$: this is the command prompt, that typically has the username (pi), hostname of the machine (raspberrypi) and the directory (~ inidcates home directory)
    • sudo: "superuser do" allows you to run the command with the privledges of the root user without having to log in as root user.  This gives you access to all files and system settings.
    • pip3: is the package manager for the Python 3.x.  It can install packages from PyPi, Git or local source files
    • install: is the argument passed to pip3 telling it what you want to do (install a package)
    • gpiozero: is the package you want to install, which is a library for controlling the 40 GPIO pins.



    This is the jist of how to use GPIO zero and you need to go over it.  You will see a layout of the GPIO pins, different ways to assign code to a pin, and basic code tasks.  There are two levels of recipes; BASIC and ADVANCED. This should be the first place you seek code. The first few lines of a recipe tell you what components of the GPIO library you need to use in order to perform the desired task, along with any other components of other libraries that are needed.  

    Remote GPIO 

    You can configure the Raspberry Pi OS so that the GPIO pins can be operated remotely.  You must first activate this feature in the Raspberry Pi. If you are using a non Raspberry Pi as the remote computer, you will need to install pigio.  Note, if you are using VNC or SSH you are not running these remote, but from the Pi. An application where you may wish to use remote GPIO is a distributed system where multiple Pis are interacting with each other. 


    [This is under construction] These are like the parameters of object methods (functions) and easily allows one device to update another with its value, and thus avoid long procedural programming constructs like a while loop. This enables event-driven programming.  Every GPIO zero object has a value, which typically ranges 0 to 1 (PWM), -1 to 1 (reversible electric motor) or Boolean True/False.  So for example, the class LED has the method .value which is either true (on) or false (off).  So led.value=True is like led.on()


    API-Input Devices

    Base Classes

    13.2 Base Classes

    clipboard_ef81760c6eec4d7848b43559bdc8d9edc.pngFigure \(\PageIndex{1}\):Schematic for the base input classes. (Nuttall, BSD-3-Clause)

    13.2.1 DigitalInputDevice


    class gpiozero.DigitalInputDevice (pin, *, pull_up=False, active_state=None, bounce_time=None, pin_factory=None) [source]

    13.2.2 SmoothedInputDevice

    class gpiozero.SmoothedInputDevice (pin, *, pull_up=False, active_state=None, threshold=0.5, queue_len=5, sample_wait=0.0, partial=False, pin_factory=None)[source]

    13.2.3 InputDevice

    class gpiozero.InputDevice (pin, *, pull_up=False, active_state=None, pin_factory=None)[source]

    13.2.4 GPIODevice

    class gpiozero.GPIODevice (pin, pin_factory=None) [source] 




    Regular Classes


    13.11Button (readthedocs)

    class: gpiozero.button (pin, pull_up, active_state, bounce_time, hold_time, hold_repeat)

    • pin: the pin number
    • pull_up: 
      • True (default) means pin starts high and you hook across ground
      • False means it starts low and you hook across to 3.3V
      • None means it is "floating" and you must externally pull up or down and the active_state parameter must be set.
    • active_state: (bollean or none)
    • bounce_time: (float or none) - default is none
    • hold_time: amount of time betweem when the button has been pushed and an action initiates (1 sec is default)
    • hold_repeat: if True, when_held repeatedly executes command

    Some of the methods

    • wait_for_press
    • wait_for_releast
    • held_time
    • hold_repeat
    • hold_time
    • is_held
    • is_pressed
    • pin
    • pull_up
    • pull_down
    • value
    • when_held
    • when_pressed


    Line Sensor

    13.1.2 LineSensor (TRCT5000)

    Class gpiozero.LineSensor(pin, queue_len,sample_rate, threshold,partial)

    extends the SmoothedInputDevice for sensor with three pins, VVV (3.3V), gnd, Out (data)

    Motion Sensor

    13.1.3 MotionSensor (D-SUN PIR)

    Extends SmoothedInputDevice for passive infra=red (PIR) motion sensors

    API-Output Devices

    Light Sensor

    13.1.4 LightSensor

    Distance Sensor

    13.1.5 DistanceSensor (HC-SR04)

    API-Output Devices

    Base Classes



    clipboard_eeeadb1fa3efb2d050689b4a91a1b8764.pngFigure \(\PageIndex{1}\):Schematic for the base output classes. (Nuttall, BSD-3-Clause)




    Digital Output Device

    class gpiozero.DigitalOutputDevice (pin, *, active_high=True, initial_value=False, pin_factory=None) [source]

    PWM Output Device

    class gpiozero.PWMOutputDevice (pin, *, active_high=True, initial_value=0, frequency=100, pin_factory=None) [source]

    Output Device

    class gpiozero.OutputDevice (pin, *, active_high=True, initial_value=False, pin_factory=None) [source]

    GPIO Device

    class gpiozero.GPIODevice (pin, *, pin_factory=None[source]


    Regular Classes


    class gpiozero.LED (pin, *, active_high=True, initial_value=False, pin_factory=None) [source]


    class gpiozero.PWMLED (pin, *, active_high=True, initial_value=0, frequency=100, pin_factory=None) [source]

    RG LED

    class gpiozero.RGBLED (red, green, blue, *, active_high=True, initial_value=(0, 0, 0), pwm=True, pin_factory=None) [source]

    Additional Devices


    This page titled 2: GPIO zero is shared under a not declared license and was authored, remixed, and/or curated by Robert Belford.

    • Was this article helpful?