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



  • 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.
time.sleep(3)   //delay function pauses the program for 3 secs
print ("Button not pressed")
GPIO.cleanup() //To reset the pin status
  • To run type python switch.py in the terminal.