Flashing Atmega328PB only works once

This is the test sketch:

#include <LowPower.h>

#define LED_PIN 9


// the setup function runs once when you press reset or power the board
void setup() {
 // initialize digital led PIN as an output.
 pinMode(LED_PIN, OUTPUT);
 
 for (byte i=0; i<30; i++)
 {
   digitalWrite(LED_PIN, HIGH);   // turn the LED on (HIGH is the voltage level)
   delay(15);
   digitalWrite(LED_PIN, LOW);    // turn the LED off by making the voltage LOW
   delay(15);
 }
}


// the loop function runs over and over again forever
void loop() {
 LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
}

Steps to reproduce:

  1. Burn Bootloader using Atmega328PB-Testing Board package. (Board with "Crystal Clock" and "16MHz" selected in menu). Fuses are also set here.
    I've changed the boards.txt of the atmega328pb package to contain:
    atmega328pbcc.bootloader.low_fuses=0xDE
    atmega328pbcc.bootloader.high_fuses=0xD6
    atmega328pbcc.bootloader.extended_fuses=0xF5
    but even if you don't change this, the results should be the same.

I had to edit avrdude.conf too to include:

part parent "m328"
    id			= "m328pb";
    desc		= "ATmega328PB";
    signature		= 0x1e 0x95 0x16;

    ocdrev              = 1;
;
  1. Upload the sketch above via USB/FTDI. Upload works. Running of setup() routine can be observed by fast LED blinking.

  2. Try to upload the above sketch once again. The board resets (since LED blinking of setup() routine can be observed) but Avrdude gives a sync error.

If you change the sketch, so that ADC_OFF is replaced by ADC_ON, and burn the bootloader again, and upload the new sketch, there are no problems with subsequent uploads.

P.S.: For this to compile you need the attached (modified) Lowpower Lib. It's just a quick hack to make the Lowpower lib work....

LowPower.cpp (33.6 KB)

LowPower.h (2.88 KB)