You are on page 1of 11

Faculty of Science, Engineering

and Information Technology (SEIT)


Fall 2022

COMP4131 Lab 4
RPi Operation with External Circuits Individual Lab

This lab will allow you to program and control external circuits using the RPi and Python
programming language.
Part A – Basic Output Circuit – LED Traffic Light
Part B – Basic Input Circuit – Detecting Pushbutton states
Part C – Combined Input / Output circuit

There are Five Demos to be made to the lab instructor.

Lab4Traffic.py Lab4PB2.py Lab4PB3.py Lab4PB4.py and Lab4PB5.py

Part A – Basic Output Circuit – LED Traffic Light


1. Insert the GPIO (General Purpose Input / Output) breakout board into your PCB
making sure the breakout board straddles the center grove in your PCB. Don’t let the
breakout board hang off the edge of the PCB.

2. To interface the Pi400 with the breadboard we need to attach the ribbon cable between the
GPIO breakout connector and the Pi400 – We want the wire connected to pin 1 on the
Pi400 to connect to Pin 1 on the breakout board. If it is not connected correctly, we could
short out the power and damage the Pi400. Check with your instructor if not sure.
Header Pin #1

Header Pin #40

1
3. Once you have determined how to connect the ribbon cable, it should be
disconnected while you build your circuit. Wire the following Circuit on your
breadboard.
Use 220 or 100 blue
resistors from your kit for
current limiting

GPIO #17

To resistor of red LED


To resistor of yellow LED
GPIO #27
To resistor of green LED

GPIO #22

Any GND pin or the


negative side of the
5V on the breakout
board (see next
page)

4. Test the Hardware:


It can be very difficult to troubleshoot a circuit that includes software if we don’t know for sure
that the hardware is working and ready to respond to the changes in output levels of the GPIO
pins. Whenever possible we would like to test the hardware first, before running the software
component. Below is an example of testing the hardware before running the program.

2
3.1 External Circuit Test:

Connect your ribbon cable and power up your RPi. We will now use the 5V available at pins
2 and 4 to test the LED circuits. One at a time, move the jumper wire from the GPIO pin and
touch it to the +5V connection. Each LED should light up as it is connected to 5V. Put the
circuit back to its original state.

1. Remove from GPIO #22 at pin15

2. Connect to +5 at pin 2 or 4V

3. Repeat to check all three diode circuits

3.2 Microcontroller Test

Now let’s make sure we have a properly working Raspberry Pi since this is the first time
we will use the GPIO pins.

1. Under Accessories open the Text Editor. (You can also use the python module if you like)
2. Type in the following program:

import RPi.GPIO as GPIO


GPIO.setmode(GPIO.BCM)
GPIO.setwarnings (False)
GPIO.setup(17,GPIO.OUT)
GPIO.setup(27,GPIO.OUT)
GPIO.setup(22,GPIO.OUT)
GPIO.output(17,1)
GPIO.output(27,1)
GPIO.output(22,1)
#GPIO.output(17,0)
#GPIO.output(27,0)
#GPIO.output(22,0)

3. Save the file to the desktop as HWTest.py  do not forget to add the .py extension so that
it will run as a python program. You should see the file on your
desktop.

4. Open a Linux Terminal (Fourth Icon to the left of the menu bar, looks
like a monitor)

5. To run the program, we must be in the same directory as the program (or point to it).
Change current directory by typing cd Desktop and you should see Desktop added to the
prompt.

3
6. Now run the test program by typing in sudo python HWTest.py and enter.

7. All your LED’s should now be on, and we have confirmed the RPi can control the LED’s

8. Now open the HWTest.py file by double clicking it from your desktop

9. Remove the number signs (hashtags) from in front of the bottom three commands and place
them in front of the three before that (the three ending in ,1). Save and run the program
again (you can use the up-arrow key to go back to previous commands). This will turn your
LED’s off. The hashtag makes a line into a comment and so the program does not execute
those lines.

