12v Relay

  • Relay is an electrically operated switch.
  • Relay is used to isolate electrical load.
  • Two configuration
    1.NO(normally open).
    2.NC(normally close).
  • Relay have a coil that is energized by 12v , when coil energized switching action takes place.


  • BC548 is general purpose NPN bipolar junction transistor.
  • This transistor act as a driver to pull the relay ON.
  • The base is connected with a micro-controller in series with the resistor.
  • Emitter to ground.
  • Collector to relay.

Hardware Required

  • Relay.
  • BC548 Transistor.
  • Push-button.
  • Raspberry pi.
  • Breadboard



  • Create a new text file “Relay.py” by typing the following

           nano Relay.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(23, GPIO.IN)  //Set pin 23 to be an input pin for push button.
GPIO.setup(24,GPIO.OUT)  //set pin 24 as output for relay.
if GPIO.input(23) == GPIO.LOW:
print ("Relay pulled On")//This line prints the status on to the terminal.
time.sleep(3)            //delay function pauses the program for 3 secs
print ("Relay pulled Off")
GPIO.cleanup()    //To reset the pin status
  • To run type python Relay.py in the terminal.