Getting Started with Raspberry Pi: LED blinking on Raspberry Pi Zero


LEDs are probably the best thing that ever happened in electronics, they go well with absolutely everything (like raisins, pretty much) and as it turns out, they’re pretty simple to work with. That’s why they’re just perfect for any beginner tutorial on electronics.

This tutorial will show you how to control an LED from a Raspberry Pi Zero. We’ll be using Python to control the LED – it is definitely possible to use a variety of programming languages for that, but Python is probably the easier since you have built-in libraries on Raspbian to interact with the pins of your Raspberry Pi via Python code (no need to install any extras).

If you are looking for a little introduction on electronics before getting started with actual practice, have a look at my Noob’s crash-course on electronics.

What you are going to need for this tutorial

  • Breadboard
  • A Raspberry Pi Zero with a soldered header. It must be running Raspbian Jesse. You should be able to log on either by connecting a display and keyboard, or by logging in via SSH if your RPZ is connected to the network. If you’re new to all this, check out our Raspberry Pi Zero detailed setup guide.
  • LED (any regular LED will do)
  • 220 ohm resistor
  • Breadboard jumper wires

Understanding the Components


A Breadboard is used to prototype new projects without the need for soldering. You just need to plug / connect the components to create your circuit. Pretty much like a “development environment” you might be used to, it’s ideal to start and develop a project. It’s also a great way  for just playing around with no strings attached.

Learn More


LED comes from  “light-emitting diode”. LEDs have two legs, one positve (anode) which is usually the longer one, and a negative (cathode) which is usually the shorter leg. You will normally connect the positive leg of an LED to a resistor (around 220 ohm or higher up to 1k) and the negative leg to GND.

Learn More


Resistors are a fundamental electronic component. They are used to limit current flowing through a circuit. Resistors can have many different pre-defined values, measured in ohms. The bigger the value, the bigger the resistance, which means less electrons passing through.

Learn More

Jumper Wires

Jumper wires are simple reusable wires that can be easily connected to the breadboard and components for prototyping. 

Learn More

Getting Started

The Raspberry Pi Zero is a tiny computer that you can use to create a wide range of little projects, with or without network access. The most interesting thing about the Raspberrys is that they run a Linux distribution (Raspbian) based on Debian, so it’s very straightforward to configure and work with external devices, APIs and whatnot. You have the freedom to choose any programming language that can run on Linux and this gives the Raspberrys a lot of flexibility in terms of usage.

It’s important to point out that usually when you buy the Raspberry Pi Zero or a kit containing this board, it won’t come with a soldered pin header. You’ll need to solder a pin header before being able to connect components and controlling them via software.

The Board Pinout

It’s always interesting to have at hand the board pinout. It will show you details about the board, the available pins and other information. You can find pinouts for all the most popular boards easily with a Google search – you can even go to “images” directly to get what you need quickly.

This is the Raspberry Pi Zero / Zero W pinout:

Making Things Happen

First example: LED ON

Before doing anything more complex, let’s start creating a circuit that will simply turn the LED ON when the board is ON. Yes, that’s right – our first circuit!

Circuits are, basically, paths in which electricity can run.

You can think of a circuit as a program. It has a very specific flow of execution (electricity) that can be altered by controls: function or method calls, conditionals and such. In a circuit, such controls are implemented in “bare metal” through the use of components like LEDs, resistors, capacitors, buttons, etc.

This doesn’t require any I/O operation, we will simply connect the LED to one of the 5V pins of the board (so it will be its power source) and to a GND pin also in the board.

To connect the LED, we’ll need a 220 ohm resistor (values up to 1k can still light up the led).

This is how such circuit looks like – I’m using female-male jumper wires to connect the Raspberry Pi Zero to the breadboard:

We are only using one of the 5V pins and one of the GND pins of the Raspberry. These are connected to the breadboard power rails on the right. The LED anode (positive leg, most usually the longer leg of the LED) is connected to the 220 ohm resistor, which is then connected to the power rail (5V). The negative leg of the LED (cathode) is connected directly to the GND rail. If you don’t quite understand how they are connected, have a look in our Whatsit page explaining how breadboards work.

As soon as you turn the board on, the LED should light.

Now, what if you want to control how this LED turns on and off? Finally… let’s put some code in it!

Second Example: playing with the LED

With just a tiny change to our current circuit, and a few lines of code, we can make this beauty blink! (insert glitter emoji)

Connect a display and keyboard to log on your Raspberry, or log in via SSH. We’ll be controlling the LED in real-time from the Python console. YAY! That’s what I call fun.

The first thing we’ll do is change the circuit. Now, instead of connecting the positive leg of the LED (anode) to the 5V pin directly, we’re gonna connect this leg to an IO pin that we can control via code. Our code will decide when to send 5V to the pin and light up the LED. We’ll be using pin 19, just because it’s very easy to find it. Look at the board pinout and check your own board – you’ll find it easier this way. It should be the third pin, bottom to top, on the left column. Here’s the photo of how my circuit looks like:

Notice that the red wire is only there as a “formality” because we’re not using the 5V input anymore.

Now, open the Python console. If you have Internet on your Raspberry, I highly recommend installing ipython, which is an interactive Python console so you can “tab” your way into things. To open the console, just run python or ipython .

From the console, import the GPIO library:

import RPi.GPIO as GPIO

The RPi.GPIO library is used to interact with the pins on your Raspberry.

Now we’re gonna set the PIN_MODE to BCM and specify which PIN we’re using, in which mode – output for us. We’re using pin number 19.


Next, let’s make this LED high!


If your circuit is correct, the LED should be ON right now. To turn it off, run:


Yes, simple as that… Isn’t it fun?

Third example: Blinking the LED

Now we’re going to create a little Python script to blink the LED. Create a file named blink.py on your home directory, inside the Raspberry Pi Zero. Edit this file with the following contents:


import RPi.GPIO as GPIO
import time


while True:
  print "LED on"
  print "LED off"

After saving, make it executable with:

$ chmod +x blink.py

And execute it, by running:

$ ./blink.py

If everything is right (and your LED is connected to pin 19) you should see the LED start to blink, followed by an output message on the console. Mission accomplished \,,/ Now can you imagine all the things you can build with a little Python code, internet and LEDs?!? <insert heart-eyes-emoji>

Bonus: PHP code

The same blinking implementation can be accomplished by this PHP code using the library piphp/gpio:


require 'vendor/autoload.php';

use PiPHP\GPIO\Pin\PinInterface;

$gpio = new GPIO();
$pin = $gpio->getOutputPin(19);

while (true) {

Check the library’s Github page for installation and usage instructions.

Next time, we’re gonna play with buttons.

Leave a Reply

Your email address will not be published. Required fields are marked *