4
5. Software:

1. Go to the start button AccessoriesText Editor (in options you might want to enable
line numbers). You can also use the python module Thorny if you prefer.

2. Type in the following program (the indent is important after “while 1:” Use the Tab key to
indent)

#COMP4131 Lab 4 Traffic Lights


GPIO_RED=17
GPIO_AMBER=27
GPIO_GREEN=22
TIME_AMBER=1
TIME_RED=2
TIME_GREEN=2
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(GPIO_RED,GPIO.OUT)
GPIO.setup(GPIO_AMBER,GPIO.OUT)
GPIO.setup(GPIO_GREEN,GPIO.OUT)
while 1:
#change to green
GPIO.output(GPIO_RED,False)
GPIO.output(GPIO_AMBER,False)
GPIO.output(GPIO_GREEN,True)
time.sleep(TIME_GREEN)
Note the #change to amber
indentations, GPIO.output(GPIO_RED,False)
use the Tab GPIO.output(GPIO_AMBER,True)
key. GPIO.output(GPIO_GREEN,False)
time.sleep(TIME_AMBER)
#change to red
GPIO.output(GPIO_RED,True)
GPIO.output(GPIO_AMBER,False)
GPIO.output(GPIO_GREEN,False)
time.sleep(TIME_RED)

3. Save your file as Lab4Traffic.py to your desktop

4. Open a Linux Terminal

5. Recall: to run the program we must be in the same directory as the program (or point to
it) if needed, change directories (type cd Desktop )

6. Check that your Lab4Traffic.py file is there by typing in lower case “LS” which is ls. If the
file is there, continue, if not try saving to desktop again.

5
7. Try running your program by typing in: python Lab4Traffic.py
Hey! It doesn’t work!

8. Notice how an error comes up. Your program tried to directly control hardware. The OS
would not allow this without correct permissions. You must run this as a super user by
giving the command “Super Do” or sudo.

9. Type in sudo python Lab4Traffic.py

10. Your traffic lights should now be operating. If not, interrupt the program with Ctrl C, look
for any error code or statement that might give you a hint of where the problem is. Go
back to your file and check the file for correctness. Save the file and try again.

11. If the traffic lights are running, to exit the program use Ctrl C

12. DEMO Lab4Traffic.py to your lab instructor. DEMO #1

6
Part B – Basic Input Circuit – Detecting Pushbutton states
Hardware:
1. Wire the following Circuit on your breadboard.

GPIO #21
Push Button Pin-out 220Ω

2. Open a new text editor and write the following program:

import RPi.GPIO as GPIO


GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

GPIO.setup(21,GPIO.IN)
input = GPIO.input(21)

while True:
if (GPIO.input(21)):  Note the indentations, use the Tab key.
print("Button is Pressed")

Save the file to your desktop as Lab4PB1.py

3. Run Lab3PB.py (sudo python Lab4PB1.py) when you press the Push Button, the
message “Button is Pressed” should appear (likely many times, filling the screen -
that’s ok). Troubleshoot if nothing shows up on the screen.
4. Remember to use Ctrl C to exit a running program.

If the hardware passes the testing, move on the next step

7
Software

5. Open Lab4PB1.py

import RPi.GPIO as GPIO


GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

GPIO.setup(21,GPIO.IN)
input = GPIO.input(21)

while True:
if (GPIO.input(21)):
print("Button is Pressed")

6. Save the file to the desktop as Lab4PB2.py


7. Open a Linux terminal and enter the command: “cd Desktop”
8. Run the program with: sudo python Lab4PB2.py
9. Test the program by pressing the Push Button. When you do, you should see “Button is
Pressed” printed on the screen many, many times, filling the screen.
10. Notice you get many, “Button is Pressed” notifications. This is because the program is told
to print this while the button is pressed, and at CPU speeds it is pressed for a long time.
Let’s fix that.
11. Exit the program with CTRL C
12. Add the following code to allow for a single response to a button press:
Add “ import time” and “time.sleep(0.2) as shown below:

