8

After looking into various schematics on controlling a relay via an Arduino, I have noticed that, most of the time, transistors are used to switch the separate supply into the relay coil rather than directly supplying the relay with the 5-Volt output from the I/O pin of the Arduino. For example, I have a 5-volt DPDT relay and also a small dc motor. I want to drive both directly from my Arduino UNO (SMD clone) with an Atmel328 processor? Would it be advisable to proceed?

If not (most likely):

  1. Can someone give a detailed explanation and might as well important cautions on current limits and stuff?

  2. How can one control such components without risking the Arduino? What are some common means to accomplish this?

  3. What other devices can generally harm an Arduino board (or any microcontroller unit) in a similar manner?

I'm just a beginner who wants to be extremely cautious. Thanks.

  • Although recently, with my first encounter with Arduino, I used the I/O pins directly to energize a toy dc motor and change its direction by turning a DPDT relay on and off in the same manner. Nothing seems to be amiss by far. – DorkOrc Oct 20 '15 at 7:50
8

Processor pins have strictly limited current drive capability.

  • The rated values may vary depending on whether you want a high or low drive.
  • Some processors will supply only a few mA and the most you will usually officially get is in the 20 to 30 mA range.

  • There is usually a total current limit for the processor and only a few pins can provide peak current simultaneously.

  • Processor pins have significant effective resistance and a high voltage will "drop" as increasing current is drawn and a low voltage will rise as load increases. Pins MAY be specific with a maximum short circuit current but at that point a high pin will be pulled low and a low pin will be pulled high so short circuit current has limited applicability.

Even if you have a say 25 mA per pin rated processor the power available is small. 25 mA · 4V say (1V drop on 5V Vcc) = 100 mW. Most motors will take more than that and only very small motors will run well when powered only by a pin.

Electric motors and inductors will generate significantly high voltages when current is interrupted - voltages of tens of volts can easily result and 100+ volts can happen. Connecting a motor of inductor to a processor pin directly is an invitation to destruction. Murphy will often oblige.


A transistor (bipolar or MOSFET) that will drive typical hobby motors costs 10's of cents (or free from scrapped equipment) and allows the port pin current drive to be buffered and "amplified". Use of a transistor or other buffer is an extremely good idea if you have one or a few processors and do not wish them to die semi-randomly.

MOSFET motor driver - from here - section 8.

Voltages and part numbers are for their example - select to suit.
A NPN biploar could be used with the addition of an input resistor to the transistor base.

enter image description here

Bidirectional driver - if you want the driver to be able to drive a load high and low this circuit will work. from here
The input gate is in this case the internal processor driver. The two MOSFET gates connect directly to the processor pin. Vdd should usually be no higher than processor Vmax_drive_out. Slightly higher can be made to work with suitable design. Much higher voltage loads can be driven with this circuit (or similar) plus one extra transistor.

enter image description here


A buffer such as a ULN2803 (and other family members) will drive 8 channels x 500 mA/channel and several can be paralleled.


A ULN2803 is essentially 8 x "Darlington" transistors with emitters connected to a common ground, 8 x "open collector" (unconnected) collectors, and 8 flyback diodes to deal with over voltage spikes (use optional). (There is a ULN280x family with slightly different input characteristics).

This device provides a reasonably priced means of providing 8 x 500 mA pull down drivers. A load connected from an output to V+ is turned on when the input pin is driven high. Once you've used one a few times you'll find them trivially easy to use and very useful. (There is also a ULN200x family with 7 channels per package).

YouTube "how to" video


Driving a stepper motor

enter image description here

Also here


Driving small DC motors - and quite a lot else.

enter image description here A zillion examples

Digikey - available in 1's if desired $0.72/1, $0.29 in 1000's.

ULN2803 datasheet


For sale at Sparkfun - can be had rather cheaper but these are available

7

The recommended output (source or sink) from an I/O pin is 20 mA. The absolute maximum is 40 mA. Your relay coil is likely to consume more than that, particularly when it initially energizes. This will damage your output pin. Then it will eventually fail.

