Skip to 0 minutes and 4 secondsLast week, you wrote your first Python programme. This week, we're going to do something physical with our programming, and today, we're going to be controlling some LEDs. We're going to start the session assuming that you have your LEDs set up and connected to your Raspberry Pi. Then, just like last week, we're going to go to the programming menu and open Python 3. We're not, this week, going to spend any time in the shell. Instead, we're going to go to File, New File, and create our programme. Last week, we imported the Time module. This week, we're going to use a different module called GPIO 0, which makes controlling physical hardware nice and easy.

Skip to 0 minutes and 44 secondsFrom GPIO 0, we want to import a class called LED. And a class is just a description of how an LED should behave.

Skip to 0 minutes and 58 secondsAnd then the second line we want to write is, we want to tell it where that LED is and give it a name.

Skip to 1 minute and 6 secondsI'm going to use a variable name, red_led and tell it that that is an LED, and it's going to be on pin 17. This should correspond with the pin number that I've used to physically connect the LED to my Raspberry Pi. Next, let's run our programme and see what we have working. So we first of all save it again. Ultimately, we're going to make our programme blink, so I'm going to call my programme Blink.

Skip to 1 minute and 36 secondsAnd if we run our programme, nothing very much happens. That's because all we've done is told the Raspberry Pi that we have an entity and it is on pin 17. We're going to call it red_led. We've not told it what to do with that. Once we have that object, though, we can start to play around with it. So if we were to type in red_led.on(), that command, when we press Enter, should turn our LED on.

Skip to 2 minutes and 8 secondsThe same is true if we type red_led.off(). That should turn our LED off. These two steps are really important because they allow us to test that we have control of the LED. If, at this point, your LED has not turned on or off, go and debug your circuit and check you have the LED the correct way around. To make our LED start blinking, we need to add two things to our programme. The first is, we need to import an extra function. So from Time, we're going to import Sleep, just like we did last week. And secondly, we're going to need a loop to make our light flash on and then off again.

Skip to 2 minutes and 47 secondsA loop is a structure common to most programming languages, and in this case, we're going to use an infinite loop called a while loop. If we write the line while Trip, this will make Python loop around forever, and every line inside the loop is indented. Let's start with a red_led.on().

Skip to 3 minutes and 10 secondsThis will make our light come on. We then go out and pause it using the Sleep command, and I'm going to go for one second. We're then going to turn our LED off.

Skip to 3 minutes and 24 secondsAnd after that, we're going to make it sleep again for one second. And there we go. That's our blinking programme. If we're now to run our programme, it will ask us to save again. Our programme is now running, and we should see our LED blinking on, off, on, off. Fantastic.

Skip to 3 minutes and 49 secondsIt may not feel like much, but you've just created something that controls a physical object using code. This is a really important moment in stepping from the virtual world into the physical realm with your programming. So these five highlighted lines of code can be written in a slightly shorter form. Instead of specifying whether the LEDs turn on or off, or when they sleep, we can think about the function as a whole. This function is making the LED blink. The LED class includes a function called Blink, and it can be used to replace these five instructions. When we open a parentheses, we can see the four options we can give it.

Skip to 4 minutes and 31 secondsThe on_time, how long the LED turns on for; the off_time, how long it's off; n is the number of times the blink repeats; and background is whether this occurs behind the scenes or whether the programme waits for it before continuing. If we just use a closed parentheses, we are specifying the default options, which is to make it on for a second, off for a second, infinitely, and in the background. However, if we were to write this with some other parameters, such as 0.1 for the on_time and 0.2, say, for the off_time this would flash the LED on for 1/10 of a second, off for 2/10 of a second, infinitely.

Skip to 5 minutes and 19 secondsIf we were to include a third parameter-- and so we said 0.2 for the on_time, 0.1 for the off_time, and then 5, this would flash the LED on and off 5 times. Within GPIO 0, there are a number of other really useful functions that make working with LEDs and other components much easier. We're going to briefly explore a few of those now.

Skip to 5 minutes and 46 secondsSo firstly, there's a built in function called Toggle. If I type red_led.toggle, what this will do is, it will turn the LED on if it's already off, and off if it's already on, inverting the state of the LED. Now, this may not seem revolutionary, but what it allows you to do is not have to care about what the state of the LED is, just toggle it. If I then have it just sleeping, I'll have 1 second, I could then also ask Python what the state of the LED was. So I can print out a message, and I can get that message by saying red_led.is_lit.

Skip to 6 minutes and 31 secondsAnd if I were to run that programme, it would tell me whether the LED was lit or not. I'm going to copy those three lines and paste them below, and if I run my programme, what I would see is my LED will flash on and then flash off, and it will tell me that the LED was true and that it was false. If I were to take those six lines and wrap them up inside a loop, I would have my blinking programme, albeit in a longer form. This point is really important. There are many ways to achieve the same goal, and none of them are correct or incorrect. They are just different. Are your LEDs blinking?

Blinky blinky lights

  1. Just like last week, we’re going to use the IDE - IDLE, to write and run the code. Open IDLE by clicking on Menu > Programming > Python 3 (IDLE) on your Raspberry Pi.
  2. Again, just like last week, you’re going to need an empty file to start writing your code. Click on File > New File to open a new window. Save this straight away (File > Save As..) and call it blink.py.
  3. Last week you used the time module to help write a simple program, and you imported the sleep function. This week you’re going to use a module called gpiozero. This module gives you access to the GPIO pins on the Raspberry Pi. You don’t need to use the whole of gpiozero in your program, but only a small unit of code called a class from the module that helps you control LEDs. At the top of your file write:

    from gpiozero import LED
    
  4. The next step is to create a new object for the specific component and pin we are using. Here we’ve called the object red_led, but the name you give it doesn’t really matter. This object can then receive different commands to control it’s behaviour.

    red_led = LED(17)
    
  5. Save and run your program. You can do this by clicking on File > Save and then Run > Run Module, or by using the shortcut keys - ctrl + s and then F5.
  6. Nothing will happen right away. Click into the shell (the first window that opened in IDLE) and you can now pass some different commands to your red_led object. Try the following two commands.

    >>> red_led.on()
    >>> red_led.off()
    
  7. Your led should turn on and off each time your type the lines. Let’s try and automate that a little. Back in the blink.py file you can import the time module, and specifically the sleep function. Alter you code so it looks like this:

    from gpiozero import LED
    from time import sleep
    
    red_led = LED(17)
    
  8. Now you can instruct your program to repeatedly turn the LED on and off again, by using an infinite loop.

    while True:
      red_led.on()
      sleep(1)
      red_led.off()
      sleep(1)
    
  9. Save and run your program, and see what happens. What does changing the amount of time passed to the sleep function do? What’s the smallest sleep time you can use?

A little bit of abstraction

The gpiozero module has some built in methods to give you even more control of your LED. These are abstractions. Complexity has been hidden away, making your code easier to write and more readable.

Either remove your while True: loop or copy and past your code into a new file so that it looks like this:

from gpiozero import LED
from time import sleep

red_led = LED(17)

Now you can try adding the following function calls to your program. Try them one at a time and play with the numbers to see what they do.

red_led.blink(0.1, 0.2)
red_led.blink(0.2, 0.1, 5)
red_led.toggle()
sleep(1)
print(red_led.is_lit)
red_led.toggle()
sleep(1)
print(red_led.is_lit)

Share you successes or failures with others using the comments section below or alternatively via a tweet to the wider community on #picademy

Share this video:

This video is from the free online course:

Teaching Physical Computing with Raspberry Pi and Python

Raspberry Pi Foundation