Arduino Mega 2560 doesn't write

I recently got in my hands an Arduino Mega 2560 from a friend. He told me that it got the sync error, so I reflashed it with ArduinoISP, using my Uno as programmer. Then after the bootloader is burnt I can write, but only one sketch. If i want to write another one I must reburn the bootloader and then I can write another sketch. Could it be something on the 16U2?

2 possible things that cause upload through the bootloader to work only once:

The auto reset circuit does not work. If you open the Serial Monitor while it is connected, or if you try to upload a sketch, do you see that the reset happened? The pin 13 LED should blink when the Mega 2560 is reset, just int the same way it does when you tap the reset button. You can tap the reset button at the beginning of upload instead of relying on the auto reset circuit.

Or

When you burn the bootloader you are setting the high fuse to 0xD9 because you have altered the boards.txt file at some point in the past to allow loading sketches via ISP. The default high fuse setting is 0xD8, which allows the bootloader to run upon reset.

I think is the circuit, or the 16U2. The fuse can't be, I haven't changed nothing from there (neither I know how to do it). But not even hitting the reset button it does anything

I just realized the Mega bootloader does not blink the LED upon reset the same way an Uno does. Go ahead and burn the bootloader, then upload the blink sketch. With the blink sketch running, observe that the one second on and one second off blink pattern is interrupted when you tap the reset button on the Mega. When you try to upload another sketch or when you open serial monitor, you should see that same interruption to the blink pattern. That will show you if auto reset is working.

You may need to try several times if the interruption is not noticeable. It is easier to notice an interruption if you modify the blink sketch to blink faster, like this:

  digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(100);              // wait for .1 second
  digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
  delay(100);              // wait for .1 second

If auto reset is working, in the IDE preferences check show verbose output on upload. Then burn the bootloader and look at the output shown to you in the IDE to verify it actually used the high fuse value 0xD8.

C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avrdude -CC:\Program Files (x86)\Arduino\hardware\tools\avr/etc/avrdude.conf -v -patmega2560 -cstk500v1 -PCOM7 -b19200 -Uflash:w:C:\Program Files (x86)\Arduino\hardware\arduino\avr/bootloaders/stk500v2/stk500boot_v2_mega2560.hex:i -Ulock:w:0x0F:m 

avrdude: Version 6.3, compiled on Sep 12 2016 at 17:24:16
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2014 Joerg Wunsch

         System wide configuration file is "C:\Program Files (x86)\Arduino\hardware\tools\avr/etc/avrdude.conf"

         Using Port                    : COM7
         Using Programmer              : stk500v1
         Overriding Baud Rate          : 19200
         AVR Part                      : ATmega2560
         Chip Erase delay              : 9000 us
         PAGEL                         : PD7
         BS2                           : PA0
         RESET disposition             : dedicated
         RETRY pulse                   : SCK
         serial program mode           : yes
         parallel program mode         : yes
         Timeout                       : 200
         StabDelay                     : 100
         CmdexeDelay                   : 25
         SyncLoops                     : 32
         ByteDelay                     : 0
         PollIndex                     : 3
         PollValue                     : 0x53
         Memory Detail                 :

                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           eeprom        65    10     8    0 no       4096    8      0  9000  9000 0x00 0x00
           flash         65    10   256    0 yes    262144  256   1024  4500  4500 0x00 0x00
           lfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           hfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           efuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           lock           0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           calibration    0     0     0    0 no          1    0      0     0     0 0x00 0x00
           signature      0     0     0    0 no          3    0      0     0     0 0x00 0x00

         Programmer Type : STK500
         Description     : Atmel STK500 Version 1.x firmware
         Hardware Version: 2
         Firmware Version: 1.18
         Topcard         : Unknown
         Vtarget         : 0.0 V
         Varef           : 0.0 V
         Oscillator      : Off
         SCK period      : 0.1 us

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.02s

avrdude: Device signature = 0x1e9801 (probably m2560)
avrdude: NOTE: "flash" memory has been specified, an erase cycle will be performed
         To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: reading input file "C:\Program Files (x86)\Arduino\hardware\arduino\avr/bootloaders/stk500v2/stk500boot_v2_mega2560.hex"
avrdude: writing flash (261406 bytes):

Writing | ################################################## | 100% 0.00s

avrdude: 261406 bytes of flash written
avrdude: verifying flash memory against C:\Program Files (x86)\Arduino\hardware\arduino\avr/bootloaders/stk500v2/stk500boot_v2_mega2560.hex:
avrdude: load data flash data from input file C:\Program Files (x86)\Arduino\hardware\arduino\avr/bootloaders/stk500v2/stk500boot_v2_mega2560.hex:
avrdude: input file C:\Program Files (x86)\Arduino\hardware\arduino\avr/bootloaders/stk500v2/stk500boot_v2_mega2560.hex contains 261406 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.00s

avrdude: verifying ...
avrdude: 261406 bytes of flash verified
avrdude: reading input file "0x0F"
avrdude: writing lock (1 bytes):

Writing | ################################################## | 100% 0.02s

avrdude: 1 bytes of lock written
avrdude: verifying lock memory against 0x0F:
avrdude: load data lock data from input file 0x0F:
avrdude: input file 0x0F contains 1 bytes
avrdude: reading on-chip lock data:

Reading | ################################################## | 100% 0.01s

avrdude: verifying ...
avrdude: 1 bytes of lock verified

