root/Software files/Raspberry Pi/simple_gpio_test.py
661 | natark2 | from gpiozero import LED, Button
|
|
from time import sleep
|
|||
# Print introductory information
|
|||
print("-------------------------------------------------")
|
|||
print("Hello! Welcome to the Capstone RPi GPIO test program.")
|
|||
685 | xiea | 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.")
|
|||
661 | natark2 | print("Note that to test say GPIO4, or pin 7, use the number 4 in this" +
|
|
" program.")
|
|||
685 | xiea | print("Once you are satisfied, Ctrl-C to exit out of this script\n")
|
|
661 | natark2 | ||
685 | xiea | # Ctrl-C to exit
|
|
661 | natark2 | while True:
|
|
685 | xiea | # Does user want to test input or output?
|
|
print("Are you testing input or output?")
|
|||
print("Please type \"input\" or \"output\": ")
|
|||
661 | natark2 | while True:
|
|
685 | xiea | option = input()
|
|
if option == "input" or option == "output":
|
|||
661 | natark2 | break
|
|
else:
|
|||
685 | xiea | print("Please type \"input\" or \"output\"")
|
|
661 | natark2 | ||
685 | xiea | # Testing lighting an LED
|
|
if option == "output":
|
|||
print("Please toggle the toggle switch to Output.")
|
|||
661 | natark2 | ||
685 | xiea | # 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!")
|
|||
661 | natark2 | ||
685 | xiea | # Set up LED object
|
|
led = LED(output_pin)
|
|||
661 | natark2 | ||
685 | xiea | # 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.")
|
|||
661 | natark2 | ||
685 | xiea | # 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!")
|
|||
661 | natark2 | ||
685 | xiea | # Set up button object
|
|
button = Button(input_pin)
|
|||
661 | natark2 | ||
685 | xiea | 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)
|