Friday 1 November 2013

Class notes; How To Drive LEDs From A RaspberryPi

I was asked recently to help with a RaspberryPi training session for a group of young Computer Science pupils.


So this post is simply the hand-outs from this session.


How To Drive LEDs From A Raspberry Pi

By connecting a few components to the Raspberry Pi GPIO connector, it is easy to control LED (lights) via a programming language such as Python.

Basic Configuration

You will need:-
  • an L.E.D.
  • a 220 Ohm resistor
  • 2 jumper wires
  • Optional: 1 breadboard

Note: If you use a breadboard, the jumper wires should be male to female, but for direct connection without a breadboard, use female to female jumpers.

OK, the first step is to identify the Raspberry Pi GPIO connector;

Now connect the components following this simple circuit;

Let's write a program

Now write a Python program to flash the L.E.D. like this:-

#Import the time library
import time
#Import the Raspberry Pi library which controls the GPIO
import RPi.GPIO as GPIO
#Clear the current GPIO settings
#Set mode to use Raspberry Pi pins numbers
#Set connector pin 7 to be an output
#Create a LOOP that runs & runs (...until you force it to stop)
while true:

Now save your program as:
Open the file manager and navigate to your file.
Now press F4 to open a terminal window (this will open terminal in the correct folder).
In the window type:-
...your L.E.D. should now flash! (to stop your program, just type <ctrl> c)

So how does this work?...a short description

The program sets the condition of the Raspberry Pi interface (GPIO) so that output pin 7 is driven high for 1 second, then low for 0.5 second, then the sequence repeats.
When the output is high, a voltage is applied to the L.E.D. and it lights up.
When the output is low, there is no voltage on the L.E.D. so it does not light up.

A Long Description.....way too much information

L.E.D. stands for Light Emmitting Diode, which is a component belonging to the semi-conductor family. Think of a semi-conductor as a sensitive component that will only do something exciting if you treat it nice.
Treating an L.E.D. nice means connecting it the right way around (know as polarity), and just giving it the right amount of electrical current.
The resistor in our circuit (which is not a semi-conductor, and therefore is neither sensitive nor exciting) is the component that limits the current through the L.E.D. to a safe value. It protects both the L.E.D. and our Raspberry Pi, so that we don't draw to much current and blow up the L.E.D. and GPIO components on the Pi.
So, depending how you treat your L.E.D. the 4 possible outcomes can be summed up as follows:-

Polarity Current Outcome
Correct The right amount WOW! It lights up!
Incorrect The right amount Huh! It doesn't light up
Correct Too much Oh no! I've blown it up!
Incorrect Too much Oh no! I've blown it up!

So what's GPIO? In computer jargon, IO means inputs & outputs.
Inputs are used to connect components such as switches to your computer..
Outputs are used to connect to components such as lights and motors. So with IO and a clever program, you could make your Pi detect when a door is opened, and switch on a light.
GPIO stands for General Purpose Inputs & Outputs. In fact the individual pins on the Pi GPIO can be programmed to be either inputs or outputs. The program line:-
GPIO.setup(7,GPIO.OUT) basically saying “setup GPIO pin 7 as an output”.
The program line:-
import RPi.GPIO as GPIO importing a library of routines and giving it the reference (or name) GPIO. This allows us to use the clever stuff in this library by using the prefix GPIO. When we use the instruction:-
...we are using the library routine “output” to set pin 7 low (zero Volts), and when we use:-
...we are using the “output” routine to set pin 7 high (approximately 3.3Volts).
We also import the “time” library in our flash program, so that we can pause between switching the L.E.D. on and off. Try changing the sleep values and see what happens.

What about sudo?

You usually run your Raspberry Pi as a standard user, using the “pi” account. But if you try to run the flash program by simply typing:-
...the display will say something like this;
RuntimeError: No access to /dev/mem. Try running as root!
Some operations on Linux need to be carried out as the “super user” which is a special administration account, also known as “root”.
The program cannot be run as a standard user because (as the message indicates) standard users do not have access to the /dev/mem directory.
The solution is to use the Linux command “sudo” (this is like saying: “as super user, do this”).
On most Linux systems, you need to supply an administrator password to use sudo. But on the Pi things are kept simple, so a password is not necessary.

No comments:

Post a Comment