import RPi.GPIO as GPIO


GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
import time

GPIO.setup(21,GPIO.IN)
input = GPIO.input(21)
 Just by adding 0.2 seconds between checking
while True: the output, we now can have a button press
if (GPIO.input(21)): registering once. This is software key debouncing.
print("Button is Pressed")
time.sleep(0.2)

13. Run the program and now try pressing the button a single time. Note that if you press and
hold the button, it will continue to indicate the button is pressed.

14. DEMO Lab4PB2.py to your lab instructor. DEMO #2

8
Part C – Combined Input / Output circuit
GPIO #26

1. Add an LED circuit to GPIO 26 as shown.

2. Open the program Lab4PB2.py in an editor

3. “Save as” the program to the desktop as Lab4PB3.py

4. Modify the program as shown below by adding the code lines shown in bold: This program
should now print to the screen and cause the LED to blink 3 times when you press the
button.

import RPi.GPIO as GPIO


GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
import time

GPIO.setup(21,GPIO.IN)
GPIO.setup(26,GPIO.OUT)
input = GPIO.input(21)

while True:
if (GPIO.input(21)):
print("Button is Pressed")
time.sleep(0.2)
count = 0
while count < 3:
GPIO.output(26, 1)
time.sleep(0.2)
GPIO.output(26, 0) Additional Code
time.sleep(0.2)
count=count+1

5. Save your file and run it.

6. Remember to use Ctrl C to exit the running program.

7. DEMO Lab4PB3.py to your lab instructor. DEMO #3

9
Counting Inputs

8. Open the program Lab4PB3.py to edit

9. “Save as” the program to the desktop as Lab4PB4.py

10. Modify the program as shown below by adding the code lines shown in bold. This
program will now display the number of times the button has been pushed

import RPi.GPIO as GPIO


GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
import time

GPIO.setup(21,GPIO.IN)
GPIO.setup(26,GPIO.OUT)
input = GPIO.input(21)
a=0
while True:
if (GPIO.input(21)):
print("Button has been Pressed"),a+1, "times"
a=a+1
time.sleep(0.2)
count = 0
while count < 3:
GPIO.output(26, 1)
time.sleep(0.2)
GPIO.output(26, 0)
time.sleep(0.2)
count=count+1

4. Save the file and run. Press the button several times and you should notice:

-the display now reports the number of times the button has been pressed

-the system cannot read a button press during the flashing LED loop

5. Use Ctrl C to exit the program.

6. DEMO Lab4PB4.py to your lab instructor. DEMO #4

10
Acting on a maximum count.

1. Open the program Lab4PB4.py for editing

2. “Save as” the program to the desktop as Lab4PB5.py

3. In this last modification, the program will act when the button count reaches 6.
The count will be reported on the monitor, but the LED will not flash every time
the button is pressed. Now when the count reaches 6, the LED will flash three
times and the message “Half-Dozen Completed, Change Box” is displayed. The
count will then start from 1 again.

4. Modify your program as shown below, be careful because some lines of


Lab4PB4.py have to be removed (the double equals sign is not a typo).

import RPi.GPIO as GPIO


GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
import time

GPIO.setup(21,GPIO.IN)
GPIO.setup(26,GPIO.OUT)
input=GPIO.input(21)
a=1

while True:
if (GPIO.input(21)):
print("Button has been Pressed",a,"times")
time.sleep(0.2)
if a==6:
print("Half-Dozen Completed, Change Box")
count=0
while count < 3:
GPIO.output(26,1)
time.sleep(0.2)
GPIO.output(26,0)
time.sleep(0.2)
count=count+1
a=0
a=a+1

5. Use Ctrl C to exit the program.

6. DEMO Lab4PB5.py to your lab instructor. DEMO #5

11

You might also like