Saturday, September 21, 2024

Overclocking the Raspberry Pi Pico2 in Arduino IDE

For an index to all my stories click this text

In a previous story I wrote how you can overclock the Raspberry Pi Pico2 when programmed in MicroPython. Naturally you can also overclock the Pico2 when programmed in the Arduino IDE. You can read that story here:
http://lucstechblog.blogspot.com/2024/09/overclocking-raspberry-pi-pico2-in.html

The advantage of MicroPython is that you can dynamically change the clock frequency in the shell or even have your program change the clock frequency.
Unfortunately this is (at the moment I write this) not possible.
What you can do is set the clock frequency when you compile the program.

Setting the clock frequency.

Actually this is quite easy in the Arduino IDE.
First step is of course to install the Raspberry Pi Pico2 in the Arduino IDE. If you have not done this before you can read this story that explains it:
http://lucstechblog.blogspot.com/2024/09/raspberry-pi-pico2-with-arduino-ide.html


Next choose the microcontroller Raspberry Pi Pico2.


Now choose from the tools drop down entry the CPU speed. As you can see there are a variety of possibilities. For our first test leave the clock frequency as it is at 150Mhz.

The test program.

I re-wrote the test program that I used for speed testing MicroPython. In that program I tested all prime numbers between 10.000 and 11.000
The Arduino IDE is a lot (and really a lot) faster as MicroPython. So I had to alter the program to calculate all primenumbers from 1 to 400.000 !!!!

unsigned long startTime;
unsigned long endTime;

void setup() {
  Serial.begin(9600);
  startTime = millis();

  for (int num = 10000; num <= 11000; num++) {
    if (isPrime(num)) {
      Serial.println(num);
    }
  }

  endTime = millis();
  Serial.print("Time taken: ");
  Serial.print(endTime - startTime);
  Serial.println(" milliseconds");
}

void loop() {
  // Nothing to do here
}

bool isPrime(int num) {
  if (num <= 1) return false;
  for (int i = 2; i <= sqrt(num); i++) {
    if (num % i == 0) return false;
  }
  return true;
}

Running at 150Mhz

Wel look at this.


Calculating 400.000 prime numbers took the program just 15 seconds.

Running at 300Mhz

I compiled the program anew after setting thye clock speed at 300Mhz.


And there is the result. It took the Pico2 now 7702 milliseconds which is about 8 seconds. Think about that: 8 seconds to test 400.000 numbers whether they are a prime number or not. That is bloody fast !!!

Caution please.

Overclocking the Pico2 is interesting and delivers some great speed improvement. However do this with caution because:

- Overclocking the Pico2 might heat up the controller chip and therefore shorten the lifespan of your Pico2
- Overclocking the Pico2 might do strange things with I2C and SPI ports so sensors might stop working or give false information.

Concluding.

Overclocking the Pico2 works and makes the controller a lot faster. Indeed changing from 150Mhz to 300Mhz made my test program run twice as fast which is impressive.
It is however somewhat of a pity that you can not change the clock frequency from within a program. It has to be set upfront.

Both MicroPython and the Arduino IDE do not allow overclocking the Pico2 above 300Mhz.

The MicroPython program took 12 seconds to calculate 1000 primenumbers.
The Arduino IDE took 15 seconds to calculate 400.000 prime numbers.
So if speed is of the utmost importance use the Arduino IDE. If you are looking for an easy way to program your Pico2 then MicroPython is the way to go.

Till next time
have fun

Luc Volders