Project

General

Profile

« Previous | Next » 
<-- MJA add screen reader only label -->

Revision 685

Added by Allen Xie about 1 year ago

Update the gpio test script for RPi for use with the board

View differences:

Software files/Raspberry Pi/simple_gpio_test.py
DD EE
# Print introductory information
DD EE
print("-------------------------------------------------")
DD EE
print("Hello! Welcome to the Capstone RPi GPIO test program.")
DD EE
print("This program will either light an LED, or read a button input.")
DD EE
print("The wiring instructions are given later in the program.")
DD EE
print("To get a pinout of the Raspberry Pi, run the command \"pinout\" in the" +
DD EE
" terminal.")
DD EE
print("This program is meant to be used with the Design Lab MCU tester.")
DD EE
print("To get a pinout of the Raspberry Pi, run the command \"pinout\" in " +
DD EE
"the terminal.")
DD EE
print("Plug a 3.3V power pin into the tester's VCC header.")
DD EE
print("Plug a ground pin into the tester's GND header.")
DD EE
print("Plug the pin to be tested into the tester's pin header.")
DD EE
print("Turn the potentiometer all the way counter-clockwise.")
DD EE
print("Note that to test say GPIO4, or pin 7, use the number 4 in this" +
DD EE
" program.")
DD EE
print("Picking either input or output would lock you in an infinite loop.")
DD EE
print("Once you are satisfied, Ctrl-C to exit out of this script.\n")
DD EE
print("Once you are satisfied, Ctrl-C to exit out of this script\n")
DD EE
DD EE
# Does user want to test input by reading a button or output by lighting an LED
DD EE
print("Are you testing input or output? Please type \"input\" or \"output\"")
DD EE
# Ctrl-C to exit
DD EE
while True:
DD EE
option = input()
DD EE
if option == "input" or option == "output":
DD EE
break
DD EE
else:
DD EE
print("Please type \"input\" or \"output\"")
DD EE
DD EE
# Testing lighting an LED
DD EE
if option == "output":
DD EE
# Get pin number that user wants to test
DD EE
# Does user want to test input or output?
DD EE
print("Are you testing input or output?")
DD EE
print("Please type \"input\" or \"output\": ")
DD EE
while True:
DD EE
output_pin = int(input("What is the pin you would connect an LED to? " +
DD EE
"For example, type 2 for GPIO2"))
DD EE
if output_pin >= 1 and output_pin <= 27:
DD EE
option = input()
DD EE
if option == "input" or option == "output":
DD EE
break
DD EE
else:
DD EE
print("Invalid pin number!")
DD EE
print("Please type \"input\" or \"output\"")
DD EE
DD EE
# Set up LED object
DD EE
led = LED(output_pin)
DD EE
# Testing lighting an LED
DD EE
if option == "output":
DD EE
print("Please toggle the toggle switch to Output.")
DD EE
DD EE
# Give instructions
DD EE
print("Please connect the long end of the LED to one end of a resistor," +
DD EE
f" then connect the other end of the resistor to pin {output_pin}." +
DD EE
" Then connect the short end of the LED to ground.")
DD EE
# Get pin number that user wants to test
DD EE
while True:
DD EE
output_pin = int(input("What is the pin you're testing? "))
DD EE
if output_pin >= 1 and output_pin <= 27:
DD EE
break
DD EE
else:
DD EE
print("Invalid pin number!")
DD EE
DD EE
# Blink LED
DD EE
print(f"Blinking LED connected to pin {output_pin}")
DD EE
print("Ctrl-C to exit")
DD EE
while True:
DD EE
led.on()
DD EE
sleep(1)
DD EE
led.off()
DD EE
sleep(1)
DD EE
# Set up LED object
DD EE
led = LED(output_pin)
DD EE
DD EE
# Testing reading a button
DD EE
if option == "input":
DD EE
# Get pin number that user wants to test
DD EE
while True:
DD EE
input_pin = int(input("What is the pin the button is connected to? " +
DD EE
"For example, type 2 for GPIO2"))
DD EE
if input_pin >= 1 and input_pin <= 27:
DD EE
break
DD EE
else:
DD EE
print("Invalid pin number!")
DD EE
# Blink LED 10 times
DD EE
print(f"Blinking LED connected to pin {output_pin}")
DD EE
print("Ctrl-C to exit")
DD EE
for i in range(5):
DD EE
led.on()
DD EE
sleep(1)
DD EE
led.off()
DD EE
sleep(1)
DD EE
# Testing reading a button
DD EE
elif option == "input":
DD EE
print("Please toggle the toggle switch to Input.")
DD EE
print("Please also turn the potentiometer all the way CCW.")
DD EE
DD EE
# Set up button object
DD EE
button = Button(input_pin)
DD EE
# Get pin number that user wants to test
DD EE
while True:
DD EE
input_pin = int(input("What is the pin you want to test? "))
DD EE
# There are only 27 GPIOs
DD EE
if input_pin >= 1 and input_pin <= 27:
DD EE
break
DD EE
else:
DD EE
print("Invalid pin number!")
DD EE
DD EE
# Give instructions
DD EE
print(f"Please connect one end of the button to pin {input_pin}. Then" +
DD EE
" connect the other end of the button to ground." +
DD EE
" Then press the button.")
DD EE
print("If the button is pressed, this program will print" +
DD EE
" \"Button pressed!\"")
DD EE
print("Ctrl-C to exit")
DD EE
# Set up button object
DD EE
button = Button(input_pin)
DD EE
DD EE
# Look for button press (when the pin reads LOW, as it defaults to internal
DD EE
# pull-up)
DD EE
while True:
DD EE
button.wait_for_press()
DD EE
print("Button pressed!")
DD EE
sleep(0.2)
DD EE
print("Every half a second, if the button is pressed, the program " +
DD EE
"will say so!")
DD EE
# Press the button
DD EE
for i in range(10):
DD EE
if button.value() == 1:
DD EE
print("Button is pressed!")
DD EE
sleep(0.5)

Also available in: Unified diff