40pin IO Development
Experiment 03 - Button Control LED On/Off
Hardware Connection
Connect the button circuit to IO37 (you can use a DuPont wire instead of a button), and connect the LED bulb to IO31.
Software Operation
Enter the user home directory and run:
cd usersudo python3 ./button_led.py
Terminal as shown:

You can see that when the button is pressed IO31 outputs low level; when released IO31 outputs high level (LED on/off).
#!/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 channels:
# led_pin as output to light an LED
# but_pin as input to connect a button
led_pin = 31 # BOARD pin 31
but_pin = 37 # BOARD pin 37
# Disable warnings
GPIO.setwarnings(False)
def main():
prev_value = None
# Pin Setup:
GPIO.setmode(GPIO.BOARD) # BOARD pin-numbering scheme
GPIO.setup(led_pin, GPIO.OUT) # LED pin set as output
GPIO.setup(but_pin, GPIO.IN) # Button pin set as input
# Initial state for LEDs:
GPIO.output(led_pin, GPIO.LOW)
print("Starting demo now! Press CTRL+C to exit")
try:
while True:
curr_value = GPIO.input(but_pin)
if curr_value != prev_value:
GPIO.output(led_pin, curr_value)
prev_value = curr_value
print("Outputting {} to Pin {}".format(curr_value, led_pin))
time.sleep(1)
finally:
GPIO.cleanup() # cleanup all GPIO
if __name__ == '__main__':
signal.signal(signal.SIGINT, signal_handler)
main()