Programming Blog

Jeremy Morgan

Mostly Coherent Ramblings of a Silicon Forest Software Developer

How to Blink an LED on a Raspberry Pi 2

By: Jeremy Morgan







Raspberry Pi Newsletter


The blinking LED is the “hello world” of the maker community, and today I’ll show you how easy it is to do with the Raspberry Pi 2 (or Model B)! We’re going to use Python and WiringPi for this project.

What you’ll need

For this article I’m using a Raspberry Pi 2, but you can also use a Raspberry Pi Model B. You will also need:

"Blink an LED Raspberry Pi 2"

  • A GPIO Adapter
  • Breadboard
  • Resistor
  • LED

What I am using for this project is highly recommended the Canakit for Raspberry Pi which contains everything in the list above. (This is an Amazon affiliate link).

Any decent breadboard or even a set of jumpers will work for this project.

How to Blink an LED with Python

The quickest way to get that LED to blink is to take a look at the pins of the GPIO and decide which one to tie to. Then you can use Python and the Raspberry Pi GPIO Library to create a script to light it up.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import RPi.GPIO as GPIO             ## Import GPIO Library
import time                         ## Import 'time' library (for 'sleep')

blue = 7                            ## These are our LEDs
ourdelay = 1                        ## Delay
# pins 4,17,18,21,22,23,24,25

GPIO.setmode(GPIO.BOARD)            ## Use BOARD pin numbering
GPIO.setup(pin, GPIO.OUT)        ## set output

## function to save code

def activateLED( pin, delay ):
  GPIO.output(pin, GPIO.HIGH)      ## set HIGH (LED ON)
  time.sleep(delay)                ## wait
  GPIO.output(pin, GPIO.LOW)       ## set LOW (LED OFF)
  return;

activateLED(blue,ourdelay)

GPIO.cleanup()                      ## close down library

As you can see in the code above, it doesn’t take much to get things working. But I’ll explain the code a little deeper.

1
2
import RPi.GPIO as GPIO
import time

The following code imports the Python GPIO library, and the time library. The GPIO library, as you probably guessed is the library for interacting with the GPIO in Python. It does an amazing job of simplifying the process. The time library is there so we can put a delay in, otherwise the blink might be too fast to notice.

1
2
blue = 7
ourdelay = 1

Here I created a variable named “blue” (the color of the LED) and assigned it “7” which is the pin number we want. If I wanted to add multiple LEDs I could name it something like:

1
2
3
blue = 7
red = 13
green 14

I then created a “delay” variable of one second. This way I can change the delay of the lights blinking however I want.

You can name the variables anything you want, but this was just to make it easy to see which LED is which if I wanted to do some fancy light show.

1
GPIO.setmode(GPIO.BOARD)

Then, we set the GPIO mode to “Board” which means we’ll use the numbering of the pin by board instead of GPIO. This makes it a little easier to understand when using a bread board.

With this line of code we set the pin to be an output:

1
GPIO.setup(pin, GPIO.OUT)

There are 2 main commands to turn the LED on then off:

1
2
GPIO.output(pin, GPIO.HIGH)
GPIO.output(pin, GPIO.LOW)

If you wanted to blink an LED twice you would have to repeat the last two lines each time. So I decided to put this in a function and put the pin and delay as parameters. This way making a particular LED blink is as simple as:

1
activateLED(blue,ourdelay)

This is repeatable and saves code when doing larger programs.

To close everything down, we need to run the following:

1
GPIO.cleanup()

It’s that easy! You could easily write a nice Python script that do some pretty cool stuff with just a few lines of code.

How to Blink an LED with WiringPi

For this step we’ll install WiringPi for the libraries to interact with the GPIO. This allows us to do what we just did, but from the command line. We’ll need to install WiringPi:

1
2
3
4
5
cd ~/sources
git clone git://git.drogon.net/wiringPi
cd wiringPi
git pull origin
./build

If successful you should see a screen like this:

"Blink an LED Raspberry Pi 2"

Now we can light up the LED from the command line. Remember the pin 7 in the example above? We can now light up like so:

1
2
gpio mode 7 out
gpio mode 7 1

This will light up the LED. You can turn it off by entering:

1
gpio mode 7 0

"Blink an LED Raspberry Pi 2"

This makes it super easy to light up LEDs from the command line. You could create a pretty neat BASH script to do this, and do some neat things, or call this from other languages.

Summary

I hope this has helped in showing how easy it is to blink an LED on the Raspberry Pi 2/B. Of course as you progress on you’ll want to do far more than just blink an LED, but the GPIO libraries make it very easy to create some neat stuff. If you’ve experimented with this and done something cool, leave me a comment!!

Special Thanks to the folks in this thread on Reddit who helped with some corrections to this article.


My New Course: Hands on Internet of Things with the Raspberry Pi

Internet of Things

I have just released my new course titled ”Hands on Internet of Things” which is intended to be a course for people who want to get started with IoT. I’ll take you through blinking lights and reading sensors to creating YOUR own IoT endpoint and dashboard. I’m offering this special coupon for readers of my site: get %50 off this course here. Thanks for being a reader!


Like my IoT projects? Sign up here to be added to my IoT newsletter

Comments