Nothing seems to be amiss by far.

No, not yet. :)

How can one control such components without risking the Arduino? What are some common means to accomplish this?

Use transistors or MOSFETs.

What other devices can generally harm an Arduino board (or any microcontroller unit) in a similar manner?

Anything that exceeds the maximum voltage or current limits as documented in the datasheet. Coils (eg. in relays and motors) in particular are likely to have a high reverse voltage when they are turned off, which is why you need a snubber diode.

Would it be advisable to proceed?

It would be advisable to take heed of what I wrote above, and to read the many, many articles on the Web about how to drive motors and relays from an Arduino. You are not the first person to attempt this.

4

Have a look at the simple circuit on this PDF from the Arduino Playground. It shows a single transistor to drive a small relay.

As Russell says in his answer, a ULN2803 or similar is a chip which will allow you to drive several small relays, which is neater than using several transistors, if that's what you want.

(Also note the diode "D1" in the circuit I linked - you need this, it's to protect the transistor from eventually being damaged by inductive spikes generated when the relay turns off. Some of the ULN-style chips have this diode built in, which is why you don't always see it in schematics.)

3

To really protect your arduino it's worth putting a photo-coupler on the pin and driving your circuit that way. Then no stray inductive charge or short can affect the arduino.

They are also called OptoIsolators or Optocouplers.

http://forum.arduino.cc/index.php?topic=143954.0

  • 1
    You're quite right, optoisolators are a great way to go. Another way to do it that's slightly less isolating but still handy is to use an Operational Amplifier (Op Amp) as a buffer. These are also called 'folloer' Op Amps. Note that you can do this with any off-the-shelf OpAmp, you just lay the circuit out a particular way. en.wikipedia.org/wiki/Buffer_amplifier#Op-amp_implementation – Dan Oct 20 '15 at 23:23
3

Let us look as some basic's, these seem to be the basis of a lot of questions:

A capacitor when power is switched on draws a huge amount of current that tapers off as it gets charged. This curve is also called the RC time constant (this is close but not exact "http://www.electronics-tutorials.ws/rc/rc_1.html" will give a better explanation).

When switching off a capacitor discharges at an exponential rate (RC time constant) depending on the value, charge voltage and the load. This makes them good for holding power for a short time when the power fails.

An Inductor draws nothing when first switched on but the current increases exponentially until its voltage reaches the supply voltage.

When switched off the inductive field in the inductor collapse causing the polarity to reverse. The voltage will rise unlimited until typically something externally limits it. The faster it is turned off the quicker the rise time and voltage. Energy will cease to flow when the inductive charge is dissipated. Guess where this current goes when the inductive load such as a relay is connected to a port pin?

For this reason you need to put a diode (commonly called a fly wheel diode) across the inductive load. Google for: "inductor/capacitor charge curve" you will find a lot of nice graphs explaining this. If you look at the circuit it has the cathode + connected to the most positive side of the power supply. In this configuration it will not conduct unless the voltage is reversed (when the inductive load is switched off).

Another common misconception is you can load a microprocessor I/O to the maximum. This is bad design. They give you a maximum per pin, per port and for the chip. At room temperature you will probably get away with it for a while.

Let us assume we have a port with a 40mA load. The output is 0.005 from the power rail. Using Ohm’s law we are dissipating 20 milliwatts of power on one pin. At this rate of loading it does not take long to over heat the device because of internal power dissipation.

When the output pin is changing state it draws more current because it has to charge or discharge its internal and the external capacitance, ‘more heat’, more speed ‘more heat’.

If you look some of the specs will give you a maximum temperature, it is for the junction on the die, not the case temperature. Plastic is a poor conductor so heat sinking the package does not do a lot. Now consider this along with the ambient temperature. The ratings are given typically with the device at 25C, guess what happens when it gets warmer.

Have fun,

Gil

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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