Conecting a relay

I've noticed in several pictures that a relay is controlled by the arduino by connecting the 5v arduino pin to the relay and the ground is connected through a transistor, with is controlled by another pin on the arduino. In other pictures I se that a normal output pin from the arduino is connected to the relay, and no transistor is used. What is the difference between these two approaches. Is there an advantage in using the transistor, or are these just two ways of doing the same thing? I put two picture in case my explanation wasn't very good. Thank you.

Also as a second question I wanted to ask about the relays flyback diode. Do the relay modules that come with the arduino kits have the flyback diode or is it necessary to connect your own? I'm guessing that in the case of relays that don't come as part of a module, you do have to add that diode?

Thanks for your help. I don't want to fry my arduino.

What is the difference between these two approaches.

The first is done by someone who knows what they are doing and he second is done by an idiot. Their are a lot of idiots on the net.

It is possible that the relay coil takes less than 30mA which is rare in which case direct connection can be used. Or a transistor may be fitted as part of a relay board.

A relay module may or may not have a flyback diode, you have to look and see.

There are many variations of relay modules for the arduino out there, some a whole lot better than others. Some claim isolation, but only refer to the isolation the switched side of the relay gives to the coil side. Others (better ones) use opto-isolators to isolate the coil of the relay from the I/O pin of the arduino, and then some go further (best ones) and allow the complete isolation by allowing the relay coil to be driven from a seperate voltage supply.

The vast majority of those arduino relay modules use upwards of 70mA to energize the relay (make it switch). The 5V regulator can handle this easily, a digital pin cannot. Start using an 8-relay module and it can use up to 560mA with all relays energized. Some of the smaller boards cannot handle that, and being able to use a seperate supply for the coils makes it possible.

As for the flyback,....there 'should' be one across the relay coil already.(look and see). The switch side of the relay normally doesn't have one included, as it depends on the load you attach whether it is required. It certainly could be an issue if you are attaching an AC circuit.

Thank you for your help guys. I'm glad I asked before I connected anything. I'll be sure to use at least the transistor method. As for the diode, if push comes to shve and I don't know if there is a diode, putting a second flyback diode wouldn't hurt what it?

putting a second flyback diode wouldn't hurt

Correct. :slight_smile:

But just putting diodes into circuits willy-nilly is probably a leass than ideal plan.

Best to learn WHY you place diodes where they belong.

Just read Grumpy_Mikes s comment.

The first line is the most important.

You can use almost any NPN transistor instead of a MOSFET.

Just install a 2K2 resistor between the digital output and the base of the transistor.
(emitter is connected to ground / collector is connected to the relais).

Does the absence of a flyback diode can hang the arduino. (The realy is driving an ac load ) Please reply

Thanks in advance

The absence of a flyback diode across any relay coil driven by a transistor/mosfet switch can result in
BACK-EMF damage to the switching device. The higher the relay voltage the higher the back-emf.
The back-emf from a 5V relay is obviously quite a bit less than a 12V or 24V relay but driving 230Vac
with a 5V relay is less than ideal also. Normally I would use a rugged industrial grade 12V relay . I have used these to switch 120vac. For 277-480vac I use a
contactor with a 120vac coil, which is sometime switched by the
12V relay linked above. Using cheap 5V relays to switch 230vac is a recipe for disaster.

Asking if a flyback diode is really necessary is a red flag because it suggests the individual is inclined to cut corners, which can get you in trouble in electronics. If's one thing if the part is simply unavailable , and something else if you just think it's too much trouble. I don't know which is the case so I will give you the benefit of the doubt but in the future if someone here on the forum recommends something it's probably best to follow their advice. If I misunderstood and you were asking about the
arduino "hanging" as a troubleshooting question then simply disregard the above.
I have found that any arduino running off an AC/DC Wall Wart powered by the SAME AC powering a motor
or other device being switched by the arduino is suseptible to glitches and hanging due to spikes on the
AC during the switching of the load by the arduino. Granted, given a large enough filter cap 1000uF or
greater, on the 5V dc arduino power line, can eliminate this. The fool-proof way to verify this is what is
occurring is to simply disconnect the ac adaptor from the arduino and power the arduino from a 9V
battery plugged into the External Vin Barrel Jack. If it no longer "hangs" when you switch the relay driving
the AC Powered device, then you know the hanging is caused by spikes working there way back to the
arduino 5V line by causing spikes on the ac powering the arduino.
Make sense ?

Does the absence of a flyback diode can hang the arduino.

Short answer is yes it really can.

arduinoaleman:
Just read Grumpy_Mikes s comment.

The first line is the most important.

You can use almost any NPN transistor instead of a MOSFET.

Well, to be precise, you can only use transistors with enough current and voltage rating
for the relay in question.

Just install a 2K2 resistor between the digital output and the base of the transistor.
(emitter is connected to ground / collector is connected to the relais).

No, 2k2 is most times too large a value. Using a transistor as a switch means putting
it into the saturation region, meaning the base current needs to be 5% to 10% of the relay
coil current.

For instance a 100mA relay means 5--10mA base current, which at 5V requires about somewhere
in the 470 to 820 ohms range for the base resistor.

And don't forget the free-wheel diode...

1 Like