AtMega2560 - verifying failed

Hello everybody,

I noticed a strange behavior on an AtMega2560.

I made my own PCB with the microcontroller and stuff like RTC, SDreader, relays…
And for flashing using the ArduinoIDE I’m using an Arduino UNO board where I removed the microcontroller - just using it as USB/serial converter (first I was using FTDI adapters…but as they claimed fakes to destroy…I stopped using FTDI).

One of my PCBs has a strange behavior.
Sometimes uploading the code seems to work fine, sometimes I get an verification issue:

avrdude: Version 6.3, compiled on Dec 16 2016 at 13:33:19
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2014 Joerg Wunsch

         System wide configuration file is "C:\ProgrammeKonsistent\arduino-1.8.1\hardware\tools\avr/etc/avrdude.conf"

         Using Port                    : COM16
         Using Programmer              : wiring
         Overriding Baud Rate          : 115200
         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 : Wiring
         Description     : Wiring
         Programmer Model: AVRISP
         Hardware Version: 15
         Firmware Version Master : 2.10
         Vtarget         : 0.0 V
         SCK period      : 0.1 us

avrdude: AVR device initialized and ready to accept instructions

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

avrdude: Device signature = 0x1e9801 (probably m2560)
avrdude: reading input file "C:\Users\cysig\AppData\Local\Temp\arduino_build_827033/flycontrol_15.ino.hex"
avrdude: writing flash (3580 bytes):

Writing | ################################################## | 100% 0.57s

avrdude: 3580 bytes of flash written
avrdude: verifying flash memory against C:\Users\cysig\AppData\Local\Temp\arduino_build_827033/flycontrol_15.ino.hex:
avrdude: load data flash data from input file C:\Users\cysig\AppData\Local\Temp\arduino_build_827033/flycontrol_15.ino.hex:
avrdude: input file C:\Users\cysig\AppData\Local\Temp\arduino_build_827033/flycontrol_15.ino.hex contains 3580 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.43s

avrdude: verifying ...
avrdude: verification error, first mismatch at byte 0x076c
         0xa9 != 0xe9
avrdude: verification error; content mismatch

avrdude done.  Thank you.

And the same board:

avrdude: Version 6.3, compiled on Dec 16 2016 at 13:33:19
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2014 Joerg Wunsch

         System wide configuration file is "C:\ProgrammeKonsistent\arduino-1.8.1\hardware\tools\avr/etc/avrdude.conf"

         Using Port                    : COM16
         Using Programmer              : wiring
         Overriding Baud Rate          : 115200
         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 : Wiring
         Description     : Wiring
         Programmer Model: AVRISP
         Hardware Version: 15
         Firmware Version Master : 2.10
         Vtarget         : 0.0 V
         SCK period      : 0.1 us

avrdude: AVR device initialized and ready to accept instructions

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

avrdude: Device signature = 0x1e9801 (probably m2560)
avrdude: reading input file "C:\Users\cysig\AppData\Local\Temp\arduino_build_827033/flycontrol_15.ino.hex"
avrdude: writing flash (3688 bytes):

Writing | ################################################## | 100% 0.61s

avrdude: 3688 bytes of flash written
avrdude: verifying flash memory against C:\Users\cysig\AppData\Local\Temp\arduino_build_827033/flycontrol_15.ino.hex:
avrdude: load data flash data from input file C:\Users\cysig\AppData\Local\Temp\arduino_build_827033/flycontrol_15.ino.hex:
avrdude: input file C:\Users\cysig\AppData\Local\Temp\arduino_build_827033/flycontrol_15.ino.hex contains 3688 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.45s

avrdude: verifying ...
avrdude: 3688 bytes of flash verified

avrdude done.  Thank you.

Only a little difference in the code makes this behavior repeatable:

#include <LiquidCrystal_I2C.h>
#include <sdelay.h>

LiquidCrystal_I2C lcd(0x27,20,4);

void setup()
{
    lcd.begin();
    lcd.clear();
    lcd.display();
    lcd.clear();
    lcd.backlight();
}

void loop()
{
    for (int i=0; i<5000000; i++)
    {
        lcd.display();
        lcd.backlight();
        lcd.setCursor(1,1);
        lcd.print(i);
        delay(500);
        
        lcd.noDisplay();
        lcd.noBacklight();
        sdelay(500);
    }
}

Using sdelay(); at the end it uploades fine. Using delay(); I get the verification error.
But: Using sdelay(); I get restarts everytime the microcontroller wakes up after sdelay();. Using delay(); I can see the display turning on and increasing the number.

So what might have happened here?

You may have a library conflict. Try making a test sketch and testing various combinations. However, you should never have a mismatch on verification.

But which library is responsible for delay(); ???

Or might a wrong fuse be fault of this behavior?

The Arduino library is responsible for delay(). It is always included. The fuses appear to be correct.

So where may I search the problem?

As the test-sketch nearly only contains delay, it's very small. It seems like bigger sketches are uploaded properly.

Are there any hints or details on the error codes?
I'd like to understand what might go wrong.

Try some other sketches, like blink and maybe one with around 3000 bytes. The fact you are failing to verify usually points to a problem writing. Using completely different sketches would help in finding what is going wrong.
Just something else to try.