Master reset for an Arduino Mega.

Hi,

I have an occasional (but very annoying) error with arduinos that suddenly refuse to accept a new upload. What seems to happen is this, occasionaly I will produce a dodgy sketch that will run maybe the first couple of lines, and then 'reboot' and start again. The first line is normally something like Serial.println("starting sketch abcd"); so what I see when I monitor the serial port is just the phrase "starting sketch abcd" re-cursing up the screen ad-infinitum. Not a big deal in itself, but the problem is that this recursing code seem to lock the arduino and somehow prevent a working sketch from uploading. If I fix my sketch (or find a sketch known to work) and attempt an upload, then the sketch will compile ok, start uploading and then just hang before eventually producing a time-out error if I leave it long enough. There seems to no cure except for infinite patience, after maybe a couple of dozen attempts, constant unplugging and re-plugging of the arduino, or maybe even a re-boot of the computer then I eventually get lucky and the uploads start to work again. Since this has happened on both my arduinos I guess this is some sort of system error rather than just a faulty unit.

So, my first question is is my diagnosis correct, is the recusive re-booting of the arduin preventing it accepting uploads, and if so, is there a cure for it?

Thanks

The thing here is to find out what is causing the recursive rebooting.

It is a feature of the Arduino that when the Serial port is opened it causes the MCU to reboot. This design is so when you want to upload a new sketch the MCU is rebooted and this runs the bootloader that allows the upload of a new sketch. If some software on the PC is continually scanning the serial ports and opening/closing them then this would continually cause reboots but should not really effect sketch uploading.

Another problem could be if you have enabled the watchdog timer and the mega bootloader is an old version that does not disable it to upload sketches, thus the watchdog times out and resets.

The problem could also be hardware related. Maybe the Mega is slightly damaged, Attached sensors/peripherals are drawing to much current or a faulty USB cable/port on mega of PC.

Try a different USB cable and USB ports on the PC. Try a different PC if possible and let us know what happened.

Here's a few possible solutions:

  1. Press & Hold the reset button, when the IDE shows "compiled xxx of 128xxx bytes" release it. This will keep the Mega from flooding the PC with messages too soon. Can be easier to get the release timing down if you have Verbose outputs enabled under File:Preferences.

  2. Put something in your code to delay the sending of startup messages.

  3. Read an unused pin, keep the code from proceeding to the startup messages until the button is pressed.

thanks guys,

The current issue has now resolved itself, I will bear some of these tips in mind next time the problems occur.

The thing here is to find out what is causing the recursive rebooting.

This is quite a simple really - cr4p coding on my part. The problem is that once the arduino gets trapped in this loop I can't over-write it with a working sketch.

One of the problems is that the issue is very erratic - sometimes it cause big problems, other times it just sorts itself out, seemingly without any changes from me. So for example I tried swapping the USB lead, and suddenly it worked! - but then 2 minutes later the problem re-occurred, so obviously the USB lead wasn't the problem after all.

Maybe ............... Attached sensors/peripherals are drawing to much current

There may be something in this. The board has an SD card and a GPS module on it, when I remove them the uploads seem (slightly) more reliable.

I believe that the peripherals are all working ok and wired correctly, since the project works fine once the code is finally loaded. So I guess the upload of new code to the arduino takes more current? and if the peripherals are also drawing a lot of current then there's a chance of the USB port being overloaded?

SD card needs more 3.3V current than the regulator can supply. Should not matter when it is inactive during a download (assuming it's CS/ line is pulled high), but when running will be more reliable if a 250mA 3.3V supply is available. 150mA from Mega is insufficient.

Only get 500mA from USB. SD + GPS + Mega (2560, 16U2, power LED) will be pushing the limit there.

SD card needs more 3.3V current than the regulator can supply.

Thanks, I've already learnt this lesson! The SD card is on a shield that supplies the extra current from it's own 3.3v regulator.

BigusDickus: The board has an SD card and a GPS module on it, when I remove them the uploads seem (slightly) more reliable.

If you remove all attached devices so you have just the (vanilla) Mega do you still have problems uploading? Slightly more reliable would mean you still have not found the real problem. If you can, run a new copy of the IDE on another PC with a new/different USB cable and try uploading to the vanilla Mega. If it still fails then it's looking like a Mega hardware/firmware fault. If it's okay then try your usual USB lead to eliminate that and so on until the real culprit is found.