Wednesday, September 18, 2024

Overclocking the Raspberry Pi Pico2 in MicroPython

For an index to all my stories click this text

This is an update on my first story about the Raspberry Pi Pico2.
In that story I wrote that overclocking the Raspberry Pi Pico2 was not possible with MicroPython. And actually I was wrong.......

What is overclocking.

Every Raspberry Pi Pico (Pico, Pico W and Pico2) has an internal clock generator. This generator determines at what speed the Raspberry Pi Pico runs.
The Pico2 normally runs at 150.000.000 ticks per secons. That's 150 Mhz. This is called the clock-frequency


This is how you check the clock frequency in MicroPython.

Speed test

Like I showed in the first story about the Pico2 I wrote a small speed test.

import time
start = 10000
end = 11000

starttim = time.ticks_ms()

print ("Searching the Prime Numbers from ", start, "to ", end)
for number in range (start, end + 1):
    if number > 1:
        for i in range (2, number):
            if (number % i) == 0:
                break
        else:
            #print (number)
            continue

endtim = time.ticks_ms()
print(endtim-starttim)

What this program does is loop through all figures from 1 to 10000 to 11000 and test each number on being a prime number.

As you can see this takes 6682 milliseconds which is about 7 seconds.

Overclocking

Overclocking is done by setting the machine.freq() to a higher speed.

So I started with a speed of 200.000.000 which is 200 Mhz being 50Mhz faster as the normal speed.


And there you go. The speed test now only took about 5 seconds.


By using the command machine.freq(300.000.000) I set the clock frequency at 300Mhz which is twice the speed at which the Pico2 normally runs.


And there you go almost twice the speed as at the regular speed.

300Mhz is the max.

Naturally I tried to go up with the speed.


At 310 Mhz an error ocured.
This was the error that made me think that the Pico could not get overclocked in the first story about the Pico2. I obviously tried to set the speed higher then MicroPython allowed.

Change clock speed in your program

It is indeed possible to change the clock speed inside your program.

import time
import machine

start = 10000
end = 11000

machine.freq(150000000)

starttim = time.ticks_ms()

print ("Searching the Prime Numbers from ", start, "to ", end)
print ("Using standard clock speed (150Mhz)")

for number in range (start, end + 1):
    if number > 1:
        for i in range (2, number):
            if (number % i) == 0:
                break
        else:
            #print (number)
            continue

endtim = time.ticks_ms()
print(endtim-starttim)
print(" ")

machine.freq(300000000)

starttim = time.ticks_ms()

print ("Searching the Prime Numbers from ", start, "to ", end)
print ("Using 300 Mhz clock speed")
for number in range (start, end + 1):
    if number > 1:
        for i in range (2, number):
            if (number % i) == 0:
                break
        else:
            #print (number)
            continue

endtim = time.ticks_ms()
print(endtim-starttim)

machine.freq(150000000)

This is the same speed test program first running at standard speed (150Mhz). Then inside the program the clockspeed is changed to 300Mhz. And when the program finishes the clock speed is set back to 150Mhz.


And as you can see this obviously works !!

Considerations.

Overclocking sounds great and speeds up your programs a lot. However there are some considerations.

- By overclocking the M33 core will get hotter as by6 normal use and that might limit the lifespan of your Pico2
- Overclocking might influence SPI and I2C communications in such a way that certain sensors will not work flawlessly.

So use this with caution.

Til next time.
Have fun

Luc Volders