Electrical – Correct way to control 12V relay from a Raspberry Pi

npnpnpraspberry pirelaytransistors

I am working on a project where I need to control a couple of relays from the GPIO header on a Raspberry Pi. I tried getting a PCB with the layout like the right version in the image below, but I cannot switch the relay by setting the GPIO pin high/low.

After some more studying I came up with version 2, which is the left version, using an NPN instead of a PNP transistor – is it correctly understood that it looks more correct than the first version?

VCC is 12v and H1PXX is the GPIO pins which are 3.3v. P1/P2 are used to determine what to switch and are not relevant for this

Can I somehow make the right version work? I already have the printed circuit boards, so if it is just a matter of soldering on a wire/resistor somewhere I could do that without too much of a problem.

The board has a 5V regulator for the Raspberry Pi, so GND on the circuit is shared with the Raspberry Pi

schematic

Best Answer

The problem with PNP circuit is that the Raspberry Pi works on 3.3V while your Vcc is 12V.
So, putting either a low signal (0V) or high signal (3.3V) always will turn on the PNP, because in both cases VBE = VCC - Vbase = about 0.7V.
The base current will be either (12V-0.7V-3.3V) / 1kΩ = 8 mA or (12V-0.7V-0V) / 1kΩ= 11.3mA.

Since you already have the printed circuitboards you should implement the NPN version.

EDIT
Regarding the NPN version: as you commented you need a NPN with the correct pinning (i.e. with collector and emitter swapped) like the MMBT9013 (don't know if it still exists).

Other solutions i can come up with require rework (don't if the number of PCB's and/or the space on the PCB allows for it)

  • putting a 9.1 zener in series with R10 using the pads of R10 (placing the components diagonal like /\ instead of --)
    you may need to lower the value of R10

  • replacing the PNP with a PMOS (e.g. NTR4171P), solder an 1k 0805 resistor on top of the gate and source pin and replace R10 by a tiny 9V zener (no idea what package R10 has, but e.g CDZFH9.1B)