"avrdude: verification error; content mismatch" error when programming AVR based microcontrollers

Problem:
When programming for an AVR compatible microcontroller, such as the Atmega328p, you’re facing an error after successfully building and uploading a first time, but all subsequent times result in an error like the below. Since you’re not programming for an Arduino, using the “Burn Bootloader” option in PlatformIO does not work either.

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

avrdude: safemode: Fuses OK (E:FD, H:D9, L:E2)

avrdude done.  Thank you.

Cause:
This happens mainly when you’re setting fuses in your platformio.ini file manually, for example, when you change the clock source to the internal default of 8MHz instead of using an external clock source. Mine is like such:

[env:ATmega328P]
platform = atmelavr
board = atmega328p
upload_protocol = usbasp
upload_flags =
    -Pusb
    -B5

; Set fuses for internal 8MHz RC oscillator, fast rising power
board_fuses.lfuse = 0xE2
board_fuses.hfuse = 0xD9

I’m not pretty sure what’s the deep root cause of the issue, but I’m afraid it has to do with the fact that changing some of the fuses make the program to behave unnexpectedly.

Solution:
Please always click the “Set Fuses” option on your PlatformIO menu before uploading the code everytime to the microcontroller. This ensure that the fuses are correctly set and does not conflict when avrdude is checking the upload. After that is done, use the “Upload” option to reestart the build and upload to code to the target.