[SOLVED] Relay spike problems

Hi,

I have some electrical issues with a circuit and could really use some advice.

I have made an ATmega328 based custom board that controls an ancient motor for an electric garage door. Circuit attached.
The door motor has a builtin controller that starts or reverses the motor when two external connectors are shorted. Just a brief pulse is needed to start the door or to make it reverse if it was already running.
The voltage at the connectors is 24VAC. The continuous current going through when I keep the pins shorted is 700mA. I don't have a possibility to measure any spikes.

What I'm trying to do is to use a small relay to short the connectors on the motor. Relay datasheet: http://documents.tycoelectronics.com/commerce/DocumentDelivery/DDEController?Action=srchrtrv&DocNm=108-98007&DocType=SS&DocLang=EN
It's possible to close the relay briefly to open and close the door just as I want but I'm suffering from spikes that makes the ATmega go crazy from time to time. In some cases it just resets the ATmega but sometimes it triggers my interrupts that should normally only be triggered by a push button (S1 in the schematic). Sometimes it hangs until power is removed.
I'm driving my relay with an NPN transistor, I'm using a flyback diode on the relay coil and I have coupling capacitors on the ATmega and on my voltage regulator but I wonder if it's enough.

Oddly enough I don't even have to trigger the motor using my relay to experience these problems.
If I open or close the door using a manual switch (S2 in the schematics) or if I short the connectors on the motor I experience the same thing.

I suspected that I could be suffering from noise on the power supply because the motor and the wall wart for my Arduino is in the same outlet but I have tried running the Arduino from batteries without any difference.

Any recommendation would be highly appreciated.

I think it's common practice to bypass the ANALOG_REFERENCE pin to ground with a 100nF capacitor. I also use 100nF caps on the 7805 input and output, in addition to the electrolytics. But I'm not real confident those will solve the problem given that it still occurs running on batteries.

What is connected to pin 17?

Thanks, I wasn't aware of that there should be any caps on AREF.
I'll see if adding that and acouple of 100nF caps on the 7805 improves anything. I will also try another power supply just to be sure.
Will post an update tomorrow.

Pin 17 is connected to the data pin of a 433MHz receiver module which I couldn't add to the schematic. It might be easy to create a part for a complete module like that but I'm a complete noob with Eagle.

I wonder whether there's some sort of electromagnetic interference - you have some hefty currents there. Given that you can trigger the problem without the Arduino doing anything, I'd suggest you run it on batteries (to rule out problems on the power supply side) and then see how much of your wiring you need to dismantle to stop the problem from happening. Does it stop when you disconnect the relay? How about when you disconnect the relay wires at the Arduino end?

Presumably if you were to disconnect everything then there's no way a battery powered Arduino would be reset by your garage door opener, but knowing what part of your external wiring is required for the problem to happen will tell you where to look for the problem.

PeterH:
I wonder whether there's some sort of electromagnetic interference - you have some hefty currents there. Given that you can trigger the problem without the Arduino doing anything, I'd suggest you run it on batteries (to rule out problems on the power supply side) and then see how much of your wiring you need to dismantle to stop the problem from happening. Does it stop when you disconnect the relay? How about when you disconnect the relay wires at the Arduino end?

Presumably if you were to disconnect everything then there's no way a battery powered Arduino would be reset by your garage door opener, but knowing what part of your external wiring is required for the problem to happen will tell you where to look for the problem.

You're spot on!
Interference from the wiring itself was in fact the problem. By separating the cables better i could make the problem go away.
The wires from the switch side of the relay did a loop inside my enclosure and was also connected to the external switches and the motor controller using a terminal block inside the enlosure. These wires were obviously affecting a lot of stuff on their way around my box. Removing the wire loop, letting the wires exit the box immediately after the relay and moving the terminal block out of the box seems to have solved the problem.

I wonder if the interference that triggered the interrupt was picked up by the wires between my switch S1 and Arduino D4 or if it affected the chip itself.
Would it have been possible to limit the problem a bit by pulling the D4 pin up a bit "harder" by choosing a lower resistance pull-up?
It wouldn't solve my reset problems and the hangings so it doesn't really matter though but I'm curious. Wouldn't it be possible to use quite a low resistance for the pull-up when you use a switch that connects directly to ground and that definitely will have the time to drive the pin low?

I realize that running all the cables inside the same enclosure as the chip was a bad choice but I've learned something by doing it.

Big thanks to both of you!