|
from gpiozero import LED, Button
|
|
from time import sleep
|
|
|
|
|
|
# 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("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")
|
|
|
|
# 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\"")
|
|
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
|
|
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:
|
|
break
|
|
else:
|
|
print("Invalid pin number!")
|
|
|
|
# Set up LED object
|
|
led = LED(output_pin)
|
|
|
|
# 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.")
|
|
|
|
# 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)
|
|
|
|
# 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!")
|
|
|
|
# Set up button object
|
|
button = Button(input_pin)
|
|
|
|
# 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")
|
|
|
|
# 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)
|