# Control 5v relay through 3.3v GPIO using NPN transistor

I'm not very pratical as I just started learning electronics and I'm planning to make some experiment with Raspberry Pi.

I'm trying to control a 5v relay board that, I think, is made for Arduino, as Raspberry only has 3.3v GPIO.

It's active low, as GPIO.LOW activates the relay, and the 3.3v keep it active too, so I think I'll need exactly 5v to turn it off.

So, basically I think I'll need a transistor, with the base controlled by my GPIO, that will activate a 5v line to Input line of the relay switch.

I have thought about these schematics, but I've been told it will burn my transistor as I need a resistor between 5v and the transistor collector.

My question is certainly basic, but... WHY?

When no current flow through base there will be no problem, when the transistor gets activated isn't my load the relay board? Why do I need other resistors?

This is the relay board I bought, I couldn't find schemas, so I can't provide more.

• You should provide a link to the relay board, or at least a manufacturer and part number.
– Tut
Jul 14, 2016 at 11:23
• I edited the first post, I could only find this! Jul 14, 2016 at 11:38
• The main problem with your circuit is that when Q1 turns on, it will be trying to short-circuit the +5V supply to ground. Jul 14, 2016 at 14:25
• The biggest thing here is the lack of a schematic, or even reading the transistor that is on there. Can you see any numbers? Can you make a schematic with a multimeter? Are you sure In1 is active low? Both answers are good, but it depends on if it's a PNP or NPN transistor setup. Jul 14, 2016 at 17:38
• I have thought about these schematics, but I've been told it will burn my transistor as I need a resistor between 5v and the transistor collector. Because once you turn on the transistor, you basically short circuit the 5V to Ground. It would try to push AMPS through the short, damaging the transistor and likely your power supply. Jul 14, 2016 at 17:42

From a quick look at the relay board I note the following:

These components provide for a buffered drive circuit of the relay coil. The below schematic is the likely configuration of these components. (LEDs not included).

You should be able to connect your 5V supply between the VCC and GND inputs. Then connect the GPIO signal to the IN1 terminal and also the MCU board ground to the GND pin.

This driver circuit should work with either a 0->3.3V swing or 0->5V swing GPIO. Not knowing just how those two LEDs are connected up it may be that one or the other LED may not work with a 0->3.3V swing of the LEDs are connected into the base circuit of the transistor. On the other hand if the two LEDs are connected up into the collector side of the transistor similar to as follows the the LEDs should work as well. A simple check of the circuit with an ohm meter should be able to confirm how the LEDs are connected.

• Hi Michael, thanks for your precious effort. With GPIO in mode LOW\HIGH the level of the switch always result as connected; it's not just a led issue, as I can't hear the relay "click". As a wiring, I sent my Raspberry 5v to vcc, the board GND to Raspberry GND and the GPIO output to IN1, isn't that right? Jul 14, 2016 at 13:27

The schematic is probably like this - inside the dotted box (ignoring the LEDs). The transistor is a PNP type so you need to apply close to +5 to get it to turn off.

You can do this with one transistor and a couple 10K resistors, as shown. The circuit will work with 0/3.3V input (anything from a bit over 1V to more than 10V is an acceptable '1', and < 200mV is '0').

simulate this circuit – Schematic created using CircuitLab

The way this works is that when Q2 is off, R2 pulls the input voltage to the module up to +5V, so no base current flows in Q1. Q1 is thus off, and the relay remains dropped out.

When a voltage is applied to the input, Q2 turns on, pulling its collector down to < 100mV, which causes current to flow through R2 and through R1 (from the base of Q1). Q1 turns on and the relay pulls in.

The collector of the NPN should not be connected directly to 5V -- right now, nothing limits the amount of current flowing through the transistor, so the magic smoke will come out. Instead, a resistor is put between the collector and the 5V supply to limit the current flowing through the transistor when it is ON.

The relay board itself isn't treated as a "load" as it already has a transistor on it to provide enough current to turn the relay on and off -- an Arduino's output pin isn't beefy enough to do that without the relay module's transistor's help.

• Ok, I think I understand your answer. If my relay board absorbs 20mA, I will have to supply more than 20mA to saturare the transistor or is it enough? In the case I just need 20mA so I will have to put a 5\.002 = 250 ohm just after the vcc that goes to the relay input, right? Jul 14, 2016 at 11:52

When your transistor is conducting from collector to emitter (assumed operating in saturation), there is little resistance from 5 volt to ground. Given a, say, 2 amp power supply, that's 10 watts (2 amps times 5 volts) of power the transistor will have to deal with. Further, if the transistor survives, the power supply will likely fold dropping the voltage to all the circuit causing all kinds of unexpected results.

If you are using an Arduino Uno and a USB power source, I believe there is a 500mA fuse on the Arduion board in line between the 5 volts and the USB power source.

If you are using an Arduino Uno and an external, say, 12 volt power supply, I believe you will be pulling power through the tiny 5 volt power supply regulator on the Arduino board.

You can verify by studying the Arduino Uno's schematic here.

If you are driving the relay coil directly, and the other side of the coil is connected to positive voltage, you need an open collector arrangement. You should also use a fly-back diode.

If you are driving a logic circuit which then drives the relay, you likely need to add a large resistor between 5 volts and the collector of your transistor. Then connect the input of the relay logic circuit at the node where the collector meets the resistor.

Finally, it may be that the relay board logic, if there is any logic circuit on the relay board, will tolerate 3.3 volt logic. That is, have a low enough turn on threshold to work with either 3.3 volt logic or 5 volt logic. However, such a relay board would be more susceptible to noise (falsely turning on) when driven by a 5 volt signal.

• As I tested, 3.3v volts are seen as 0v --> The switch is on. How can I calculate the resistor I need between the 5v and the collector? Should I divide 5v to the current absorbed by the board, or more current needs to be considered? Jul 14, 2016 at 12:12
• Really need the schematic of the relay board. Not having that, what you are looking for is a low enough resistance (with out the transistor being on - just remove the transistor for now) that the relay driver circuit will just turn on the relay. Then pick a even lower resistance to make sure the relay will stay on despite noise on the power line. But don't go to low as that will only wast power. As current will flow through this resistor all the time. Jul 14, 2016 at 12:22