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?

1
  • 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
    Commented Sep 23, 2016 at 7:58

3 Answers 3

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.

6
  • Aha, so its basically a circuit like the one shown here - but connected to the 3v3 rail instead?
    – Andreas
    Commented Sep 23, 2016 at 8: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
    Commented Sep 23, 2016 at 8:17
  • Alright, makes sense. Thanks a lot. Hope I have not yet fried anything. Geez, software is so much easier than hardware.
    – Andreas
    Commented Sep 23, 2016 at 8:19
  • 2
    @Andreas Alas, if software was easier than hardware, we wouldn't be rebooting computers to fix their problems. Commented Sep 23, 2016 at 12:20
  • I like OnSemi's line of NUD31xx relay drivers. They work better than any individual transistors and diodes I've seen. Commented Sep 23, 2016 at 13:16
20

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.

4
  • (The simulate link is not found. Can you fix this?)
    – nixda
    Commented Sep 23, 2016 at 10:57
  • @nixda hopefully fixed now Commented Sep 23, 2016 at 11:51
  • 4
    Be careful with that additional LED idea though. If the unaware reader uses a white or blue LED the voltage drop might be too large to sufficiently drive the transistor.
    – Ghanima
    Commented Sep 23, 2016 at 19:31
  • 1
    Very nice, simple and easy diagram using common components! Thank you!
    – RufusVS
    Commented Jun 19, 2019 at 17:27
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".

1
  • 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
    Commented Sep 25, 2016 at 19:11

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.