Cannot upload to Arduino Mega 2560 R3 via USB, only via ICSP

Hi

I have a Arduino Mega 2560 R3 that was not used for a long time. I found it in a box yesterday and decided to use it again after a year or so.

My case seems very strange, I never saw something like this.

My Arduino does work perfectly when I flash a sketch using the ICSP/avrdude, the Serial input/output works just fine, digital ports, PWM, RX/TX blinking, everything. But when I try to flash a sketch using the USB, it does not work. I receive a lot of timeouts, my board lights up the built-in led and then blinks RX once. Then avrdude outputs this and the board resets:

avrdude: Version 6.1, compiled on Nov 18 2020 at 19:22:35
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2014 Joerg Wunsch

         System wide configuration file is "/home/pi/Downloads/arduino-1.8.13/hardware/tools/avr/etc/avrdude.conf"
         User configuration file is "/root/.avrduderc"
         User configuration file does not exist or is not a regular file, skipping

         Using Port                    : /dev/ttyACM0
         Using Programmer              : wiring
         Overriding Baud Rate          : 115200
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_getsync(): timeout communicating with programmer

avrdude done.  Thank you.

An error occurred while uploading the sketch

The same happens using windows. The serial monitor works just fine.

There's also one more curious fact. When I flash a sketch using ICSP, it works just fine. One time. If I try to flash again, avrdude shows the following output:

avrdude: Version 6.1, compiled on Nov 18 2020 at 19:22:35
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2014 Joerg Wunsch

         System wide configuration file is "/home/pi/Downloads/arduino-1.8.13/hardware/tools/avr/etc/avrdude.conf"
         User configuration file is "/root/.avrduderc"
         User configuration file does not exist or is not a regular file, skipping

         Using Port                    : unknown
         Using Programmer              : linuxgpio
         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 : linuxgpio
         Description     : Use the Linux sysfs interface to bitbang GPIO lines
         Pin assignment  : /sys/class/gpio/gpio{n}
           RESET   =  17
           SCK     =  27
           MOSI    =  22
           MISO    =  18

avrdude: AVR device initialized and ready to accept instructions

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

avrdude: Device signature = 0x1e9801
avrdude: safemode: lfuse reads as FF
avrdude: safemode: hfuse reads as D8
avrdude: safemode: efuse reads as FD
avrdude: reading input file "./sketch_nov18b.ino.mega.hex"
avrdude: input file ./sketch_nov18b.ino.mega.hex auto detected as Intel Hex
avrdude: writing flash (5476 bytes):

Writing | ################################################## | 100% 1.15s

avrdude: 5476 bytes of flash written
avrdude: verifying flash memory against ./sketch_nov18b.ino.mega.hex:
avrdude: load data flash data from input file ./sketch_nov18b.ino.mega.hex:
avrdude: input file ./sketch_nov18b.ino.mega.hex auto detected as Intel Hex
avrdude: input file ./sketch_nov18b.ino.mega.hex contains 5476 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 2.07s

avrdude: verifying ...
avrdude: verification error, first mismatch at byte 0x02a6
         0xa8 != 0xaa
avrdude: verification error; content mismatch

avrdude: safemode: lfuse reads as FF
avrdude: safemode: hfuse reads as D8
avrdude: safemode: efuse reads as FD
avrdude: safemode: Fuses OK (E:FD, H:D8, L:FF)

avrdude done.  Thank you.

As you guys can see above, I'm using the RPi GPIOs to write to the Arduino through the ICSP. The loopback test does not work. The board does not answer. I've already flashed the atmega16u2 and the atmega2560 using the files that come with the Arduino IDE.

What is happening with my board? :frowning:

When you flash a sketch using ICSP, it erases the bootloader. After doing that, in order to go back to uploading over USB, you need to replace the bootloader by using the Arduino's Tools > Burn Bootloader feature.

pert:
When you flash a sketch using ICSP, it erases the bootloader. After doing that, in order to go back to uploading over USB, you need to replace the bootloader by using the Arduino's Tools > Burn Bootloader feature.

I know, I burnt the bootloader using ICSP and the firmware into the atmega16u2 using the other ICSP, then I tried to send a sketch through USB and it didn't work. The RX blinks one time and then I get a lot of timeouts, followed by a avrdude: stk500v2_getsync(): timeout communicating with programmer

Windows/Linux still recognizes the USB port and I'm able to use the Serial to read/write though.