I'm pretty stumped here, tho I'm kind of a novice with electronics...
I've read in several places about how to activate a relay with the Arduino digital output, using a transistor, etc. but I went ahead and did it the "wrong way", I guess. Here's what I've done: http://flickr.com/photo_zoom.gne?id=414926843&size=o or:
The relay switches a motor, 12V, .68A back and forth by reversing its polarity.
The relay coil is 5V, 140mW, so it draws only about 30mA, which is why I went right from the ATMEGA8's digital output to the relay. With no motor attached it works fine... click, click, click goes the relay. However when I hook up the motor the behavior is erratic... For a few cycles it works fine, then it skips a few, then it doesn't work at all! Then I removed the motor and it still didn't work. I figured I fried something, but after re-burning the bootloader and downloading the program again it worked fine (with no motor of course).
Is the fix to simply add a diode across the relay coils, or do I need to add the full transistor deal, etc.?
take out the optoisolator as used in the schematic, as it is not doing anything useful. It's like an existential optoisolator, waiting to be given a useful task
The relay's inductive load cannot be driven directly by the Arduino's pins. You need a transistor amplifier. Rebuild what you have using the diagram here.
you don't need an extra voltage regulator for the relay/motor power supply-- just use an external power supply appropriate tot he motor/relay voltage.
So is the 'inductive load' part of it the problem? Despite the current requirement being low enough for the output pin? And why would the circuit work without the motor attached, and then not work when it's attached? The relay coils shouldn't care if they're switching a load or switching nothing, should they?
ps- I tried using diodes across the relay coils, still no good.
Regarding the relay, you simply cannot drive a relay directly from a normal I/O pin of a processor... it's just not done, and it is also really bad design practice Check, for example, the standard design here that uses a similar transistor amplifier to drive a relay.
The reason you need a transistor is twofold. First, the relay is an inductive load and will throw back all kinds of messy stuff to the microcontroller, which will contribute to instability and/or or damage. Relays create voltage spikes of potentially 100's of volts momentarily when their field collapses.
Second, microcontroller pins are made to source only a few ma, after that you are asking for trouble.
The simple and usual solution to the noise problem and current drive requirements is to use the transistor amplifier diagram linked here,as for only ten cents you relieve the loading on the processor, give the relay all the current it needs, and use the transistor junction and the base relay to isolate the noisy relay from the digital pin that's driving it.
crap... I breadboarded the fix -- including a transistor between each digital output and the relay, and a diode across each relay coil -- and I'm getting the very same behavior. With no motor attached it works great, when I attach the motor the ATMEGA8 behaves erratically then not at all.
I wonder if it has to do with the power supply. The ATMEGA8 and the motor share a common ground, maybe that has something to do with it. I tried putting a diode between the micro's ground and the common ground and I think I fried something. Hmm.
usually when you start a motor or a coil there is a power surge that drops the voltage for a brief moment but long enough to freak out the microcontroller
the solution is usually to place a few capacitors in the circuit
100uF near the power pins of the microcontroller tends to help