Project: Blinky lights
Share this post
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.
- Connect the female-to-male jumper cable to your chosen pin as shown in the diagram.
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
-
Inside Mu, click on
+
(New
) in the top bar to open a new window. Save this straight away (Ctrl + S
or clickSave
). Call itblink.py
. -
You’re going to use the module
gpiozero
. You don’t need to use the whole of thegpiozero
library, just the bit that allows you to control LEDs. Inside yourblink.py
program, write:from gpiozero import LED
-
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.
-
Save and run your program by clicking on
Save
and thenRun
. -
You should see your LED light up when you run your program.
-
The bottom part of your Mu window should now contain a
Python shell
and you should see three arrows like this>>>
. -
Click into this window and type the following:
>>> red_led.off()
-
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.
Make it blink
-
Add the
off()
command to yourblink.py
program:from gpiozero import LED
red_led = LED(17)
red_led.on()
red_led.off()
-
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
.
-
Add another import statement to your program so you can use
sleep
:from gpiozero import LED
from time import sleep -
Add a call to the
sleep
function between your LED instructions.red_led.on()
sleep(1)
red_led.off() -
Run your program by clicking
Run
. -
Your LED should now blink!
-
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.
- 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.
Share this post
Teaching Physical Computing with Raspberry Pi and Python

Teaching Physical Computing with Raspberry Pi and Python

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.
Register to receive updates
-
Create an account to receive our newsletter, course recommendations and promotions.
Register for free