Skip main navigation

Project: Blinky lights

In this step, you are going to write some Python code so that your Raspberry Pi can act as a switch that can turn the LED on and off.
2.6
How can you control an LED connected to your Raspberry Pi using Python code? First, you need to rewire your circuit from the previous step, moving the wire from the 3-volt pin to pin 17. At the moment, your LED is off because pin 17 is in an off, or low, state. To turn on the LED, you’ll need to set up pin 17 to be on, or high. When set to high, the pin will provide 3.3 volts to the circuit. Now create a new file in Mu and save it as blink.py.
45.1
Next, let’s start writing our program. So we’re going to import the library we need, which is called gpiozero, and then we’re going to import the class that we need, which in this case is the LED.
61.7
I’m going to create an object for my LED, so I’m going to type red_led = LED, and then inside parentheses I’m going to put the number of the pin that the LED is connected to, which in this case is 17. I need a line of code to turn it on. So I’m going to type red_led.on().
87
So you’re going to save your program, click Run, and you should see your LED light up. You can also use the Python shell to turn the LED off. So in the shell at the bottom, I’m going to type red_led.off() and it should turn off. You have just completed your first piece of physical computing. Your program turned the LED on, and then you issued another command to turn it off, turning your Raspberry Pi into a switch. Now let’s combine these two commands into a single program to make your LED blink. So I’m going to type red_led.off().
131.4
What do you think will happen to the LED when you click Run? You might see a small flash, but more likely, the LED doesn’t turn on at all. Why is that? Computers can process thousands of instructions per second, so the gap between your on and off instructions is measured in microseconds. This is why the LED appears to stay off to the human eye. It doesn’t have enough time to turn on before the second instruction is executed. To overcome this, you need to introduce a pause into your program. Let’s import the library for time. So we’re going to type from time import sleep. And then in between our commands to turn the LED on and off, we’re going to type sleep(1).
179
And I’m going to run the code, see what happens. You can see the LED comes on, and then after a second, it turns off. You can also add a while loop and extra sleep command to make it blink on and off continuously. So I’m going to type while True– we need to remember to have a capital T for our loop– and then all of this code needs to be inside the loop. And so how we show this in Python is we make sure it’s indented.
209.3
And I’m going to run this code to show you what it does. And you can see that the LED comes on. Now, it looks like it’s on all of the time to our eye. But remember that this is in a loop, so at the moment, what’s happening is it’s coming on for a second and then turning off. And because we haven’t added another sleep. It’s going straight back to turning it on again, so it looks like to us it’s constantly on. So if I just stop this code and add a sleep(1) after my off, and we run that, we should see it turn off and off for a second.
245.7
What happens if you change the number inside of the brackets of the sleep function? Can you edit your program and add more instructions so that the LED blinks the Morse code for the words “hello world”? Have a go and share a link to your code in the comments section.

In the last step, you set up a simple circuit to test your LED. Earlier, you heard that Raspberry Pi can be used as both a battery and a switch, and you saw the former when you connected the LED to the 3V3 pin on your Raspberry Pi. In this step, you are going to write some Python code so that your Raspberry Pi can act as a switch that can turn the LED on and off.

Rewiring your circuit

The first thing to do is to remove the jumper cable from the 3V3 pin. This pin always supplies 3.3 volts, but there are plenty of GPIO pins on Raspberry Pi that can be controlled. For this exercise, I’m going to use pin 17, but you can choose any numbered pin.

  1. Connect the female-to-male jumper cable to your chosen pin as shown in the diagram.

Breadboard diagram showing the long leg of an LED connected to pin GPIO 17. As before, the circuit is completed with a resistor connected to the short leg of the LED, and a GND pin.

Your LED will most likely have turned off. This is because at the moment, pin 17 is in an off or low state. To turn on the LED, we’ll need to set pin 17 to be on or high. When the pin is in a high state, it will supply 3.3 volts.

Using Python as a switch

  1. Inside Mu, click on + (New) in the top bar to open a new window. Save this straight away (Ctrl + S or click Save). Call it blink.py.

  2. You’re going to use the module gpiozero. You don’t need to use the whole of the gpiozero library, just the bit that allows you to control LEDs. Inside your blink.py program, write:

     from gpiozero import LED
  3. The next step is to create a new object for the specific component and pin we are using, and store it in a variable so that you can use it later on. Add the following to your program:


    red_led = LED(17)

    red_led.on()

An object is a collection of functions, and this object will allow you to control the LED. Here, I’ve called the object red_led, but you can call it anything you like.

  1. Save and run your program by clicking on Save and then Run.

  2. You should see your LED light up when you run your program.

  3. The bottom part of your Mu window should now contain a Python shell and you should see three arrows like this >>>.

  4. Click into this window and type the following:

     >>> red_led.off()
  5. Then hit Enter on the keyboard.

You have just completed your first piece of physical computing! Your program turned the LED on and then you issued another command to turn it off, turning your Raspberry Pi into a switch. Next, you will combine these two commands into a single program to make your LED blink.

  1. Add the off() command to your blink.py program:

     from gpiozero import LED

    red_led = LED(17)

    red_led.on()

    red_led.off()

  2. Run your program by clicking Run.

What happens?

You might see a small flash, but more likely, the LED does not turn on at all.

Before you continue, consider the following:

Why doesn’t the LED turn on?

To answer this question, you have to consider the processing speed of your Raspberry Pi computer.

Computers can process basic instructions, like the ones you just wrote, at a rate of thousands of instructions per second. This means that the gap between your on and off instructions is measured in microseconds (thousandths of a second). This is why the LED appears to stay off; it doesn’t have enough time to turn on before the second instruction is executed.

To overcome this, we need to introduce a pause into our program. To do this, you are going to need to use another module called time and one of its functions named sleep.

  1. Add another import statement to your program so you can use sleep:

     from gpiozero import LED
    from time import sleep
  2. Add a call to the sleep function between your LED instructions.

     red_led.on()
    sleep(1)
    red_led.off()
  3. Run your program by clicking Run.

  4. Your LED should now blink!

  5. You can change your program to place the code that turns the LED on and off in a while loop:

     from gpiozero import LED
    from time import sleep

    red_led = LED(17)

    while True:
    red_led.on()
    sleep(1)
    red_led.off()
    sleep(1)

Notice that I have added an extra sleep to the program, to give a pause between turning off and turning on again.

  1. What happens if you change the number inside the brackets of the sleep calls?

Challenge time

Edit the code and add more instructions so that the LED blinks the Morse code for one of the following:

  • SOS
  • Hello World

Share your code with your fellow learners by uploading it to a file-sharing service such as Pastebin and posting a link to it in the comments section.

This article is from the free online

Teaching Physical Computing with Raspberry Pi and Python

Created by
FutureLearn - Learning For Life

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now