How to turn on an LED with gpiozero & RPi.GPIO

Published on: March 9, 2019

Welcome to another Raspberry Pi Tutorial. Today's tutorial covers something simple. How to turn on an LED! 

This is a simple tutorial, but it will serve as a precursor to my upcoming tutorials because we'll be using these libraries to make bigger and better projects. 

I use both methods for my home automation projects. Although we are turning on an LED, the same idea can be used to turn on a Relay or a transistor, which in turn can control LED strips, lights, and pretty much anything else that can be turned on and off. 

I give examples with two libraries: gpiozero and RPi.GPIO. They are both good and can be used for many purposes. 

By default, both libraries should come installed with your Raspbian. The RPi.GPIO library should definitely be installed. And if for some reason, gpiozero is not installed, use this command to install it before continuing:

sudo apt install python3-gpiozero

For this project, I use GPIO Pin 18 for the LED. Here's a simple fritz:

After you have things installed the software and hardware, you are ready to go! You can either follow the video tutorial below or just find and run the two little scripts I created in the video on Github: https://github.com/naztronaut/rpi.gpio_gpiozero_led_example

Just run them with the command python3 scriptName.py.

Come back for the next few tutorials where I'll show you how to install Flask, Apache, and then Flask behind Apache, followed by how to turn your LED on and off through your browser!

Have questions about the tutorial! Ask below, I welcome all questions and comments.

Remember to checkout the Resources section below for associated downloadable content, JSFiddle links, and other resources. Watch the video and follow along!

Resources:



Comments: