Hello World for the Raspberry Pi - Making an LED Blink

I unboxed my Raspberry Pi a few weeks ago and started learning Python. Let's code the "Hello World" of the Pi, and make an LED blink.

Hello World for the Raspberry Pi - Making an LED Blink

I finally unboxed my Pi a few weeks ago, and since then I’ve been learning some Python - the primary language of the Pi.

You can do fun things with it out-of-the-box, like running and modifying the Python games that install with Raspbian (as well as writing your own), or playing around with MIT’s Scratch program (which also comes preinstalled). Or you could try another OS, like the OpenElec media platform that turns your Pi into a photo gallery / movie streamer (something a few of us were playing around with at the a recent user group).

But a whole new world opens up when you start experimenting with the GPIO (General Purpose Input/Output) pins. By turning them on or off, and sending (or receiving) signals through them, you can interact with other devices and react to the surrounding environment.

A Brave New World (GPIO and Sensors)

One way to use the GPIO pins is with a piece of hardware called a HAT, which sits on top of the Pi and plugs directly into the pins, adding a new set of functionality. For example, the Sense HAT provides multiple sensors and a grid of RGB LEDs (they can be set to any color), and is even being used on the ISS by ESA astronaut Tim Peake as part the Astro Pi competition.

A second option is to buy a kit with random peripherals that you can connect to, and signal through, the GPIO pins. Kits vary, but generally include a breadboard, LEDs and resistors at a minimum. Some offer switches, small motors and fans, sensors, and more.

If you're just starting out, look for one of the CanaKit or Vilros "complete starter" kits on Amazon or similar. They'll run about $75 - $100, but I've had good luck with them, and they're highly rated, and the kit should come with some extras that help you get started playing around quickly. Go for the Pi 4 unless cost is an issue and you want to save a little money.

The main difference is that the HAT provides an out-of-the-box set of features, whereas the kit takes longer to setup but allows you to customize the configuration. And you don’t have to choose one or the other, as the HAT leaves the GPIO pins accessible.

The kit above cost me about $20. There are less expensive options like the CamJam EduKit that runs about $7, which is good for a tight budget, but the kit I got comes with more of everything, as well as a cobbler and ribbon cable.

The Cobbler

Sounds like the villain in a Batman movie…

The cobbler is the black T-shaped device above, sitting on the red padding material. They can come in different shapes and sizes, like this smaller one for the original Pi. The images below, from the CanaKit product page, show it in more detail. It plugs into the breadboard, bridging all 40 pins from your Pi to the breadboard.


Cobbler and ribbon, connected to the Pi

Without a cobbler, as with the EduKit set, you’ll have to connect individual wires from the GPIO pins on the Pi to your breadboard. That’s not difficult, and would allow for more flexibility depending on what you’re trying to do, but it could be tedious and an unnecessary step. The cobbler eliminates wires that might get tangled up, and the one I got is clearly labeled, so you can tell at a glance which GPIO pin is mapped where on the breadboard.

Anyway, I got my kit quickly (thank you Amazon Prime!) and pulled it out this weekend to try the “Hello World” of Pi hardware…

Blinking an LED (Hello World)

Making an LED blink is simple once you know how, and it’s about the smallest project you can complete that makes your Pi do something in the outside world.

If you don’t have one of those cobbler boards, then you can just follow along with the The PiHut tutorial titled “Turning on an LED with your Raspberry Pi’s GPIO Pins“. It’s short, has good images demonstrating where everything goes, and even includes a nice explanation of what each line of the short Python program is doing at the end. Read through it once or twice and go for it!

In my case, there was no need to make the connections from the Pi to the breadboard, seen as black and orange wires in the tutorial. The cobbler takes care of that. But I did stare at the diagram for a few minutes, trying to envision how to modify it without accidentally frying something.

Breadboard Setup

Unlike the tutorial, I used pin 21 for the longer (anode) side of the LED (again, read the tutorial first), then connected the shorter (cathode) side to an empty row. This allowed me to add a resistor to the same row as the cathode side, and then complete the circuit by connecting to one of the “ground” terminals. Hopefully that makes more sense once you take a look at the image below, showing my setup.

Also, if you’re wondering why to even bother with the resistor, I was too. From what little I understand, once the LED lights up it offers no resistance at all, so you’ve basically got a short-circuit. Things may work for awhile, but you could drastically shorten the lives of both the LED and the Pi. From the aforementioned tutorial:

