Can't upload more than once on AtMega328 chips

I bought from various sources a bunch of ATMega328P-U and ATMega328PB-U that I soldered on a custom PCB.
I successfully flashed MiniCore bootloader on each through SPI, and then uploaded a basic sketch to read fuses with an USBtoUART dongle.
Here is what I noted:

  • #1 MEGA328P U-TH 35478D 20074PU : bootloader burned successfully ; sketch uploaded successfully at first (tho didn't display Serial.print unless I unplug-replug it) ; sketch upload timed-out on second try ; needed to reburn bootloader to reupload new sketch.
  • #2 MEGA328P U-TH 35478D 20074PU : likewise #1
  • #3 MEGA328PB -U 2021D TW 2021BH : likewise #1 but never displayed serial prints.
  • #4 MEGA328PB -U 20120 TW 2012R82 : likewise #3, never displayed serial prints.
  • #5 MEGA328P U-KR 35473D 1950AN6 : everything worked as expected (serial print on first sketch upload, second sketch upload ok as well).
  • #6 MEGA328P U-TH 354730 2012SMH : likewise #1

FWIW, chips #4 #6 were bought on Farnell, and the #1 #2 #3 #5 on Aliexpress.

Fuses read on serial prints for all MEGA328P were: Low: 0xE2 ; High: 0xD6 ; Ext: 0xFD ; Lock: 0xCF
Apart from the uC, the PCB is populated with 3 capacitors between each VCC and GND of the uC, various resistors, 2 2N7002 transistors, some buttons and headers.

I can't make any sense out of those logs. Did anyone encountered such behaviour?

The "can only upload once" symptom usually means that the auto-reset circuit is missing or faulty.

In order to upload, the bootloader must be activated. Normally, this requires the microcontroller to be reset just before the upload process starts. The Arudino boards like the Uno have a special circuit, which is the DTR or RTS pin of the USB to Serial adapter chip (e.g., "FTDI") connected to the reset pin of the ATmega328P, etc. via a 0.1 uF capacitor.

If you don't have this auto-reset circuit in place, you will need to manually reset the microcontroller at the start of the upload process. Note that the bootloader times out after a short time without an upload starting and when you press the "Uplpoad" button the Arduino IDE compiles the sketch before starting the upload process. So if you do the reset while the compilation is happening, the bootloader will have timed out by the time the upload starts and it will still fail. So wait until the black console pane at the bottom of the Arduino IDE window shows the sketch memory usage (which indicates the compilation finished) before doing the reset.

When you burn the bootloader, the application is erased, so the bootloader just runs constantly, meaning no reset it necessary to activate it. This is why you can upload the first time without a reset.

Well I forgot about it, but for all those tests I did have a 0.1uF between DTR and RST.
edit: let me double-check, it might be wired the wrong way...

Try doing a manual reset as I described above to check whether your auto reset circuit is faulty.

Ok, just did it manualy (wired a pushbutton between DTR and RST, that I pressed shortly before upload starts) but it didn't work better than my adapter circuit with a .1uF cap.
I happen to mis-wire that cap circuit, but now that I corrected it I have a new unexpected behaviour: it seems I need to unplug my board for a few seconds before being able to reupload a new sketch. That means I manage to re-upload a sketch, but not one after the other, unless I unplug-replug the dongle and the board for a couple of seconds.
Is that normal, like for the cap to discharge?

Definitely not normal behavior.

Nailed it! I didn't know the DTR -> 0.1uF cap -> RST circuit needed a pullup resistor (as in an RC circuit to get the delay, am I right?). Just added a 10kO resistor between RST and VCC, now it works every time!
Thx for your help @pert :slight_smile:

You're welcome. I'm glad to hear it's working now. Enjoy!