I've designed what I thought would be a simple relay driver circuit to drive a relay with a raspberry pi GPIO pin (which is pulled down with a 50K resistor internally). In the circuit diagram, pin 6 on the relay is connected to +5v, but that didn't make it into the image. The relay is used to isolate the circuit and pi from a second device. The relay pulls a pin to ground on the second device (GNDS, not circuit GND).

I originally breadboarded this circuit and had no issues whatsoever, it worked perfectly for weeks. Then I built the circuit on a protoboard and immediately ran into problems with the relay actuating randomly. Sometimes once a minute, sometimes several times per second. I tried adding a small filter capacitor to the base of the transistor, I've double and triple checked my solder connections, replaced components(same model, but different part). No luck. I designed a PCB for the circuit and built it, and I had the same issues as the protoboard. I've breadboarded it again and it works just fine!

Can anyone see what I've done wrong or suggest something I should try?


The full schematic can be found here:https://ve7mhw.weebly.com/uploads/1/2/9/9/129977040/pcb.pdf

NPN Relay Driver




  • \$\begingroup\$ What happens to LED D3 when the relay activates randomly? I assume it goes on. If you remove the relay, does D3 activate randomly by itself? If you put an o'scope probe on pin 26/37 does it activate randomly? \$\endgroup\$ Commented Aug 26, 2023 at 23:26
  • \$\begingroup\$ Yes, D3 does come on each time the relay activates, so I assume that my issue is with the transistor. Unfortunately, I don't have an oscilloscope, I haven't tried pulling out the relay, I will try that and report back. Thanks. \$\endgroup\$
    – Matt
    Commented Aug 26, 2023 at 23:34
  • 1
    \$\begingroup\$ @FabioBarone, I'm not exactly sure where I got the ribbon to breadboard interface, however, Adafruit sells one just like it here: adafruit.com/product/2028 or if you're in Canada as well, then BC Robotics is a reseller here: bc-robotics.com/shop/adafruit-pi-t-cobbler-plus-breakout \$\endgroup\$
    – Matt
    Commented Aug 27, 2023 at 2:35
  • 1
    \$\begingroup\$ @FabioBarone The schematic is a reflection of all versions, I didn't post the full schematic as to not add complexity to the post, but there are 2 relays and accompanying components. I will edit the original post with a copy of the full schematics and respond further to your longer post, Thank you. \$\endgroup\$
    – Matt
    Commented Aug 27, 2023 at 2:42
  • 1
    \$\begingroup\$ Thanks everyone, you've given me lots of great suggestions and I'll let you know how it pans out. \$\endgroup\$
    – Matt
    Commented Aug 27, 2023 at 4:16

1 Answer 1


OK, lets examine the relay coil. Part no: HK4100F-DC5V-SHG. Datasheet (link 1 below) says this has a resistance of 125ohm (the "H" in "SHG" indicates coil power of 0.2W). At 5V this draws 40mA, and the parallel LED indicator draws about 2mA, which we can ignore.

The BC337 transistor datasheet (link 2 below) specifies all data regarding saturation mode with a collector current to base current ratio of 10, so that means in this case applying base current of 4mA to be able to apply the datasheet specs. However, having used this transistor many times, Ic=40mA represents a relatively low current, and a ratio of 20 for base current will be more than enough to ensure it is fully saturated, so let's go for 2mA of base drive.

That means the base current limiting resistor can be selected based on:
(Vo - 0.7V) / 2mA,
where Vo is the output voltage of the Raspberry Pi GPIO pin. Assuming that is 3.3V, then you could probably increase R2 from 390r to, say, 1k without too much concern, which reduces the current the GPIO pin has to supply, which is a good start.

The next issue to look at is the coil current path. It flows from the +5V supply to the gnd via Q2. Please ensure that this current path, including all the wires that go between the two PCAs (the Raspberry Pi, and the PCA with the relay) are low impedance, and have good connections. Think like an electron, and follow the complete path all the way from the +5V power supply negative terminal, all the way to +5V supply positive terminal (or use conventional current flow from positive to negative if you prefer). You need to ensure that this path has low impedance, and is not being affected by other currents from other devices that could cause the coil current to reduce unexpectedly. Is there a bad solder join, or perhaps a connector pin that is being asked to carry more current than it should?

The next issue: Is the circuit immune to electromagnetic fields? I would usually put a resistor across the base of Q2 to ensure it stays OFF when you turn it off. Something like a 10k will be fine - this will rob about 0.07mA from the base drive when you turn it on, but since there is 2mA of drive available this won't be a problem.

You have D4 protecting Q2 from excessive voltage at coil turn-off, so all is good there.

I think if you follow the above you should be able to sort out your problem.

My gut feel is: a combination of (a) changing R2, (b) adding 10k across Q1 BE, and (c) ensuring a good current path for the coil current will fix your troubles. I suspect one or more connections between the Rasberry Pi and your relay card may be not quite right.

Link 1: https://datasheetspdf.com/pdf-file/1493116/HUIKE/HK4100F-DC5V-SHG/1

Link 2: https://www.onsemi.com/pdf/datasheet/bc337-d.pdf

  • \$\begingroup\$ Regaring your comments above, D4 is underneath the protoboard design soldered right to the relay pins, however, on the PCB, it is right below the GPIO pins. I'm beginning to understand that putting it there was probably a mistake, as it should have been closer to the relay. Regarding the state of the relay, the problem is "a" the relay is coming on when it should be off. The black component between the relays is just a jumper wire from the GPIO header to the prototyping area. \$\endgroup\$
    – Matt
    Commented Aug 27, 2023 at 2:54
  • 1
    \$\begingroup\$ OK, if the problem is: "(a) the relay is coming on when it should be off", then I suggest making R2 bigger (~1K), and putting a 10K across Q2 BE. \$\endgroup\$ Commented Aug 27, 2023 at 3:00
  • \$\begingroup\$ @Matt Although I do agree with Fabio about adding the 10k resistor across the BE junction (cheap to do and helps avoid problems), pulling the relay in requires a fair bit of base current, which almost certainly cannot come from static charges or charge storage alone. Having problems with a solderless protoboard (with thick wires hanging all over the place and trace capacitances connecting everything) and a PCB (with exceptionally thin traces) while both times you built this on a breadboard it worked fine says that we likely cannot guess at the problem well. Can you scope circuit points? \$\endgroup\$ Commented Aug 27, 2023 at 3:08
  • \$\begingroup\$ I will try that, thank you for taking the time to post, there's a lot of good info here for me. \$\endgroup\$
    – Matt
    Commented Aug 27, 2023 at 3:10
  • \$\begingroup\$ @Matt If those two changes don't fix it, then the problem is more to do with the physical implementations. So things like wiring, connections, stray capacitances, etc, all contribute. Question: is the unexpected turn-on of the relay very short in duration (<1 sec), or does it last several seconds? \$\endgroup\$ Commented Aug 27, 2023 at 3:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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