You must ALWAYS use resistors to connect LEDs up to the GPIO pins of the Raspberry Pi. The Raspberry Pi can only supply a small current (about 60mA). The LEDs will want to draw more, and if allowed to they will burn out the Raspberry Pi. Therefore putting the resistors in the circuit will ensure that only this small current will flow and the Pi will not be damaged. Resistors are a way of limiting the amount of electricity going through a circuit; specifically, they limit the amount of ‘current’ that is allowed to flow.

Ultimately, here’s how I laid it out. The resistor I used was only 220 Ω (the tutorial recommends 330 Ω), but it worked okay.

breadboard single led circuit

Python Script

After you create the circuit, you still need to signal the GPIO pin, in order to turn on the LED.

I wrote a short program (modified from the tutorial), which loops 10 times. Each time, it signals pin 21 once to turn the LED on for a quarter-second, then again to turn it off for a quarter-second. The effect is a light that blinks twice a second for 5 seconds.

import RPi.GPIO as GPIO
import time
 
pin = 21         # The pin connected to the LED
iterations = 10  # The number of times to blink
interval = .25   # The length of time to blink on or off
 
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(pin, GPIO.OUT)
 
# The parameters to "range" are inclusive and exclusive, respectively,
#  so to go from 1 to 10 we have to use 1 and 11 (add 1 to the max)
for x in range(1, iterations+1):
 
    print "Loop %d: LED on" % (x)
    GPIO.output(pin, GPIO.HIGH)
    time.sleep(interval)
 
    print "Loop %d: LED off" % (x)
    GPIO.output(pin, GPIO.LOW)
    time.sleep(interval)

Console Output

Here’s the output that prints to the console while the LED blinks. That last word, “scrot”, is the command for taking a full-screen capture with the Scrot app in Raspbian.

blinking led output

It’s Aliiiiive!

Feeling invulnerable, I got crazy and tried two LEDs. Slow down, right? 😏

I just wanted to see it operate both, and it did, albeit the green LED seemed a bit dimmer. When I tried to use blue with red or green, the blue didn’t light up. I assume that’s because it uses more power than the resistor was allowing through.

If I’m wrong about that assumption, I’d be interested in hearing the real reason. I assumed all the LEDs were identical other than color, but the blue is brighter than the red or green. And there’s a white LED with four wires that I haven’t tried it yet, but I’d bet it’s brighter than blue.

Where can I purchase the hardware?

If you want any of the accessory hardware you saw here, including the T-shaped cobbler and cable (it’s very handy to not have to wire up all the individual GPIO pins!), you can pick up a kit on Amazon - I've found Canakit and Vilros to be good brands, but I'm sure there's others too.

Where can I go from here?

Try out these easy projects, to get more familiar with the Raspberry Pi:

What else did I learn this week?

More (completely random) things I learned this week.

Taking Screen Captures in Raspbian

If you want to be able to take screen shots on the Pi, you need to install an app called “scrot”:

sudo apt-get install scrot

Then just run “scrot” from the command line and it’ll take a full-screen capture and place it in the current working directory, wherever you happen to be in the command window when you call the command.

The GPIO Library (aka, don’t reinvent the wheel)

There’s a Python library called Rpi.GPIO that makes it easier to access and manipulate the GPIO pins. Here’s a few useful commands:

import Rpi.GPIO           # You have to import the library to use the commands in it
 
GPIO.setmode(GPIO.BOARD)  # Recommended! "Board" numbering mode, consistent between models
GPIO.setmode(GPIO.BCM)    # "Broadcom" numbering mode, could change between models of Pi
 
GPIO.setup(13, GPIO.OUT)  # Set the pin direction. In this case, sending signals out.
GPIO.output(13, True)     # Turn the pin "on". Could use: True/False, 1/0, GPIO.HIGH/GPIO.LOW

Courses, Demos and Random Notes

When programming in Python, there are online Python editors. Personally, I just installed the free PyCharm IDE from JetBrains.

By using VNC (Virtual Network Computing) and following a tutorial on How to give your Raspberry Pi a Static IP Address, I’m now able to connect to the Pi from other machines. Maybe I’ll write something up on how I did that. It’s convenient to not have* *to plug it into the TV or use a spare mouse/keyboard with it unless I want to.