Hello everyone and welcome to PioTex! This little blog will post tutorials and for the anything to do with the Raspberry Pi! Today I will be teaching you to control an LED using GPIOZero; a library composed by Ben Nuttall at the foundation who aims to make using the GPIO on the pi simple, and more accessible to beginners.
Firstly, you will need to wire up an LED and for this you will need an LED, breadboard, 330 (or similar) ohms resistor, 2 male to female jumper cables as well as a pi and peripherals. LEDs can be wired without a breadboard, just with 2 female to female jumper cables, however it really depends on what you have available, so do whatever method suits you best!
The longer, positive leg of the LED (or anode) should be connected to the resistor and then to a GPIO pin, which can be any, however I have used GPIO10. The shorter, negative leg (or cathode) is simply connected to ground. If you want to remember which pin is which, you can print off a raspberry leaf, get a Portplus keyring etc.
After wiring the LED, double check it to make sure the wiring is correct, as you have a risk of damaging your pi through incorrect wiring. When you are sure it is correct, boot up your pi into either the graphical environment or command line, it really does not matter which as Raspbian Jessie now supports sudo-less GPIO access, meaning that you do not need to be a super user on the pi in order to access the GPIO. If you are in the graphical environment, navigate to programming, then Python3, then press control + n for a new document. If you are in the command line then type “nano led.py” to create a new document.
In this program we will code the LED to turn on, wait one second and turn off, making the LED effectively blink.
To do this, copy this code into your python window, remembering to change the LED pin number to the GPIO pin you have selected (GPIOZero uses BCM and not Board for labelling the pins
from gpiozero import LED
led = LED(10) # change this to whatever pin you have selected
while True: # making the program loop forever
# indent the next 3 lines by pressing tab or space 4 times
Thank you for reading! See you next time with some more GPIOZero tutorials!