9

I am trying to toggle a small relay on and off. I have connected a GPIO pin and GND directly to the relay. I can hear the relay clicking, when I set the GPIO pin to HIGH.

However, the relay only rarely lets a signal through. It works some times, but rarely. If I set the pin to HIGH, and measure the voltage, it is only around 1.6V - so I guess sometimes there is not enough power to drive the relay.

I have previously measured the voltage on the pins, and it read 3.3V. Any ideas on what could be the problem?

goldilocks
  • 58,859
  • 17
  • 112
  • 227
Andreas
  • 193
  • 1
  • 3
  • If you've connected the relay directly to the Pi's GPIO pins it's unfortunately quite likely that you've broken something. Is the relay mounted on any kind of PCB module with isolators? – goobering Sep 23 '16 at 07:58

3 Answers3

19

If you want to drive a single low-voltage relay, using a 7-channel driver chip will be an overkill. You'll be perfectly fine with a single NPN transistor and a flyback diode:

schematic

simulate this circuit – Schematic created using CircuitLab

Note: R1 can be replaced by a LED and a 200 - 500 Ohm resistor in series if you want a visual clue about the state of your relay. This is what is often done on pre-made relay boards.

Dmitry Grigoryev
  • 27,928
  • 6
  • 53
  • 144
14

You should not directly drive a relay from the Raspberry Pi. An individual GPIO can only safely provide about 16mA at 3V3 which is unlikely to be enough to energise the coils of a standard electromagnetic relay. Even if you could the back EMF caused by the collapsing magnetic field in the relay coils when it was switched off could destroy a GPIO and the Pi.

You should use the GPIO to switch an external circuit to switch the relay.

Typically a transistor (to amplify the current) and a diode (to protect against back EMF) would be used.

Personally I'd probably use a ULN2003A chip as they were sort of designed for this purpose.

There are lots of relays built into driver boards available for the hobby market.

joan
  • 71,024
  • 5
  • 73
  • 106
  • Aha, so its basically a circuit like the one shown here - but connected to the 3v3 rail instead? – Andreas Sep 23 '16 at 08:03
  • @Andreas Yes, although you could use the 5V rail if the relay is happy with 5V. You are using the GPIO to switch the transistor on. The transistor will be quite happy to switch 5V. – joan Sep 23 '16 at 08:17
  • Alright, makes sense. Thanks a lot. Hope I have not yet fried anything. Geez, software is so much easier than hardware. – Andreas Sep 23 '16 at 08:19
  • 2
    @Andreas Alas, if software was easier than hardware, we wouldn't be rebooting computers to fix their problems. – Dmitry Grigoryev Sep 23 '16 at 12:20
  • I like OnSemi's line of NUD31xx relay drivers. They work better than any individual transistors and diodes I've seen. – Jeanne Pindar Sep 23 '16 at 13:16
  • Note that some relays have the protection diode built in -- this makes things a little more compact and less assembly required. But before omitting the diode from your design, be sure the exact model of relay you're using has it built in (usually a quick Google of the relay's part number will yield a datasheet that indicates such). – Doktor J Sep 24 '16 at 03:39
0

I have successfully used I2C expander MCP23017 with cheap relay-boards. I have not actully written any how-to's yet but there is plenty around, just look at google and search for "RPI MCP23017".

  • Welcome to Raspberry Pi! Please take the tour and visit the helpcenter to see how things work here. Whilst this may theoretically answer the question, it would be preferable to include some explanation here (without having the OP look it up elsewhere)? – Ghanima Sep 25 '16 at 19:11