0
\$\begingroup\$

This question already has an answer here:

I want to use a relay switch with the GPIO on my raspberry pi 2 (40 pins).

I found the link below on google and it mostly makes sense, what I'd like to know is why do I need to use a transistor to activate the relay switch? why can I not just wire it up directly?

http://www.susa.net/wordpress/2012/06/raspberry-pi-relay-using-gpio/

please keep in mind I am a novice with electronics.

Thank you :o)

\$\endgroup\$

marked as duplicate by Passerby, Null, PeterJ, Dave Tweed Oct 4 '15 at 15:38

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1
\$\begingroup\$

The Raspberry Pi GPIO outputs (and outputs on most other microcontrollers) can only deliver a few mA, and even a small relay will require more current than the GPIO can handle. The site you link suggests keeping the GPIO output current below 3 mA, and suggests that the relay may require 50 - 100 mA.

A very small current into the base of a transistor can control a much larger current through its collector - it effectively amplifies the GPIO output to a level that can safely control the relay.

\$\endgroup\$
1
\$\begingroup\$

The G5LA-1 5DC relay specified in that link takes a rated 70mA to operate. This is more than the GPIO can handle.

Look at the circuit diagram in the link, and use all the components it shows. The series resistor R1 is needed to limit the base current, they suggest 1k will give about 3mA. The diode D1 is needed to avoid the relay turn-off spike killing the transistor. Don't get it backwards or it will kill itself, the transistor, or both, when the transistor turns on. As they say, Q1 can be almost any NPN transistor. With 3mA to drive the base, and only 70mA collector current required, almost any transistor will have adequate gain.

\$\endgroup\$
  • \$\begingroup\$ Ok thanks both of you... I now understand the need for the transistor. Am I correct in thinking the resistor helps control the mA supplied to the relay? I'm using a LU-5 relay switch in my circuit... does that change anything? and finally, how do I know which transistors are not adequate? \$\endgroup\$ – StuartMc Sep 30 '15 at 22:26
  • \$\begingroup\$ The resistor controls the current supplied to the transistor base. When the transistor is turned fully on, saturated to use the technical term, there is very little voltage across it, as far as the relay is concerned it is a switch that's turned on. The relay current is controlled by the supply voltage and the relay resistance. Needing a gain of only 30, and only 70mA collector current, you'd have to look very hard amongst exotic unusual special purpose transistors to find one that would not work. Any general purpose NPN will be fine. LU-5, where the -5 means 5v coil, will be fine \$\endgroup\$ – Neil_UK Oct 1 '15 at 7:27
  • \$\begingroup\$ If you have several relays to drive, then you could look at using a ULN2803 IC. It is basically 8 of those resistor + transistor + protection diode circuits in one package. It has a pin-out designed for the job, 8 inputs running along one side, and the 8 outputs running along the other. The outputs will handle up to 500mA and 50v, enough for some pretty big relays, lamps, LEDs and motors. \$\endgroup\$ – Neil_UK Oct 1 '15 at 8:06
  • \$\begingroup\$ Awesome thank you... one last question... I don't have any proper diodes but I have lots of LED's... could I use one of them instead of a normal diode to prevent the feedback? \$\endgroup\$ – StuartMc Oct 1 '15 at 11:38
  • \$\begingroup\$ Nooooooo, not LEDs. Their reverse breakdown voltage is insufficient, their forward current marginal. Buy a packet of 'proper' diodes. Nothing fancy, the cheapest general purpose types will be sufficient. If you'd like numbers, then 1N4148 is a good low power fast switching diode that's man enough for most relays. 1N4004 will let you rectify mains as well, and is as cheap as chips. \$\endgroup\$ – Neil_UK Oct 2 '15 at 7:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.