ATMEGA328PB with 16 MHz crystal

Hi. I'm trying to program a single ATMEGA328PB MCU using MiniCore and a USBTinyISP programmer.
At this time, i can burn the bootloader and upload any sketch using the 8 MHz internal oscillator, but when I try to program using the "External 16 MHz" option, the process is not done succesfully. I have a lot of 50 ATMEGA328PB pieces and all of them show the same behaviour.

The circuit I'm using consists in one 16 MHz crystal and 2x 22pF capacitors as shown in the image.

The error message I get when i try to burn the bootloader using the External 16 MHz option is shown below:

avrdude: Version 6.3-20201216
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2014 Joerg Wunsch

         System wide configuration file is "C:\Users\joser\AppData\Local\Arduino15\packages\MiniCore\hardware\avr\2.1.1/avrdude.conf"

         Using Port                    : usb
         Using Programmer              : usbtiny
avrdude: usbdev_open(): Found USBtinyISP, bus:device: bus-0:\\.\libusb0-0001--0x1781-0x0c9f
         AVR Part                      : ATmega328PB
         Chip Erase delay              : 9000 us
         PAGEL                         : PD7
         BS2                           : PC2
         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    20     4    0 no       1024    4      0  3600  3600 0xff 0xff
           flash         65     6   128    0 yes     32768  128    256  4500  4500 0xff 0xff
           lfuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           hfuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           lock           0     0     0    0 no          1    0      0  4500  4500 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
           efuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00

         Programmer Type : USBtiny
         Description     : USBtiny simple USB programmer, http://www.ladyada.net/make/usbtinyisp/
avrdude: programmer operation not supported

avrdude: Using SCK period of 10 usec
avrdude: AVR device initialized and ready to accept instructions

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

avrdude: Device signature = 0x1e9516 (probably m328pb)
avrdude: erasing chip
avrdude: Using SCK period of 10 usec
avrdude: reading input file "0x3f"
avrdude: writing lock (1 bytes):

C:\Users\joser\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino18/bin/avrdude -CC:\Users\joser\AppData\Local\Arduino15\packages\MiniCore\hardware\avr\2.1.1/avrdude.conf -v -patmega328pb -cusbtiny -Uflash:w:C:\Users\joser\AppData\Local\Arduino15\packages\MiniCore\hardware\avr\2.1.1/bootloaders/optiboot_flash/bootloaders/atmega328pb/16000000L/optiboot_flash_atmega328pb_UART0_115200_16000000L_B5.hex:i -Ulock:w:0x0f:m 

avrdude: Version 6.3-20201216
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2014 Joerg Wunsch

         System wide configuration file is "C:\Users\joser\AppData\Local\Arduino15\packages\MiniCore\hardware\avr\2.1.1/avrdude.conf"

         Using Port                    : usb
         Using Programmer              : usbtiny
Writing | ################################################## | 100% 0.00s

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

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

avrdude: usbdev_open(): Found USBtinyISP, bus:device: bus-0:\\.\libusb0-0001--0x1781-0x0c9f
avrdude: verifying ...
avrdude: 1 bytes of lock verified
         AVR Part                      : ATmega328PB
avrdude: reading input file "0b11110111"
         Chip Erase delay              : 9000 us
avrdude: writing efuse (1 bytes):

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

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

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

         PAGEL                         : PD7
avrdude: verifying ...
         BS2                           : PC2
         RESET disposition             : dedicated
         RETRY pulse                   : SCK
         serial program mode           : yes
         parallel program mode         : yes
         Timeout                       : 200
         StabDelay                     : 100
         CmdexeDelay                   : 25
         SyncLoops                     : 32
avrdude: 1 bytes of efuse verified
avrdude: reading input file "0b11010110"
avrdude: writing hfuse (1 bytes):

         ByteDelay                     : 0
         PollIndex                     : 3
         PollValue                     : 0x53
         Memory Detail                 :

Writing | ################################################## | 100% 0.00s
                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------

avrdude: 1 bytes of hfuse written
           eeprom        65    20     4    0 no       1024    4      0  3600  3600 0xff 0xff
avrdude: verifying hfuse memory against 0b11010110:
           flash         65     6   128    0 yes     32768  128    256  4500  4500 0xff 0xff
avrdude: load data hfuse data from input file 0b11010110:
avrdude: input file 0b11010110 contains 1 bytes
avrdude: reading on-chip hfuse data:

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

avrdude: verifying ...
avrdude: 1 bytes of hfuse verified
avrdude: reading input file "0b11111111"
avrdude: writing lfuse (1 bytes):

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

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

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

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

