The switch is a component that connect two points in a circuit when you press it.LED turns on when you press the button.
Hardware Required
- Push Button.
- LED
- Resistor
- Raspberry pi
- Jumper wires
- Breadboard
Circuit

Code
- Create a new text file “switch.py” by typing the following “nano switch.py”
- Now type the following code
import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) //BCM is broadcom which specifies the actual pin number considered by broadcom chip GPIO.setwarnings(False) //Ignore warnings GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) //Set pin 24 to be an input pin and set initial value to be pulled low (off) GPIO.setup(23,GPIO.OUT,initial=GPIO.LOW) //set pin 23 as output & set initial value to low(off) if GPIO.input(24) == GPIO.HIGH: print ("Button pressed") //This line prints the status on to the terminal. GPIO.output(23,GPIO.HIGH) time.sleep(3) //delay function pauses the program for 3 secs else: print ("Button not pressed") GPIO.output(23,GPIO.LOW) time.sleep(3) GPIO.cleanup() //To reset the pin status
- To run type python switch.py in the terminal.