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