Raspberry pi with direct relay

gpioraspberry pirelay

I have a very back to basics question.
I've been using a 5v relay with the raspberry pi. The relay trigger is connected directly to an output pin and the other side is connected to the ground of the board.
I've been counting on the internal coil resistance to serve as over-current protection, and so far in small application it worked out fine. But I've have been told that this is not a good idea and i might damage the board if the output is held HIGH for too long. Any confirmation on this would be appreciated.

Best Answer

It might work at the moment, but you are running a risk not checking the various specs out and making sure you work within them. So I would use a transistor and the correct voltage.

I haven't used an R-pi, nor do I know the relay part number you are using, but if you are determined to go ahaead without a transistor, then what I would do is:

  • Read the R-pi datasheet for it's pins maximum working current, and the general advice surrounding safe use of the pins.

  • Measure the relay coil with a multimeter to check it's DC resistance (and read it's datasheet for other specs such as working voltage range, etc), work out the current drawn from your pin using Ohm's law. See what value resistor you would need to lower current to a safe level.

  • Make sure there is a reverse diode across the coil to protect the pin from kickbacks.

Having said the above, I would again really advise against trying to power a relay directly from an R-pi's pin, especially the wrong voltage.
There are loads of simple tutorials on using a transistor controlled from an IO pin to switch the relay. Even if you don't want to use a 5V supply, at the very least still use the transistor with the 3.3V supply - any general purpose one will do.

Basically, if you are running outside the specs with stuff like this, it may work, may work for a while then blow, cause seemingly unrelated issues, etc. That's why it's never a good idea to ignore them.