How I fixed my Arduino ISP 'Device signature = ...' errors

Problem:

I was trying to program my Arduino using an AVRISP MK II programmer, but every time I tried to program it (using avrdude or the Arduino software), I encountered errors like this:

avrdude: stk500v2_command(): command failed
avrdude: stk500v2_program_enable(): bad AVRISPmkII connection status: MOSI fail, RST fail, SCK fail, Target reverse inserted
avrdude: initialization failed, rc=-1
avrdude: AVR device initialized and ready to accept instructions
avrdude: Device signature = 0xb058e2
avrdude: Expected signature for ATmega2560 is 1E 98 01
avrdude: NOTE: "flash" memory has been specified, an erase cycle will be performed
         To disable this feature, specify the -D option.

Solution

In my case, the reason for this issue was that there was a shield connected to the SPI bus.

Since the SPI bus on the Arduino is shared with the ICSP header for In-System Programming (ISP), which is the protocol used by external programmers for programming AVR chips, connecting anything else to the SPI bus may cause issues.

Try to disconnect anything from the SPI bus. If the CS (chip select) signal of the extra device on the SPI bus is low, it might interfere with the communication with the AVR chip!