Raspberry Pi GPIO Usage Tutorial: Start With LEDs


If you have a Raspberry Pi, and you like building robots, you probably wondering how to use the GPIO pins in your project. You have come to the right place. In this tutorial I will show you how to turn a LED on and off using the GPIO pins on the Raspberry Pi.

The proper way dealing with power electronics with Raspberry Pi

The safe approach is to use a proper buffered board that protects the Pi’s system on a chip, or use an Arduino connected to the Raspberry. However, you can also just use the GPIO pins directly.

BUT IF YOU DESTROY YOUR RASPBERRY PI DON’T BLAME ME. YOU HAVE BEEN WARNED! 😀

pi_led

Get Started!

 

The ‘Slice’ is a very low cost entirely passive breakout board for the Pi’s GPIO connector. There is a breakout board used, but it doesn’t really matter if you don’t have one.  If you prefer, you could just connect header sockets to the appropriate pins on the Pi board itself, but this avoids the risk of accidental shorting of pins.

Then use the RPi.GPIO library to control the LED from the Raspberry Pi.

slice_1

slice_3

Current Rating with the Raspberry Pi GPIO pins

The Pi’s GPIO pins are 3.3V and the max current that runs through it could be very large (>50mA) but it’s a good idea to keep this as low as possible. An LED will glow with just 1 or 2 mA flowing through it, which shouldn’t do the Pi any harm. By using a 1kΩ resistor, if the LED forward voltage is 2V, and the supply voltage is 3.3V, that leaves 1.3V across the resistor, using Ohms Law, I = V / R so I = 1.3mA.

Hardware Connection

I just twisted one leg of the resistor around the negative lead of the LED. The negative lead is the shorter lead.

led_and_res
Put the positive lead of the LED into the 3.3V power socket on the slice and the free end of the resistor into the socket on the Slice labelled GP0.

You can now fit the slice onto your Pi.

slice_on_pi

pi_led

Raspberry Pi Software Configuration

Now its time to sort out the software side of things.

Step 1. If you are not reading this page on your Pi, then switch now, so you can copy and paste.

Step 2. Browse to here and download RPi.GPIO-0.1.0.tar.gz and save it somewhere convenient. I saved it to the ‘other’ folder on the Desktop.

Step 3. This is a gziped tar file. Which needs unzipping and untaring. To unzip it open a Terminal, which you will find from the ‘start menu’ under ‘accessories’. Now paste the following commands into it.

cd /home/pi/Desktop/other
gunzip FPi.GPIO-0.1.0.tar.gz
tar - xvf FPi.GPIO-0.1.0.tar

Step 4. Install GPIO, by typing these lines in your terminal window:

cd FPi.GPIO-0.1.0
sudo python setup.py install

Step 5. Run Python 2. You will find this from the menu under Programming – Use Python 2 not 3.

We now need to enter some commands:

 >>> import RPi.GPIO as GPIO
 >>> GPIO.setup(11, GPIO.OUT)

After we enter this second command, the LED should light. That is because the pin will have been set to an output and initially be low. A low pin will light the LED, a high pin and it will turn off.

 >>> GPIO.output(11, True)

This should have turned the LED back off.

Let’s now write a little program to make the LED blink.

 >>> import time
 >>> while True :
 >>> GPIO.output(11, True)
 >>> time.sleep(1)
 >>> GPIO.output(11, False)
 >>> time.sleep(1)

Press the enter key twice. When you have had enough of the LED blinking press the key combination ctrl-C.

Here is a example video:

You may have noticed that the LED is connected to pin GPIO0 and not 11 as we are using in Python. The library uses different pin numbers to the names on the Pi. I’m not sure why. But anyway, I mapped a few of them as shown below.

library name Pi name
 11 0
 12 1
 13 2

After that, the mapping changes.

I wanted to know how fast the GPIO library was, so I took off the LED and attached my oscilloscope to the output and then ran the following test program.

 >>> state = True
 >>> while True :
     GPIO.output(11, state)
     state = not state

The ‘scope measured the frequency as 800Hz. Not very fast, but fast enough for PWM and servo control.

Share Button

Leave a Reply