40pin IO Development
Experiment 02 - GPIO Input
Hardware Connection
Connect the button circuit to IO37 (you can use a DuPont wire instead of a button).
Software Operation
Enter the user home directory and run:
cd usersudo python3 ./GPIO_input.py
Terminal as shown:

You can see that when the button is pressed, the terminal outputs low level; when released, the terminal outputs high level.
#!/usr/bin/env python3
import sys
import signal
import Hobot.GPIO as GPIO
import time
def signal_handler(signal, frame):
sys.exit(0)
# Define GPIO channel 37
input_pin = 37 # BOARD pin 37
GPIO.setwarnings(False)
def main():
prev_value = None
# Set pin numbering mode to BOARD
GPIO.setmode(GPIO.BOARD)
# Set to input mode
GPIO.setup(input_pin, GPIO.IN)
print("Starting demo now! Press CTRL+C to exit")
try:
while True:
# Read pin level
value = GPIO.input(input_pin)
if value != prev_value:
if value == GPIO.HIGH:
value_str = "HIGH"
else:
value_str = "LOW"
print("Value read from pin {} : {}".format(input_pin, value_str))
prev_value = value
time.sleep(1)
finally:
GPIO.cleanup()
if __name__=='__main__':
signal.signal(signal.SIGINT, signal_handler)
main()