Learn how to use the General Purpose Input/Output (GPIO) pins on your Raspberry Pi to control hardware devices.

This guide will walk you through the process of installing and using the GPIO library on your Raspberry Pi, allowing you to easily interface with a variety of external devices. …


Updated October 8, 2023

Need help with your Raspberry Pi?
Contact Me!

Do you love silly Raspberry Pi Projects?
Check out my this YouTube Channel!


This guide will walk you through the process of installing and using the GPIO library on your Raspberry Pi, allowing you to easily interface with a variety of external devices.

Introduction

The Raspberry Pi is an incredibly versatile device that can be used for many different projects. One of its most useful features is its General Purpose Input/Output (GPIO) pins, which allow you to control and interact with external hardware components. This guide will show you how to install the GPIO library on your Raspberry Pi, as well as provide some examples of how to use it in practice.

Prerequisites

Before you begin, make sure that you have a Raspberry Pi running the latest version of Raspbian (the default operating system). You should also have a basic understanding of Python programming and be comfortable using the command line interface.

Installing the GPIO Library

Installing the GPIO library is a straightforward process. First, open up a terminal window on your Raspberry Pi and update your package list:

sudo apt-get update

Next, install the python3-gpiozero package using the following command:

sudo apt-get install python3-gpiozero

This will install all of the necessary dependencies for the GPIO library and make it available for use in your Python programs.

Using the GPIO Library

The GPIO library makes it easy to control external devices by providing a high-level interface for interacting with the Raspberry Pi’s GPIO pins. Here are some basic examples of how you can use the library to accomplish common tasks:

Blinking an LED

One of the simplest things you can do with the GPIO library is blink an LED. First, connect an LED to your Raspberry Pi using a resistor and a jumper wire. The long leg of the LED should be connected to GPIO pin 18, and the other end of the resistor should be connected to ground (GND). Then, run the following Python code:

from gpiozero import LED
from time import sleep

led = LED(18)

while True:
    led.on()
    sleep(1)
    led.off()
    sleep(1)

This script will turn on the LED for one second, then turn it off for one second, creating a blinking effect. You can adjust the sleep values to change the speed of the blink.

Reading a button press

You can also use GPIO pins as inputs to read the state of external buttons or switches. For this example, connect a pushbutton to GPIO pin 23 and ground (GND). Then, run the following Python code:

from gpiozero import Button

def button_pressed():
    print("Button pressed!")

button = Button(23)
button.when_pressed = button_pressed

This script will print “Button pressed!” to the console every time you press the button. You can modify the button_pressed function to perform any action you’d like when the button is pressed, such as turning an LED on or off.

Controlling a servo motor

If you have a servo motor, you can use the GPIO library to control its position. First, connect the servo motor to a PWM (Pulse Width Modulation) pin on your Raspberry Pi, such as GPIO pin 12. Then, run the following Python code:

from gpiozero import Servo
import time

servo = Servo(12)

while True:
    servo.min()
    time.sleep(1)
    servo.mid()
    time.sleep(1)
    servo.max()
    time.sleep(1)

This script will rotate the servo motor to its minimum, mid, and maximum positions in a continuous loop. You can adjust the time.sleep values to control the speed of the rotation.

Conclusion

With the GPIO library installed on your Raspberry Pi, you can easily interface with a wide range of external devices using just a few lines of Python code. The possibilities are endless - from controlling LEDs and servos to reading sensor data and driving motors, the GPIO pins provide a versatile and powerful tool for building all kinds of projects.