Controlling relays with ULN2003

I am new to Arduino Mega2560. For one of my projects I have to control 8 DC motors for direction and another 8 DC motors as pairs for direction. The motors are driven directly by pairs of relays. The relays are driven by ULN2003. The inputs for the ULN2003 are given by the Digital output pins. High on one pin drives a input of the ULN2003 which activates the relay which drives the motor in a particular direction. There are 3 numbers of ULN2003 with a total of 20 inputs from the Mega2560
The motors are driven at 12V from an independent power source fed directly to ULN2003 from that of the Mega which is powered by the USB of my laptop. Now the outputs from the Arduino have been tested and found to be working properly. but when conected to the ULN2003 they just start to oscillate randomly. The Mega2560 hangs up and then the system has to be shutdown and rebooted for it to start working. I thought there is more current being drawn from the output pins of the Mega and maybe they could not drive all the pins of the ULN2003
I have tried adding 20K between the mega2560 and the input of ULN2003 to limit the current but with no success.
To put a long story short I have blown the Mega2560 and am getting a new one. But before I fry another board I would like to know what I am doing wrong. I came across
Loading/stalling DC motor temporarily breaks serial communication to PC over USB
http://arduino.cc/forum/index.php/topic,58217.0.html
There is a reference to adding 0.1uF capacitors to the end of motors. Here I can try adding them to the relay coil as they are both inductive load.

Another solution to limit back EMF would be to add diodes to the digital output of the mega2560 to the ULN2003
Would this be a solution?
but why is the mega hanging up?
Has anybody else faced a simillar problem?
Please help.

Firstly without the circuit diagram we can but guess.

You haven't mentioned flywheel diodes for the relays - you have used freewheel diodes?

What are the relays? How are the relays powered? Are all grounds commoned up?

Dear MarkT
I am sending the circuit diagram i have used as an attachment.
The freewheeling diodes are added as shown though the ULN2003 has its own diode on each output.
The relays are 12V relays driven by 12V supply (Red line)
The grounds are commoned for the arduino mega 2560 also (Blue line) as shown in the diagram.
I thank you in advance.

The way those relays are drawn they won't control the motors, it looks like you either have 12v on both sides or 0v on both sides of the motors.

Not that that would have anything to do with your symptoms.

Are you sure you've blown your Mega, I can't see how you would from that circuit.

add diodes to the digital output of the mega2560 to the ULN2003

I don't see any point in that.

but when conected to the ULN2003 they just start to oscillate randomly.

That's very weird and doesn't make much sense, do you have the chips in the right way? (well it's been known)


Rob

Dear Greynomad,
the relays are conected so that when the input to the ULN2003 is the same for then the relays don't rotate the motor. but when either of the inputs to the ULN2003 is different then one of the relays will conduct to the motor rotating it in one direction. The relays are working almost as a H bridge. Thus I can have two direction for the motor rotation and full stop of the motor when not required. I have tested the circuit independently and it works. It works for al the 20 relays when it is not connected to the mega 2560.
The moment the mega is conected it works fine. but when any of the mega output is high, all the relays start oscillating.
The output from the mega is tested and works fine without the further circuit. It follows according to the software very nicely.
From what I could gather there is either
Too much current drawn from the mega or
There is a back EMF or some parasitic oscillation setup because of the circuit which could not be ascertained.
I could not understand why the mega locks up
My mega 2560 is not being recognised by the computer USB though the green light is switched on.

The way you are wired up, the ULN2003 output is providing the low for the motor, while the relay connects the 12V.
I think you'd have better results connecting the bottom right pins of the relays to ground directly and not to ULN2003. Kee the bottom left pin connected to 12V.
When the relay is unenergized (ULN2003 output is high), both leads have +12V, thus no rotation.
When R9 energises, current flows 'up' thru the motor. When R10 energises, current flows 'down' thru the motor. Thus you are not relying on the ULN2003 to sink the current thru the motor. Your parts will be much happier that way.
As it is, the poor ULN2003 is trying to sink both the coil current to energize the relay, and the motors current.

Are you sure the 12v PSU is up to the job of running all the relays and motors? Oscillating relays is often a sign of a bad supply.

Do this all still happen with no motors attached?

Too much current drawn from the mega

Those 2003 inputs would only draw about 1.2mA I think so that should be OK.

EDIT: Are you sure about that CR, I thought the schematic looked right, albeit very confusingly drawn.


Rob

Okay, maybe not 100% - I didn't see the ground connection to the top of the relay - the schematic is too big and takes a lot of scrolling around to see it.

The oscillations starts even if the motors are not connected.
I have even tried to run only the relays through the ULN2003, Even only one ULN2003 at a time. It has not helped.
I have tried to change the connections to the relay as suggested by Dear Crossroads to no avail.
I have even tried to invert the outputs from the mega through a 74LS04 to create a barrier between the mega and the ULN2003 and changed the digital outputs for the same through the software to take care of the inverted inputs to the ULN2003. But no luck.
I have even tried to run the motor directly from another source through the relays. There can be no current dropings because I have tried to give the supply through a 32A 12V battery.
The current drawn for the mega is from a USB of my laptop. I have even tried to give a 7V supply to the mega board through the power jack.

Post your code then - maybe something funny is going on there.