Skip main navigation

Blinky blinky lights

In this step, having connected your LED to a GPIO pin, you'll control it with some Python code making it flash!
Last 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 gpiozero, which makes controlling physical hardware nice and easy.
From gpiozero, we want to import a class called LED. And a class is just a description of how an LED should behave.
And then the second line we want to write is, we want to tell it where that LED is and give it a name.
I’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.
And 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.
The same is true if we type 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.
A 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().
This 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.
And 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.
It 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.
The 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.
If 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 gpiozero 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.
So 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.
And 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?
  1. Just like last week, we’re going to use the ‘development environment’ IDLE, to write and run Python code. Open IDLE by clicking on Menu > Programming > Python 3 (IDLE) on your Raspberry Pi.
  2. As before, 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
  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 the gpiozero library, just the bit that allows you to control LEDs. Inside the library are loads of blueprints for objects you can control; these blueprints are called classes. You want to import the LED blueprint from the gpiozero library. 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 its behaviour.

    red_led = LED(17)

    (This assumes you have connected the LED to pin 17 on your Raspberry Pi.)

  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()
  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, 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:
  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 additional 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 code snippets to your program. Try them one at a time and play with the numbers to see what they do.

Snippet 1

red_led.blink(0.1, 0.2)

Snippet 2

red_led.blink(0.2, 0.1, 5)

Snippet 3


How did you get on? Share you successes or failures:

  • Using the comments section below
  • via a tweet to the wider community on #RPiLearn
  • by uploading a video to our Flipgrid
This article is from the free online

Teaching Physical Computing with Raspberry Pi and Python

Created by
FutureLearn - Learning For Life

Our purpose is to transform access to education.

We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.

We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.

Learn more about how FutureLearn is transforming access to education