Decrease CPU frequency

Hi,

I am using Arduino Nano Every for my application and I want to reduce the power consumption by decreasing the CPU frequency. Can anyone provide the coding steps for this purpose?

Thank you in advanced,

Nicolás

may be some information of interest here:

1 Like

Are you using one of the low-power 'sleep' modes to save power? It is possible that using a slower clock when awake will increase power use because you need to be awake longer to do the same work.

1 Like

MCUdude's MegaCoreX for the atmega4809 lets you select the clock speed in the IDE under the Tools menu.

1 Like

Thank you for your answer.

Maybe I could use that sleep mode, but my intention was to be working all the time but using a lower CPU freq, kind of 1 MHz.

Is there any solution for changing the prescaler or something like that?

have you tried what's in the link above?

Hi, yes!

However, when I modify that boards.txt file, I find problems uploading the code.
Should I modify any other line that "uno2018.build.f_cpu=16000000L" ?

Thank you!

I've never done it (I don't own this device) , just remembered seeing that discussion

what is the reported problem ?

may be worth moving this question in the Nano Every - Arduino Forum rather than generic Microcontrollers - Arduino Forum

It is okay, somehow I managed to get rid of the error. Thank you for that reference!

Any suggestion about how to verify that the frequency actually did decreased? I have measured power consumption at 16 MHz and (apparently) at 1 MHz. However, there is not much difference on the mA consumption.

on AVR Arduinos there was a trick where you could use the watchdog which runs on a different clock to compare timing.

would need to adjust to Nano Every

a crude solution would be to run a very long loop (like doing10,000,000 times some maths) and compare how long it takes with and without the modification

yeah, because of the LEDs, voltage regulator etc you probably have power going down the drain...

2 Likes

Nice, I'm gonna take a look and let you know. Thank you so much for the help! :slight_smile:

That line just tells the IDE/compiler the clock frequency, it does not change the actual clock frequency on the nano every, for that you also need to specify different fuse setting in the boards.txt file (unlike most arduino boards, the fuses are written every time a sketch is uploaded to the nano every). I've experimented with running the board at 1MHz, but do not recall the correct fuse settings. It is much easier to use MCUdude's MegaCoreX then you can use Tools > Clock in the IDE to select the clock frequency, use the "internal" options, there is no external clock source on the nano every board.

For testing purposes, you can run the Blink sketch to verify that the blink rate is correct, I think that you will notice that only changing the uno2918.build.f_cpu line in boards.txt will result in an incorrect blink rate.

1 Like

Thank you so much for your reply, I'm gonna consider to use that MegaCorteX.
Definitely looks much easier.

Regards!

In this post it was said to work. so worth checking indeed

Its been a while since I looked at changing the clock frequency on the Nano Every, took a quick look at the data sheet and it appears the internal oscillator is set to either 16MHz or 20MHz by the OSCCFG fuse, which can be changed in the boards.txt file. The output of that oscillator is fed into a prescaler that outputs the clock frequency. The prescaler can divide by 1, 2, 4, 8, 16, 32, 64, 6, 10, 24, or 48, and is set within the code, but if I recall correctly the board package used by the IDE does not implement all the possibilites. You can alter the prescaler within your own code, but that will result in timing problems because the compiler needs to know the correct frequency in order to adjust time-sensitive code such as the millis counter, serial baud rate, etc.

1 Like

OK thanks for this. So it was kinda a solution for 16 or 20 MHz but not quite there for 1MHz.

MegaCorteX to the rescue !

Hi.

That is exactly what I was intending to do. I've seen a Youtube video explaining about this consumption decrease when lowering frequency using the prescaler. However, when I try to put the same commands in my own code, the Arduino IDE returns an error about the variables declaration.

Find the video here:
Low Power Arduino - Sleeping at 0.3mA perfect for batteries (Arduino Nano deep sleep) - YouTube
The interesting part is around 1:30

Do you think I need kind of a special library to make this run?

You can try this to change the pre-scaler of the main clock at run-time

	CCP = CCP_IOREG_gc;
	CLKCTRL.MCLKCTRLB = CLKCTRL_PDIV_48X_gc | CLKCTRL_PEN_bm;

The PDIV can any of these values depending on how slow you want the CPU to run

This would change the clock feeding your UARTs and TIMERs so Serial.XXX and millis() (or most Arduino libraries) would no longer work as intended

This discussion explains how to change the prescaler within your code, but as the previous post pointed out anything that is dependent on the clock frequency will be affected.

1 Like

Those commands work perfectly. Thank you so much!