How do I change the internal clock of an Atmega328PB using the Arduino IDE?

Sorry for my poor English. I'm from Brazil and I'm using automatic translation.

I'm having trouble changing the internal oscillator of an Atmega328PB from 1MHz to 8MHz.

Before this post, I saw several posts with similar issues, but with different configurations than mine, so I'll explain what's happening to me.

I have a custom board consisting of an Atmega328PB (no external crystal), an ISP connector, and a buzzer, as shown in the schematic below.

I'm using the Arduino IDE 2.3.6 and an Arduino Nano to program the Atmega328PB. To add support for the Atmega328PB, I included the following URL in the board manager:

https://mcudude.github.io/MiniCore/package_MCUdude_MiniCore_index.json

Then I configured the following in the Tools menu of the Arduino IDE:

Board: “ATmega 328”

Bootloader: “No bootloader”

Clock: “Internal 8MHz”

Variant: “328PB”

Programmer: “Arduino as ISP”

Then I wrote the following code:

#define BUZZER PIN_PD6



void setup() {

  // put your setup code here, to run once:

  pinMode(BUZZER, OUTPUT);

}



void loop() {

  // put your main code here, to run repeatedly:

  digitalWrite (BUZZER, HIGH);

  delay(5);

  digitalWrite(BUZZER, LOW);

  delay(1000);

}

When I upload the sketch to the Atmega328PB via Arduino Nano, the buzzer starts to cycle on and off, as expected, but at a frequency 8 times lower, which suggests that the Atmega328PB clock is still operating at 1MHz. In fact, when I change the clock option (Tools > Clock) to "Internal 1MHz," the buzzer frequency is correct.

What can I do to change the internal oscillator frequency to 8MHz?

You need to set a correct values of the MCU fuses

I believed that by selecting the Clock: "internal 8MHz" option, the programmer would already do this. How do I do this?

Selecting the Clock option you only change a core frequency value that used during the compilation process.
To change the hardware fuses via Arduino IDE you need to perform an IDE action "Update the bootloader". Note that you need this even in case your system doesn't use a bootloader.

2 Likes

I couldn't find that option. Is it located in the Tools menu? Below is a screenshot of my Tools menu:

I believe that despite the fact that you do not want a bootloader, you still have to use the "burn bootloader" function (bottom of your screenshot) to set the fuses appropriately. A bootloader will not actually be written because you have selected the option "no bootloader ". More or less as @b707 has already said.

It worked. Thank you very much to @b707 and @6v6gt