Arduino code not running when USB disconnected

I have two arduino mega2560 and using the basic master-slave codes

The code work perfectly fine as long as the both arduino boards are remain connected to USB, after uploading their codes.

Issue 1: When I remove the USB from master-arduino and power the board with external power, the slave-arduino stop receiving data (as seen in serial monitor) - very STRANGE! or is it the master-arduino stopped sending data!

Issue 2: I understand that the above code requires slave-arduino to stay connected to USB. So, I changed the code a little, instead of serial write, it will now set a digital pin (say 22) high when receive data from master. After uploading the code, I remove the USB and power the slave-arduino through power plug. I found that, its not receiving any data even when master-arduino is connected to USB!

My ultimate aim is to have both the arduinos powered by external power-plug (I am using 9V battery) and can work as master-slave (without the USB plugged in).

Would really appreciate any help.

thank you.

Did you connect the grounds?

Hi,

My ultimate aim is to have both the arduinos powered by external power-plug (I am using 9V battery) and can work as master-slave (without the USB plugged in).

I hope not one of these?

Do you have a DMM to measure some voltages?

Tom... :slight_smile:

If the above does not solve the issue, you will have to show your sketches.

I've just tested it using two Unos and it works perfectly. (modified slave code to toggle a LED in the slave event). Master powered via barreljack with 12V, slave powered via USB with a cellphone charger. Next powered the slave from the master and that also worked.

TomGeorge:
Hi,I hope not one of these?

Do you have a DMM to measure some voltages?

Tom... :slight_smile:

I have noticed this repeatedly but I have no idea: what is wrong with this battery?

Hi
This battery cannot provide the current you require.

Do you have a DMM to measure some voltages?

Thanks.. Tom... :slight_smile:

Thank you all for your reply, specially sterretje and Coding Badly

I found that connecting both arduino grounds is the key part. :cold_sweat:

It works fine with 9V battery. :grinning:

laskar:
It works fine with 9V battery. :grinning:

For a little while.

laskar:
Thank you all for your reply, specially sterretje and Coding Badly

I found that connecting both arduino grounds is the key part. :cold_sweat:

It works fine with 9V battery. :grinning:

What do you mean by connecting both grounds? The GND pins on the left and right of the Arduino?

The ground of the one Arduino needs to be connected to the ground of the other Arduino.

When connecting two Arduinos to a PC via USB, those connections provide the common ground through the PC.

If one of them (or both) is not connected to the PC, the common ground is no longer there and you have to create one.