Raspberry Pi is a great thing
Parts Of Lists
Parts Of Explanation
Push Button Switch
Cobbler breakout board
Resistor
Standard LED
Connection
First step is to connect the Push Button and LED on Raspberry Pi.
LED Connection
Connect the Positive Pin of the LED to the GPIO 4 on the Raspberry Pi and negative pin to the GND pin.
PushButton Connection
The circuit connection
Cobbler breakout board
Programming, GPIO Coding
  1. # gpio_blink.py
  2. # LED is on pin 4, use a 270 Ohm resistor to ground
  3. import RPi.GPIO as GPIO
  4. import time
  5. GPIO.setwarnings(False)
  6. GPIO.setmode(GPIO.BCM)
  7. GPIO.setup(4, GPIO.OUT)
  8. state = True# endless loop, on / off for 1 second
  9. while True: GPIO.output(4, True) 
  10. time.sleep(1) 
  11. GPIO.output(4, False) 
  12. time.sleep(1)
Explanation
Python coding
  1. # gpio_swtich.py
  2. # LED is on pin 4, use a 270 Ohm resistor to ground
  3. # Switch is on pin 22, use a pull-down resistor(10 K) to ground
  4. import RPi.GPIO as GPIO
  5. import time
  6. GPIO.setwarnings(False)
  7. GPIO.setmode(GPIO.BCM)
  8. GPIO.setup(4, GPIO.OUT)
  9. GPIO.setup(22, GPIO.IN)# input of the switch will change the state of the LED
  10. while True: GPIO.output(4, GPIO.input(22)) 
  11. time.sleep(0.05) 
Explanation