Download presentation
Presentation is loading. Please wait.
1
Lighting LEDs with a RASPBERRY PI
Presented by the School of Computing and the Center for Forensics, Information Technology, and Security
2
What is a Raspberry Pi? A Raspberry Pi is a computer that you can hold in your hand It contains almost everything a regular computer contains Memory USB Ports Network Port Processor Audio Out HDMI Out One thing it doesn’t contain is a hard drive. For this we use a micro SD card like you would use in a digital camera A Raspberry Pi can be purchased for $35
3
Building a CIRCUIT The LED has two wires.
The shorter wires needs to be connected to low voltage. The longer wire need to be connected to high voltage. However, in order to prevent our LED from burning out quickly, we use a resistor. We connect the longer wire on the LED to the resistor. We connect the other end of the resistor to a source that can supply high voltage. We will use the Raspberry Pi to supply that high voltage. In order to create this circuit, we will need something to make the connection so that we don’t have to hold the wires together.
4
The breadboard This board allows to connect wires together so that the connection doesn’t easily come apart. The principle behind this board is that certain columns and rows are connected to each other. In the two columns on each side that are labeled + and -, every point in that column is connected to every other point in that column. Note that there is a separator in the middle. This seperates the two sides of the board. In each row labeled 1, 2, 3, 4, etc. every point in that row is connected to every other point in the row. What this means is that any wire placed into any row will be connected to anything else placed in that row.
5
The led An LED is a Light Emitting Diode.
When appropriate current is applied, it produces light. The short wire is called the cathode. The long wire is called the anode.
6
Connecting the led to the board
Gently pull the wires slightly apart. What we are going to do is to place the cathode (short wire) into the column labeled –. At the same time we are going to place the anode ( long wire) into one of the rows. You want to select a row that you can easily reach with the anode. Once you place the wires push them firmly into the board.
7
Connecting the resistor to the led
Locate the row where you placed the anode (long wire). Place one end of the resistor (it does not matter which end of the resistor you use) into the same row as the anode. Make sure the resistor is firmly pushed in.
8
Connecting the other end of the resistor
Remember that there is no connection between rows across the divider. Take the other end of the resistor into any row on the other side of the divider that is convenient for you to reach. Make sure the resistor is firmly connected.
9
Connect a wire for low voltage
Take a M – F wire and connect it to the same column where you connected the cathode (short wire) of the LED. Make sure it is firmly connected.
10
Connect a wire for high voltage
Take another M-F wire and connect it to the same row where you placed the other end of the resistor. Make sure it is firmly connected.
11
THE pins of the pi On the Pi we have what are called GPIO pins. GPIO stands for General Purpose Input/Output. Some of the pins have functions like high voltage or low voltage and we can’t modify their functions. There are, however, pins we can change. The pin diagram on the right assumes you are holding the Pi so that the network connector is at the bottom. We need to connect one of our wires to low voltage. We can find that in the 3rd pin from the top on the right column. For the high voltage we need a pin whose value can be changed. The one we will choose is the 4th pin from the top on the left. So connect the wire that you connected for low voltage to the 3rd pin from the top on the right. Now connect the wire that you connected to the other end of the resistor to the 4th pin from the top on the left.
12
Connecting to the pi In order to connect to your Pi, you will find an icon on the desktop labeled setip. In order to run this program, you will need to be an administrator. Right-click on connect. In the menu, choose Run As Administrator You may be asked to give permission. Choose Yes.
13
ConnectTopi.jar On the desktop you will also find an icon labeled connect. This program will make it easy for you to connect to the Pi. Make sure that the network cable is connected to both the Pi and the laptop. Make sure Graphical is selected and then click Connect.
14
The desktop This is the desktop of the pi.
You are connecting to it through a service known as VNC.
15
The command prompt At the top of the desktop is a group of icons.
The one we will use is the Command Prompt icon. Click on the icon and you will see the Command Prompt.
16
CREATING A program The programming language we are going to use to create our program to turn the LED on and off is called Python. Python is an easy to learn and easy to use language for which many libraries have been written that work with the Pi. Python was named for the comedy group Monty Python’s Flying Circus.
17
gksu idle3 lightled.py Starting Idle
There is a nice IDE (Integrated Development Environment) that we can use to write and test Python programs. Is it called IDLE. When we want to access the GPIO pins, we need to have administrator access. In order to open a graphical program like IDLE so that it can run as administrator, we are going to type the following command into the command prompt. gksu idle3 lightled.py After you type in the command, hit the enter key
18
idle You will first see this window. Click Close.
This is the IDLE windows. We will write our program here. Two important menus here are File and Run. You can save your work by choosing File and Save. You can run your program by click on Run and Run Module.
19
Importing into python There are certain functions that Python understands such as mathematical operations. However, there are some functions that Python can’t perform unless it has an additional package available. To make that available, we use import statements As long as the package can be found, we will be able to use it in our program. The package that we will need to control the pins of the pi is called RPi.GPIO Note that the spelling is important. This package contains certain functions which allow us to set up the pins to be used for input and output and set the pins to high voltage or low voltage
20
Importing packages from time import sleep
In order to input the GPIO package, we use the following line import RPi.GPIO as GPIO This loads the appropriate package and allows us to access the fields in it by using the name GPIO We also need to import a function from a package called time that allows us to pause the program. from time import sleep
21
Addressing the pins GPIO.setup(7,GPIO.OUT) GPIO.setmode(GPIO.BOARD)
In order to cause the voltage on a pin to change, we have to first find a way of identifying the pins. We will use a scheme called BOARD to number the pins. We use the following line to use this scheme. GPIO.setmode(GPIO.BOARD) If you hold the Pi with the network connection at the bottom and call the first pin in the left column pin 1, and the first pin in the second column pin 2, then the second pin in the first column pin 3, and the second pin in the second column pin 4, etc. then the 4th pin from the top in the first column is pin 7. We need to setup this pin so it can be used as output. GPIO.setup(7,GPIO.OUT)
22
Setting pin 7 high GPIO.output(7,True)
Since pin 7 is available to be used as output, all we need to do is use a function to set the voltage on the pin. GPIO.output(7,True) Note that the T has to be capitalized.
23
Pausing the program sleep(1)
Now we need to pause the program to keep the LED on. The function sleep accepts the number of seconds you want to pause. sleep(1)
24
Turning the led off GPIO.output(7,False)
Now we need to use the output function to turn off high voltage. GPIO.output(7,False) Note that the F is capitalized.
25
Cleaning up GPIO.cleanup()
To set the pins back to their initial states, we use the cleanup function. GPIO.cleanup()
26
To run the program go to Run and select Run Module
The complete program import RPi.GPIO as GPIO from time import sleep GPIO.setmode(GPIO.BOARD) GPIO.setup(7,GPIO.OUT) GPIO.output(7,True) sleep(1) GPIO.output(7,False) GPIO.cleanup() To run the program go to Run and select Run Module
27
Making the led blink Now that we know how to make the LED turn on and off, we can make the LED blink. In order to do this, we will use a loop. A loop is code that can repeat more than one time. In order to create a loop, we use a while loop. A while loop is based on a condition. As long as that condition is true, the loop will execute. The way we will create our loop is we will create a controlling variable. When the value of the variable reaches a limit, the loop will stop.
28
The while loop sleep(1) GPIO.output(7,False)
To create a variable we use the following line. counter = 0 In order to create a while loop, we use this syntax while (counter < 5): Notice that after you type the : and hit enter, IDLE will indent the next line. Indentation is very important in Python. The indentation of lines after the while defines what will execute in the loop. GPIO.output(7,True) sleep(1) GPIO.output(7,False)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.