How do I configure the 328PB's clock prescaler so it can run at 4Mhz

Hi, so I have an ATMega 328PB that has an external 8Mhz oscillator. But I want to configure it using the prescaler so I can run it at 4Mhz so I can run the microprocessor at lower voltages such at 1.8v
image
(https://ww1.microchip.com/downloads/en/DeviceDoc/40001906C.pdf)

Does anyone know how I do this? Can it be done via the platformio.ini file or does it need to be done in the actual sketch code?

This is what I currently have in my platformio.ini file

[env]
platform = atmelavr
board = ATmega328PB
framework = arduino
board_hardware.oscillator = External ; Oscillator type
board_build.f_cpu = 8000000L
board_hardware.bod = disabled        ; Set brown-out detection
board_hardware.eesave = yes          ; Preserve EEPROM when uploading using programmer
build_unflags = -flto
board_hardware.uart = uart0          ; Set UART to use for serial upload
board_upload.speed = 57600
monitor_speed = 115200
upload_flags =
    -P$UPLOAD_PORT

Assuming you have the fuses burned without the CLKDIV8 set, then I think according to

and

You just need to add that build flag and set the f_cpu frequency to 4000000.

[env]
platform = atmelavr
board = ATmega328PB
framework = arduino
board_hardware.oscillator = External ; Oscillator type
; CPU freq after prescalers
board_build.f_cpu = 4000000L
; div sysclk (original 8MHz from crystal osc.) by 2
build_flags = -DOSC_PRESCALER=0x01
board_hardware.bod = disabled        ; Set brown-out detection
board_hardware.eesave = yes          ; Preserve EEPROM when uploading using programmer
build_unflags = -flto
board_hardware.uart = uart0          ; Set UART to use for serial upload
board_upload.speed = 57600
monitor_speed = 115200
upload_flags =
    -P$UPLOAD_PORT
1 Like