The Raspberry Pi is more than just a tiny computer. It’s a powerful board that lets you do so many things with its GPIO pins. Here we show you how to make blinking LEDs with the Raspberry Pi.
What Makes the LEDs Blink?
When you look at the top part of your Raspberry Pi, you’ll find about 40 metal pins jutting out of the circuit board. If you have a Raspberry Zero, there is probably circular holes for soldering header pins. In either case, they are called GPIO pins (General Purpose Input / Output).
Each GPIO pin is made to have one of two modes at any given time: a HIGH and a LOW. For the Raspberry Pi’s pinout specifications, a pin charged at 3.3 V counts as a HIGH or a “logical 1,” while anything below around 2.5-ish V counts as a LOW or “logical 0.” A board that’s based on 3.3 V for high and low outputs is said to be on “3.3v logic.”
When you connect an LED between a pin on HIGH and a GND pin, you’re basically making a complete circuit. The LED should light up because of the passing electricity.
Sometimes your LEDs will pop in smoke when there’s too much current passing through it. To keep that from happening, you can add a resistor. It doesn’t matter whether it’s on the anode or cathode side – either side should decrease the current passing through.
GPIO and Other Pins
Let’s be clear here, as not all of those metal pins are considered GPIO. They are only GPIO if they can be programmed to have a high or low – hence the term “Input / Output.” For the Raspberry PI, there are also pins made for power (3.3v, 5v, and GND) and working with EEPROM (ID_SD and ID_SC).
This time, you won’t need to think about all those other pins except GND and one GPIO pin.
Programming the GPIO Pins
How do you tell each GPIO Pin what to do? At the most basic level, you’ll have to make commands in machine code. That’s going to be a bit too difficult for beginners.
For this particular project, we are using Python, as it’s easier to use.
What You Need
- Any Raspberry Pi model that’s not the Pico (preferably the Raspberry Pi 3 Model B+ like the one in this example, but anything works), installed with the Raspberry Pi OS.
- An HDMI monitor and cable
- Mouse and keyboard
- A phone charger (to power the Raspberry Pi)
- A small LED
- A 250Ω resistor (can be any value close to this)
- A solderless breadboard
- x2 male-to-female jumper wires (or male-to-male if you have a header hat)
How to Make Blinking LEDs
Let’s take this one step at a time and make one LED blink on its own.
- Open your terminal and type
sudo apt-get install python3-rpi.gpioto install the RPi.GPIO module for Python 3.
- Open a text editor and type this code:
import RPI.GPIO as GPIO from time import sleep GPIO.setwarnings(False) GPIO.setmode(GPIO.BOARD) GPIO.setup(7, GPIO.OUT, initial=GPIO.LOW) while True: GPIO.output(7, GPIO.HIGH) print("LED on") sleep(1) GPIO.output(7, GPIO.LOW) print("LED off") sleep(1)
- Save it in a folder somewhere. The extension name should be .py. ON my Raspberry Pi, I named it “rpi-blink.py” to make it easier to find.
- Shut down your Raspberry Pi and remove it from all power sources.
- To start building the circuit, wire your LED so that there’s a resistor on either the cathode or anode, then point the cathode side to pin 7 and the anode side to pin 9 (GND).
Tip: to figure out the pin number, hold your Raspberry Pi in a way that the GPIO pins sit to the right. The top-left pin is pin 1, top-right is pin 2. The one below pin 1 is pin 3, then to the right of it is pin 4, and so on.
- Power it up again so that we can run the Python script. Open your Terminal and use
cdto move to the Python file’s folder. Type:
to make the LED blink.
Alternative: if you have Thonny Python IDE, then click on the “Run current script” button to make it run straight from the IDE.
- To make it stop, press Ctrl + C within the terminal. But if you’re using Thonny Python IDE, then just close the editor.
How the Code Works
There are two things that make it work: the code and the circuit. We are starting with the code and cutting it into three parts:
- Import commands
- Setup commands
- Looped commands
In practice, it’s good to think of code as small functions grouped together to make bigger functions.
Python normally doesn’t make it this easy to program GPIO pins. There’s a ton of stuff going on behind the scenes. The good news is that you can import the code that handles all these pesky things.
Take a look at lines 1 and 2:
import RPI.GPIO as GPIO from time import sleep
These are a pair of lines that import code from something called a “Python module.”
import RPI.GPIO as GPIO lets you import the contents of the RPI.GPIO module and lets you use the
GPIO keyword to call a function related to
On the other hand,
from time import sleep lets you import the
sleep() function from Python’s built-in time module. This lets you delay the next line of code for a given number of seconds.
Some code has to be “set up” or defined in such a way because it’s used by other code to do complex logic. We’ll call these setup commands.
Unlike import commands, setup commands do not “import” code from external modules. They import them from the modules you’ve already imported.
As an example,
GPIO.setwarnings(False) imports the
.setwarnings() function from the
RPI.GPIO module, which was previously defined as
GPIO. This function stops a trigger warning when you run the code. It’s set to
True by default.
To explain the other two, we are continuing with
GPIO.setmode(GPIO.BOARD). That tells
RPI.GPIO what kind of pinout you’re going to use. There are two types: BOARD and BCM. The BOARD pinout lets you pick pins based on their numbers. Meanwhile, the BCM pinout bases it on their Broadcomm SOC Channel designation. To keep things short, BOARD is easier to use because it’s always the same no matter which Raspberry Pi model you use. BCM, on the other hand, tends to be different from model to model.
Lastly, we have
GPIO.setup(7, GPIO.OUT, initial=GPIO.LOW)that uses the
.setup() function, which asks you three things: the pin number, its designation, and its initial value. The pin number we’re using here is pin number 7. We’re supposed to set it into an output pin and make sure that it starts out as
LOW. Without this, the Raspberry Pi will never know what to do with pin 7.
This one’s the cool part. Looped commands let you tell the Raspberry Pi to do things. We started this loop with
while True:, which loops the next lines of code around forever.
There were three functions in the loop:
GPIO.output()takes an output pin and sets it to either
LOW. If you thought about changing which pin to use on your Raspberry Pi, then you should have changed
7with a pin number of your choice.
print()makes it print something out on the console. It takes in a string, number, or variable that contains the previous two.
sleep()pauses the whole program for a certain number of seconds. Use a number smaller than 1 to make it pause for less than a second.
Now that you know how the code works, let’s take a look at the circuit. The code makes a circuit by connecting pin 7 to GND. When pin 7 is on
HIGH, it emits 3.3V that passes through the resistor and LED, then enters GND. This becomes a complete circuit and is why the LED lights up.
But what happens when pin 7 is on low? The 3.3V goes down to around 0V. This way, no electricity passes through the LED, so it doesn’t light up. You can think of pin 7 as a sort of a switch, as it turns the circuit either on or off.
Let’s Make More LEDs Blink!
Now that you know what makes things work, let’s modify our code a little to make it run two LEDs.
For this, you’ll just need to add two more LEDs of any color and two more 250Ω resistors.
- Open your code editor again and paste this code:
import RPI.GPIO as GPIO from time import sleep GPIO.setwarnings(False) GPIO.setmode(GPIO.BOARD) GPIO.setup(7, GPIO.OUT, initial=GPIO.LOW) GPIO.setup(12, GPIO.OUT, initial=GPIO.HIGH) GPIO.setup(37, GPIO.OUT, initial=GPIO.LOW) while True: GPIO.output(7, GPIO.HIGH) GPIO.output(12, GPIO.LOW) print("LED on @ pin 7") print("LED off @ pin 12") sleep(1) GPIO.output(7, GPIO.LOW) GPIO.output(12, GPIO.HIGH) print("LED off @ pin 7") print("LED on @ pin 12") sleep(1) if x == 1: GPIO.output(37, GPIO.HIGH) print("LED on @ pin 37") x = 0 elif x == 0: GPIO.output(37, GPIO.LOW) print("LED off @ pin 37") x = 1 else: print("Logic Error") sleep(1)
- Save then shut down your Raspberry Pi.
- To wire up the circuit, for every LED you have, connect it to a resistor in series, then wire up the cathode side to the Raspberry Pi. There should be one for pin 7, another for pin 12, and the last one for pin 37. The anode side should be connected to GND. Each of these pins has a GND pin next to them. Those should be pins 9, 20, and 39.
Tip: if you run out of male-to-female jumper wire, you can stick a male-to-male to a female-to-female jumper wire to form a longer male-to-female jumper wire.
- Once you’re done, fire up the Raspberry Pi and repeat step 6 to launch the Python script.
Frequently Asked Questions
Why does my LED stay open when I stop the script?
When the Raspberry Pi reads the Python script, it reads the line before executing the command. You probably stopped it right after it read the line
GPIO.output(7, GPIO.HIGH), so it wasn’t able to bring the pin to LOW first. You can leave it as is, as it will return to LOW the next time you reboot. Alternatively, make another Python script that turns the pin to LOW as soon as it runs.
I placed my LED on the right pin, but it doesn't light up at all. Why?
There are two possible reasons: either your LED is broken or you put the cathode on the opposite side. Try turning the LED’s pins around first.
Is it safe to place the LED the opposite way?
Yes. You can place it opposite, and the current will just not pass through. It’s a special trait among diodes – even light emitting diodes – they allow electricity to pass through on one side and not the other.
Is it safe to reposition the LED while the Raspberry Pi is powered?
If it’s just turning the LED the opposite way, then it’s alright, and there’s no harm there. But if you are going to place the LED everywhere, there’s a chance that you might short the 5v pin to a GPIO pin. That’s going to break your board.
Our latest tutorials delivered straight to your inbox