Raspberry Pi Knight Rider LEDs Part 1

This is the first part of a two parter on creating the K.I.T.T. light bar with LEDs using your Pi (I know, awesome, right?).

Here’s mine in action:

In this section we’ll install the wiringPi library on the Raspberry Pi, connect up a single LED, and write a simple program in C to make it flash.

In part two we’ll connect up more LEDs, expand the code, and then travel back to the 80s with our finished product.

There’s lots of info on using Python on the Pi, but you know I’m from an old-school embedded background, so we’ll be doing all this in C instead.

If you’ve never done any electronics or embedded work before, this can seem like a daunting task, but it really isn’t.

This guide is for complete beginners, so I’ll be talking through all the parts and the entire process step by step.

Ready? Let’s go.

Step 1 – Acquire The Hardware

This is the list of items you’ll need. If you want clarification on any of these (buying electrical components is a bit of a leap in the dark the first time you do it), have a look at the pictures and further info below. If you’re pretty familiar with these things, then grab the list, skip the details and move onto Step 2.

  • A Raspberry Pi and 3.3V adapter for power
  • A breadboard
  • Eight 330 ohm resistors (we will only use one in Part 1)
  • Eight 5mm red LEDs (again, we will only use one to start with)
  • Nine male/female jumper wires (I used a selection of different colours which makes things easier to ID)

I shouldn’t need to tell you any more about the Pi and power adapter – I’m assuming you’ve either got one already or are thinking about buying one. Onto the components:

Breadboard

A breadboard is a way to create electrical circuits without having to use a soldering iron. Very handy. You can get them in shops (like Maplin in the UK) that sell electrical components, or buy them online. They come in various sizes. I’m using a “full size” board, with two power buses (the outer columns), 63 rows, and 10 inner columns. You can use a half size board – as long as it has at least 31 rows you won’t need to change the wiring in this tutorial. My breadboard looks like this:

full size breadboard

330 Ohm Resistors

I’m going to give you a little background on resistors, because I use two different types in this tutorial for no other reason than that’s what I had available. However, it’s worth understanding the different types so when you go shopping for yours you understand what you are buying.

  1. Resistors come with coloured bands around their bodies that you use to calculate their resistance.
  2. The number of bands (3,4,5 or 6), indicates their tolerance (more bands is generally higher precision).

First of all, the bands. The colours represent numbers – this is just easier than printing teeny, tiny font on them. There are lots of online calculators and explanations for the colours, so I’ll leave that up to you to read about later.

Second, the number of bands – this is where it becomes a little more complex. I’m using resistors with 4 bands (orange, orange, brown, gold) and resistors with 5 bands (orange, orange, black, black, brown):

330_ohm_resistors

My 5 band resistor is calculated as follows:

orange – 3
orange – 3
black – 0
black – 1 (multiplier)
brown – 1 (tolerance)

330 x 1 = 330 ohms
1 = +/-1% tolerance

My 4 band resistor is calculated as follows:

orange – 3
orange – 3
brown – 10 (multiplier)
gold – 5 (tolerance)

33 x 10 = 330 ohms
5 = +/-5% tolerance

In essence all you need to do is ask for 330 ohm resistors, but the info above will help you if you are given blue ones or brown ones and you don’t want to feel like an idiot for asking what the difference is (like I did – I knew I should have paid more attention when I was little and my Dad was fixing the telly).

5mm Red LEDS

You can’t go far wrong with these. They come singly and in multi-packs (I bought a pack of 10), and although there are specifics such as light angle, colour, brightness, etc., for our purposes any old working LED will do, so don’t spend more than you need to:

red_5mm_led

Male/female jumper wires

Lots of connectors and components are termed male/female in electronics depending on whether they (to put it bluntly) stick in or out. We want male-female connectors because the Pi has GPIO pins that go into the female end, and the breadboard has holes that require the pins on the male end:

male_female_jumper_leads

There we go. Now you’ve got all your stuff together, put it to one side and let’s focus on getting the code ready before we do any connecting up.