avrdude done.  Thank you.

This is the progress of the burning process. I'm going to try the blink program thing you told me.

EDIT: I just used an script to read values and on HFuse it reads 0xD8, so maybe that's correct, too.

That looks like the second part of the burn bootloader, and no fuses were set during that avrdude command. The first avrdude command is the one that has the fuses.

dmjlambert:
That looks like the second part of the burn bootloader, and no fuses were set during that avrdude command. The first avrdude command is the one that has the fuses.

I used for a second time the process on Gammon Forum : Electronics : Microprocessors : Atmega bootloader programmer to try, but still nothing. It puts the HFuse correctly, and I can see it configured as it should be, I think.

Atmega chip programmer.
Written by Nick Gammon.
Version 1.37
Compiled on Oct 15 2016 at 03:15:37 with Arduino IDE 10612.
Attempting to enter ICSP programming mode ...
Entered programming mode OK.
Signature = 0x1E 0x98 0x01 
Processor = ATmega2560
Flash memory size = 262144 bytes.
LFuse = 0xFF 
HFuse = 0xD8 
EFuse = 0xFD 
Lock byte = 0xFF 
Clock calibration = 0x9F 
Bootloader address = 0x3E000
Bootloader length = 7474 bytes.
Type 'Q' to quit, 'V' to verify, or 'G' to program the chip with the bootloader ...

Which version of the Arduino IDE are you using?

Newest, 1.6.12

What are your findings regarding whether or not the auto reset works?

dmjlambert:
What are your findings regarding whether or not the auto reset works?

I tried again after reflashing the bootloader to run a loopback and now it works, but still, I've got to do this process:

1.- Reburn bootloader on the Mega.
2.- After the process is done, I can write a sketch on the board.
3.- If I try to write another sketch, it throws me the error, but after last night reburn of the bootloader if I am quick pressing the reset button it writes the sketch.

EDIT: Something I've done to try was changing COM port on Device Properties, putting the Mega from COM6 to COM2 and it lets me write a Sketch, but then I can't write anymore. ¿Could it be something with the drivers or the virtual ports?

OK, so perhaps auto reset does not work. You can live with it the way it is and press reset manually to upload, or get another board, or troubleshoot further to figure out more and perhaps get into the SMD soldering world. There are a few components that could be bad and causing a problem, or the trace could be broken. There is a RESET-EN jumper that was made specifically to be scratched through to disable the auto reset, and to restore the auto reset all you would do is put a small wire or solder blob across the scratched trace. The 0.1 uF capacitor between the DTR line of the ATmega16U2 and the reset line of the ATmega2560 turns the DTR signal to a momentary pulse. Perhaps something is wrong with that component. Study the Mega 2560 schematic.

You can make a logic probe out of a resistor (~220 ohm?) and an LED. Connect the resistor to 5V, other side of resistor to the anode of the LED, and use the cathode of the LED to probe and see what the DTR signal is doing. If it goes low and stays low as indicated by the LED lighting up when you open serial monitor, it is OK. Move the probe to the other side of the 0.1 uF capacitor, and you should see the LED only flash briefly when you open serial monitor. Probe your Uno to see how it works, and compare your findings to the Mega 2560. The auto reset circuit works the same way on both boards.

If you suspect the DTR pulse is there but it is weak, you could put a 15K or 10K resistor between GND and RESET on the Mega and see if it fixes the problem.

You could re-flash the ATmega16U2 in case you suspect the firmware on it. I don't strongly suspect the firmware since it does work to do an upload. If the DTR line is not going low at all, I suppose it could be the firmware. The firmware hex file is in one of the subdirectories of the IDE installation, a directory called "firmwares" and you can upload it using Arduino as ISP and the ICSP header near the USB connector.

There are lots of things you can do to test and troubleshoot, the above is just the tip of the iceberg. I don't know how much experience you have with electronics. If you don't want to get into it, that is understandable, since you have a workaround pressing the reset button and since a knock-off brand of Mega 2560 board would only be about $10.

pert was interested in what version of the IDE you are using. I have seen weird things before with software and driver versions, and you just never know what sort of thing you will find fixes problems like this. Some people use an older version of the IDE such as 1.0.6 or 1.6.5 with an older design board such as the Mega 2560, because they find it works better. 1.0.6 and 1.6.5 are particularly stable versions of the IDE and known to work with the Mega 2560. If you have a different computer to try it with and a different USB cable, that also couldn't hurt.

I'm going to give it a try to the 1.6.5 and 1.6.0 IDE and I'll tell you, but maybe seeing that using the Reset button does the job, maybe I'll stick with that if the older IDE doesn't solve anything.

It didn't solve anything, but something with the verbose output I noticed, is that when I upload an sketch on the Uno, there's a Line that says "Using Programmer: arduino", and on the Mega, it says "Wiring". ¿Could it be something?

I don't know, but I am guessing probably not. I have noticed the same thing on my boards. I don't think you can find out anything definitive without following the traces and components and doing the logic probe thing.

I was asking about the IDE version because there were some issues with resets on Windows with IDE 1.6.8.

Maybe is the RESET EN, seems that is cut (My Uno doesn't have it cut), so maybe with a small wire I can do the job, right?

Yes, just a small piece of wire soldered across it, or a solder blob, or find surface mount jumper pins with a jumper shunt, so you can disable auto reset whenever you wish.