Wikijunior:Raspberry Pi/Raspberry Pi Python GPIO Zero Simple LED Tutorial

Tutorial by Andrew Oakley
Public Domain 24 Sept 2016
www.cotswoldjam.org

The electronics

edit

Light-emitting diode (LED)

edit
 

The light-emitting diode (LED) has a short leg and a long leg. If you feel around the rim, you'll also find a flat edge.

The short leg and flat edge always connect to the negative (ground).

Resistor

edit
 

The resistor can be connected any way around. Any resistor between 220 and 470 Ohms will work fine.

Jumper wires

edit
 

Jumper wires (also called DuPont wires) connect electronic components to the GPIO pins.

You should also have two short and one long jumper wire (all "female to female" – holes at both ends).

Putting it together

edit
 

Step 1: Connect one short wire to pin 7 (GPIO 4). This is the 4th pin from the left of the bottom row.

Step 2: Place the resistor at the end of the short wire.

Step 3: Connect another short wire to the resistor.

Step 4: Connect the end of this short wire to the long leg of the LED. This is a positive connection.

Step 5: Next, connect the long jumper wire to the ground (negative). There are several ground pins on the Raspberry Pi, for example, the 5th pin from the left of the bottom row which is pin 9 (GND).

The program

edit

Power up your Raspberry Pi. From the desktop menu, select Programming – Python 3 (IDLE). Then use File, New Window to create a new program.

Type in the following program, or alternatively you can use File, Open to open the led.py program in the python/led folder.

from gpiozero import LED
from time import sleep

led=LED(4)
led.on()
sleep(1)
led.off()

Use File, Save to save this program as led.py and then run it by selecting Run menu, Run Module. You should see the LED light up for one second, and then turn off.

Not working? Check that you have:

  • The correct GPIO pins.
  • That the LED is the correct way round; short leg to ground (GND).
  • There are no spelling mistakes, no missing brackets, and you've used round brackets instead of pointy, curly or square ones.

What is this program doing?

edit
from gpiozero import LED 
from time import sleep

The first two lines tell the computer to learn about a new thing. Computers can learn from programs that other people have written, and we call these other programs "libraries". Our instructions tell the computer to learn about LEDs from the GPIOZero library and learn about sleeping from the time library.

led=LED(4)

We then create an LED object. We say that the LED is connected to GPIO 4.

led.on()
sleep(1)
led.off()

Turn the LED on, wait for 2 seconds, then turn it off.

See if you can flash SOS in Morse code. Have a look at the led2.py program and learn about the led.blink() command.

Files

edit

Cjam-led-gpiozero-tutorial.pdf

edit

The original PDF for this tutorial is available on Wikimedia Commons: Cjam-led-gpiozero-tutorial.pdf

led2.py

edit
from gpiozero import LED

led=LED(4)

# Blink the time signal
# 6 short flashes followed by 1 long
led.blink(0.5,0.5,6,False)
led.blink(1,0,1,False)

# led.blink() has 4 parameters:
#   On time, in seconds, can be decimal fraction
#   Off time, in seconds, can be decimal fraction
#   Repeats, a whole number
#     or the value None which means forever
#     default is None
#   Background, whether to wait (False)
#     or carry on (True) (default is True)
# Try to blink SOS in Morse Code
# S = short short short
# O = long long long