Step 2 – Write Some Code

As I said in the intro, we’re going to use the wiringPi library to talk to the GPIO pins on the Raspberry Pi. It was written by Gordon Henderson and I have been so impressed with its ease of installation and use.

I’m going to assume at this point that you have a Raspberry Pi that is up and running on the network. If you haven’t, then the Raspberry Pi website is the best place to start, or you can look at my post on headless boot of the Pi, if you like a challenge ;-).

You don’t need a keyboard or monitor for this tutorial – if your Pi is on the network you can log in via ssh and do everything from a terminal.

To install git (code repository) and then wiringPi, type the following commands:

sudo apt-get install git
git clone git://git.drogon.net/wiringPi
cd wiringPi/
 ./build

You can check if it’s all working with the following:

gpio -v
gpio readall

You should get output on the version you are using and then a listing of all the pins with the readall command.

There are more detailed instructions on wiringPi install on the wiringPi website, plus alternatives if the git method doesn’t work for you.

Okay, good.

Let’s write some code and then we’ll go back to our hardware.

In an editor (nano is available by default), type the code below.

#include "wiringPi.h"

int main()
{
    //required before using GPIO calls
    wiringPiSetup();

    //set GPIO pin 0 (GPIO 17) to output mode i.e. send current
    pinMode(0, OUTPUT);
 
    while(1)
    {
        //send current to GPIO 17
        digitalWrite(0, HIGH);
        delay(500); //milliseconds
        //stop current to GPIO 17
        digitalWrite(0, LOW);
        delay(500);
    }

    return 0;
}

Compile the code with:

g++ knightrider.c -o kr -lwiringPi

Step 3 – Wire Up The LED!

Warning: From this point onwards it matters whether you have a revision 1 or 2 Raspberry Pi. Why? Because the GPIO pins are in a different order on the revision 2 boards. You can completely fry your Pi if you get the wiring wrong, so make sure you have a pin mapping to refer to (this one is good). I will point out differences (I use a revision 1 board), but your Pi is counting on YOU to wire it up correctly.

This is the wiring on the breadboard that you need to replicate:

singleled_bb

If you’ve never wired anything before, this can be a mysterious art. What we’re doing here is creating a circuit – a continuous path from our Pi, onto the board, through the LED, through the resistor, and back to the Pi.

In wiring terms, we connect the GPIO 17 to an inner row (row 57) on the breadboard (via the brown wire). The inner rows are connected by metal strips underneath the cover (but not the inner columns). When you plug something into a row, the circuit will continue to any other item that is also plugged into that row. In our case the circuit will find an LED.

The LED bridges to another row, 55, and the circuit continues into our resistor, which is also plugged into row 55.

The resistor bridges row 55 onto the negative bus (or ground). The buses on the outside of the board are connected in two columns – the opposite of the inner rows. So we now have a connection between our resistor and our white jumper lead. The white lead finally goes back to the ground GPIO pin on the Pi.

Points to bear in mind when wiring are:

  • LEDs only work in one direction. Ensure the longer leg (the positive, or anode) is on the same row as the wire coming from GPIO 17.
  • Resistors can go either way around.
  • The white wire goes to the GROUND pin on your Pi (check your pin reference)
  • The brown wire goes to GPIO pin 17 (check your pin reference)

 Note that for this first step, the wiring on the Pi is the same for revision 1 and 2 boards.

Step 4 – Run Your Code

There’s only one thing left to do. Take a deep breath and hope for the best.

Double check your connections, and when you’re ready, run the compiled code with:

sudo ./kr

You should see a blinking LED.

single_led_raspberry_pi

Note that when you CTRL-C out of the program, your LED may remain on – this will happen if you interrupt the program before the GPIO pin is told to stop the output. It will be reset when you run your code again, but you may want to avoid pulling wires in and out when the GPIO pin is active, just in case.

So there we go – part 1 complete. In part 2 we’ll wire up some more components and edit our program to create the K.I.T.T light bar.