Project

General

Profile

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

Revision 661

Test instructions for Jetson and Raspberry Pi.

View differences:

Software files/Raspberry Pi/simple_gpio_test.py
DD EE
from gpiozero import LED, Button
DD EE
from time import sleep
DD EE
DD EE
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("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
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
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
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
break
DD EE
else:
DD EE
print("Invalid pin number!")
DD EE
DD EE
# Set up LED object
DD EE
led = LED(output_pin)
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
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
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
DD EE
# Set up button object
DD EE
button = Button(input_pin)
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
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)

Also available in: Unified diff