VIN 12V supply. Arduino stopping

I'm powering up the arduino mega at pin VIN using an SMPS (12V 4Amps). After I upload the code and disconnect the communication cable. The arduino stops and starts again and again. When I connect back the communication cable, it starts to work again normally. Can someone please help.

What is all connected to the Mega? And how. Please provide a schematic (reasonably size photo (around 300k) of a hand-drawn one is OK).

Do not use "Vin" or the "barrel jack". The on-board regulator is basically useless as you have found.

The logic chips operate on regulated 5 V. Supply regulated 5 V to the "5V" pin and ground. For a Mega2560, UNO or Leonardo, disconnect this when connecting to a PC via USB. For a Nano this is not a problem.

A Mega should work fine with 12volt on the DC socket or V-in.
But... at that voltage you can't draw more than 100mA total from any pin of the Mega.
If you do, then the 5volt regulator overheats and shuts down periodically (if you're lucky).
It all comes down to the question already been asked "What is connected to the Mega".

Any combination of pins including I/O pins and the "5V" pin. Just to be clear. :astonished:

And at 12 V, probably less than 100 mA which means the regulator would be dissipating over 700 mW.

In my experience, with a bare Mega (not in a case) and at room temp, problems start at about 130-150mA (above the ~70mA a Mega itself draws).
Note that a hot regulator is not recommended for a long service life.
Let's see what OP is building.

Thank You guys. Actually I had connected general purpose 5mm LEDs, which were drawing alot of current. Thats why the nano was turning off and on.

Did you forget the resistors?

Yeah.I connected them now(147ohms). They're still very bright. I think I'll have to increase the resistance.

Must keep pin current below 20mA if you want the LED and the pin to have a long life.
But 10mA is often bright enough.
Resistor value also depends on the colour of the LED (LEDs have different working voltages).
There are many LED/resistor tutorials/calculators online.

Not only observe the current per pin, but also the combined current of all pins. While each pin can source something like 20mA (although I wouldn't go even near that much!), the total amount of current you can pull from all pins together is still something like 100mA max. Fortunately Atmega's have fairly good overcurrent protection as long as you don't go too crazy with them.

I'd change those 147R resistors to 510R or so.