====== ButtonLED ====== **//The Circuit://** {{:hpc0:buttonled.jpg?nolink|}} **//The Code://** 1 #!/usr/bin/env python3 2 ######################################################################## 3 # Filename : ButtonLED.py 4 # Description : Control led with button 5 # auther : www.freenove.com 6 # modification: 2019/12/28 7 ######################################################################## 8 import RPi.GPIO as GPIO 9 10 ledPin = 11 # define ledPin 11 buttonPin = 12 # define buttonPin 12 13 #initial setup of the board 14 def setup(): 15 16 GPIO.setmode(GPIO.BOARD) # Use PHYSICAL GPIO Numbering 17 GPIO.setup(ledPin, GPIO.OUT) # Set ledPin to OUTPUT mode 18 GPIO.setup(buttonPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Set buttonPin to PULL UP INPUT mode 19 20 #the main loop. Controls LED 21 def loop(): 22 while True: 23 if GPIO.input(buttonPin)==GPIO.LOW: # If button is pressed 24 GPIO.output(ledPin,GPIO.HIGH) # Turn on led 25 print ('led turned on >>>') # Print information on terminal 26 #if button is released 27 else : 28 GPIO.output(ledPin,GPIO.LOW) # Turn off led 29 print ('led turned off <<<') 30 31 #when done turn everything off 32 def destroy(): 33 GPIO.output(ledPin, GPIO.LOW) # Turn off led 34 GPIO.cleanup() # Release GPIO resource 35 36 if __name__ == '__main__': 37 print ('Program is starting...') 38 setup() 39 try: 40 loop() 41 except KeyboardInterrupt: # When program ends do this 42 destroy() 43 For this project I worked on making a circuit that will turn on an LED when a button is pressed. After setting up the circuit, as shown in the starting diagram, I went into the code and started cleaning it up. Primarily by making comment blocks are indented together, as can be seen within the definitions. Then I put in some comments that stated what every definition was to accomplish, and I deleted the comment telling us that we are entering the main function for it is not needed.