Posts: 1
Joined: Sun Feb 26, 2017 4:17 am

simple relay transistor attchment to pi and make it toggle

Sun Feb 26, 2017 4:33 am

I have a raspberry pi 3, and I have a 5 pin 5 volt relay(QIANJI JQC-3F) and a transistor(2N2222). All I ever want to do is to make the relay on and off using a simple blink led sketch

here are my connections:

Pi ground -> relay ground
Transistor curve surface is facing the relay
Transistor left leg -> relay 5v input terminal
Transistor right leg -> pi 5v terminal
Transistor middle leg -> GPIO 27

code :

import RPi.GPIO as GPIO
import time
GPIO.setup(27, GPIO.OUT)
while 1:
GPIO.output(27, GPIO.HIGH)
GPIO.output(27, GPIO.LOW)

Its very simple, but it didnt toggle the relay

Posts: 11163
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: simple relay transistor attchment to pi and make it togg

Sun Feb 26, 2017 2:19 pm


First off check you have all the parts connected correctly like in this diagram
you need a resistor in the between the GPIO and the base of the transistor to limit current flow and a diode across the relay coil to prevent the reverse voltage damaging the transistor on relay switch off.
this diagram will show you which legs are which on the transistor
also make sure you are connected to the correct GPIO pin.
GPIO is pin 13

your code looks ok but you might want to change you time.sleep to 2 to give the relay time to switch on and then off.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
Posts: 6454
Joined: Thu Sep 29, 2011 4:34 pm
Location: Berkshire UK Tralfamadore
Contact: Website

Re: simple relay transistor attchment to pi and make it togg

Mon Feb 27, 2017 10:51 am

The 2N2222 comes in two different packages: https://upload.wikimedia.org/wikipedia/ ... Pinout.jpg
So describing your circuit in terms of its curved surface doesn't help - "left leg" could be either Emitter or Collector

Also, you are putting the transistor in the 5v supply to the relay. To switch it on you would need to drive its Base to 5v or higher. The Pi can only drive its GPIO to 3v3.

Connect it as in pcmanbob's diagram

Return to “Beginners”