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. Instead, for the Raspberry Pi, you can use Python or C++, which then gets compiled into machine code. 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 cableMouse and keyboardA phone charger (to power the Raspberry Pi)A small LEDA 250Ω resistor (can be any value close to this)A solderless breadboardx2 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. 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. 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.

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: In practice, it’s good to think of code as small functions grouped together to make bigger functions.

Import Commands

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: 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 RPI.GPIO. 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.

Setup Commands

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.

Looped Commands

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(), print(), and sleep().

GPIO.output() takes an output pin and sets it to either HIGH or LOW. If you thought about changing which pin to use on your Raspberry Pi, then you should have changed 7 with 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.

The Circuit

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.

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. 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.