Flash an LED with Python

Last time they made a simple LED circuit, but didn't control it with the Pi. This time the students will control an LED with a Python program. Once they can control an LED, the same technique can be used to control motors, speakers and other actuators.

Learning objectives

Students will learn:

Resources

Lesson Plan

Setup Pi : 5 minutes

Use the stopwatch code to time your students, they should be pretty fast by now! You could time how long it takes until they have started Idle. The author managed to go from unplugged to typing code into Idle in 68 seconds.

Looping with Python : 10 minutes

The Raspberry Pi has many GPIOs (General Purpose Input/Output) that we can use. They can either be set as an input or an output. Flashing an LED is a good first example of using the Pi's output to controlling something in the real world. Once they can control an LED we can move on to things like motors and buzzers.

Ask the students to think about what is needed to flash a light on and off:

  1. Being able to control the pins on the Raspberry Pi with python,
  2. Pausing a short moment - without a pause the LED will flash so fast it will just look dim!
  3. A loop, so the LED will continue flashing,
  4. The physical circuit: an LED, resistor and some wires.

Ask the students if they've seen any code that could something similar? The stopwatch program from last time has the loop, and it can pause. What it's missing is the ability to turn on and off the LED.

Ask the students to open the stopwatch.py program they used last time and save it as a new file flash.py. Then ask them to modify the program to the following:

Check that everyone has managed to get the program printing out the messages.

Add the LED control : 10 minutes

We need to use another library to control the LEDs. Get the students to add these lines to the top of their flash.py code. They can leave in the printing lines, as they are helpful for debugging.

These lines are also in the handout.

The first line imports the new library, the second sets the library so they can refer to the pins by their physical number, and the 3rd turns off some distracting warnings:

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)

Now we store the pin number in a variable, so it's easy to change later, and then set that pin to be an output:

led_pin = 8
GPIO.setup(led_pin, GPIO.OUT)

Finally, we can turn an LED on with this line:

GPIO.output(led_pin, True)

And off with this line:

GPIO.output(led_pin, False)

Challenge your students to finish the program themselves. If they get stuck, see the example below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#import the library to control the GPIO pins
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)

#import the time library
import time

led_pin = 8

#set the pin to be an output
GPIO.setup(led_pin, GPIO.OUT)

#in a loop:
while True:
    #turn on the led
    print('LED on')
    GPIO.output(led_pin, True)
    #wait for 1 second
    time.sleep(1)

    #turn off the led
    print('LED off')
    GPIO.output(led_pin, False)
    #sleep again
    time.sleep(1)

Running the program : 10 minutes

Ask the students to run the program. It will fail with this error:

RuntimeError: No access to /dev/mem. Try running as root!

This is because we can only access the GPIOs as the super user (root). Here's how to run Idle as the super user:

These instructions are also in the handout.

Build the circuit : 15 minutes

Ask students to build the LED circuit in their handout. When their circuit is correct, their LED should start flashing.

Explain that the LED is connected to pin 8 because we set the led_pin variable to pin 8 in the software. If we wanted the LED on another pin, we'd have to make sure the hardware and the software matched up. We can use any pin that isn't marked GND, 3.3v or 5v. Ask the students to identify which is pin 8. They can use the etching on the Pi holder. It is the 4th pin down on the right hand side.

If you're not using our Pi mounting kits, then here's a picture of the GPIOs with ground (same as GND), 3.3v or 5v marked.

 GPIOs

Explain that the controlling an LED is just the first and simplest thing they can control. They can use the same technique for controlling motors, though they'd also need some kind of amplifier.

Extension activities

Pack away : 5 minutes

Pack away the Pi. Remind students to follow the instructions in last lesson's handout.

Homework

Outcome

All students:

Most students:

Some students: