Playing with Electronics: RPi GPIO Zero Library Tutorial

Overview

A simple way to learn electronics is using Raspberry Pi and its GPIO Zero Library. With a very few lines of code in Python you will be able to control actuators, read sensors, etc. It was created by Ben Nuttall of the Raspberry Pi Foundation, Dave Jones, and other contributors.

playing-with-electronics-rpi-gpio-zero-library-tutorial

Here in this quick tutorial, I will give you the basis for creating simple circuits controlled by the Raspberry Pi. For full details, please see this link: GPIO Zero V 1.3.1 Documentation. Also you can download it from magPi magazine a full free book that will guide you step by step on several projects using the GPIO Zero library: Simple Electronics with GPIO Zero.

On this tutorial, we will explore as input devices (“sensors “):

  • Button
  • Motion Detector Sensor

And as output (“actuators “):

  • LED
  • Buzzer
  • Generic Digital Output (Stepper Motor)

Let’s go!

Bill of Material

bill-of-material1

  • Breadboard
  • Push-Button
  • LED
  • Resistor 330 ohms
  • Jumper Wire Dupont Cable (Female/Male and Male/Male)

Installing the GPIO Zero Library

installing-the-gpio-zero-library

The first thing to do it is to update your repositories list:

sudo apt-get update

Then install the package of your choice. Both Python 3 and Python 2 are supported. Python 3 is recommended:

sudo apt-get install python3-gpiozero

Pin Numbering

Important to mention that GPIO Zero Library uses Broadcom (BCM) pin numbering for the GPIO pins, as opposed to physical (board) numbering. Any pin marked “GPIO ” in the above diagram can be used as a pin number. For example, if an LED was attached to “GPIO18 ” you would specify the pin number as 18 rather than 12.

“Hello World”: Blinking a LED

hello-world-blinking-a-led

To connect our RPi to the world let’s first connect:

  • Physical Pin 6 (GND) to GND Breadboard Power Grid (Blue -), using a black jumper
  • Physical Pin 1 (3.3V) to +VCC Breadboard Power Grid (Red +), using a red jumper

Now, let’s connect a LED, using the physical pin 12 (GPIO18) connected to LED cathode (longer LED leg). Connect the LED anode to breadboard GND using a 330 ohms resistor to reduce the current that will be drained from the RPi as shown in the above picture.

Once the HW is connected, let’s create a Python program to turn-on the LED:

from gpiozero import LED
led = LED(18)
led.on()

To create and run the program, you can use the Python3 application that appears in the RPi menu or use any text editor by saving the file, for example as “MyPgmName.py” and then executing it using a Command line on the monitor, for example:

sudo Python MyPgmName.py

As you can see, it is very simple to code using GPIO Zero Library.

Now, let’s Blink the LED (the real “Hello world “, when we are talking about HW. To do that, we must also import another library that is “time “. We will need it to define the the amount of time that the LED will be ON and OFF. In our case bellow, the LED will blink with a 1 second time.

Read More:  Playing with Electronics: RPi GPIO Zero Library Tutorial


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

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

Scroll to Top