Professional Documents
Culture Documents
COMP 4131 - Lab 4 - Operation With External Circuits - Fall2022
COMP 4131 - Lab 4 - Operation With External Circuits - Fall2022
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
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
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
GPIO #22
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.
2. Connect to +5 at pin 2 or 4V
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:
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 AccessoriesText 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)
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.
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
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Ω
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")
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.
7
Software
5. Open Lab4PB1.py
GPIO.setup(21,GPIO.IN)
input = GPIO.input(21)
while True:
if (GPIO.input(21)):
print("Button is Pressed")
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.
8
Part C – Combined Input / Output circuit
GPIO #26
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.
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
9
Counting Inputs
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
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
10
Acting on a maximum count.
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.
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
11