avrdude done.  Thank you.

           lfuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           hfuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           lock           0     0     0    0 no          1    0      0  4500  4500 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
           efuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00

         Programmer Type : USBtiny
         Description     : USBtiny simple USB programmer, http://www.ladyada.net/make/usbtinyisp/
avrdude: programmer operation not supported

avrdude: Using SCK period of 10 usec
avrdude: AVR device initialized and ready to accept instructions

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

avrdude: Device signature = 0x1e9516 (probably m328pb)
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: Using SCK period of 10 usec
avrdude: reading input file "C:\Users\joser\AppData\Local\Arduino15\packages\MiniCore\hardware\avr\2.1.1/bootloaders/optiboot_flash/bootloaders/atmega328pb/16000000L/optiboot_flash_atmega328pb_UART0_115200_16000000L_B5.hex"
avrdude: writing flash (32768 bytes):

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

avrdude: 32768 bytes of flash written
avrdude: verifying flash memory against C:\Users\joser\AppData\Local\Arduino15\packages\MiniCore\hardware\avr\2.1.1/bootloaders/optiboot_flash/bootloaders/atmega328pb/16000000L/optiboot_flash_atmega328pb_UART0_115200_16000000L_B5.hex:
avrdude: load data flash data from input file C:\Users\joser\AppData\Local\Arduino15\packages\MiniCore\hardware\avr\2.1.1/bootloaders/optiboot_flash/bootloaders/atmega328pb/16000000L/optiboot_flash_atmega328pb_UART0_115200_16000000L_B5.hex:
avrdude: input file C:\Users\joser\AppData\Local\Arduino15\packages\MiniCore\hardware\avr\2.1.1/bootloaders/optiboot_flash/bootloaders/atmega328pb/16000000L/optiboot_flash_atmega328pb_UART0_115200_16000000L_B5.hex contains 32768 bytes
avrdude: reading on-chip flash data:

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

avrdude: verifying ...
avrdude: verification error, first mismatch at byte 0x7e00
         0x08 != 0x01
avrdude: verification error; content mismatch

avrdude done.  Thank you.

Error quemando bootloader

Add 100nF decoupling to the power pins .

I’m not so sure about R1 , the Atmega data sheet does not show it and I’ve never fitted one .

In this case, should I try adding a 100nF capacitor between +5V and GND?

There should be a 0.1uF/100nF cap on Each VCC and AVCC pin.
3 pins, 3 caps.

If I understand correctly, you take a stock 328pb, program the fuses first, then program the flash, then trying to verify and that failed. I wonder if you CAN program fuse after this process. If not, you're probably using the wrong crystal and it doesn't oscillate so you get nothing once you program your fuse to use the crystal. Do you have a scope to measure the oscillation of the crystal?

Well, I have used an oscilloscope to view the waveform at the XTAL pins.

Now, I will describe what I have done to get these pictures:

I am using a USBTinyISP programmer and a 32-pin SMD to 28-pin PTH to program the MCU via ISP. The socket is mounted on a PCB with the circuit shown in my post.
The configurations for MiniCore i'm using are shown below, I only switch between External and Internal oscillator:

  1. I used a 32-pin socket to program a single ATMEGA328, as I have always done. I burnt the bootloader and uploaded a blink sketch using both Arduino AVR Boards core and MiniCore, and it worked correctly. In both cases I was able to get the following nice 16 MHz waveform.

  2. Using the same 32-pin socket, I burnt bootloader onto a single ATMEGA328PB and uploaded the blink sketch using the internal 8 MHz oscillator option provided by MiniCore. At this time all have worked well.

  3. I tried to burn the bootloader just changing the oscillator option to external 16 MHz with the ATMEGA328PB in the socket. At the moment that burning the bootloader starts, I can see in theoscilloscope the waveform shown below, which is quite different from the other one, but stills at 16 MHz. At this moment, I get again the Error while burning bootloader.

After doing this, I can use the ATMEGA328PB as normally if I set the clock source back to internal 8 MHz.

That's really strange. Are you sure you have good connection to your crystal and caps? It took me a while to find where your lfuse value and it looks right, 0xFF. Maybe decrease the cap values a bit if you have say 18pF caps. There might be too much stray capacitance if you're using sockets etc. If you don't have other values, remove the 22pF caps see if it works.

Do you have a spec sheet for the exact crystal you're using?
16MHz is the top of the range that the 328pb is supposed to support, and apparently the built-in oscillator is "picky."

And in particular: