Pages

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

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

Friday, September 13, 2024

Testing an obstacle avoidance sensor

For an index to all my stories click this text

I was playing around with an obstacle avoiding sensor and want to share my experiences with you.

What is it.

An obstacle avoidance sensor is mostly used in robotics.

The sensor has an infrared led. This led sends out lightin the infrared spectrum which we can not see. the light normally shines streight on.

Next to the infrared led there is a photodiode. That photodiode detects infrared licht.

When the sensor approaches an obstacle the infrared light is reflected back and detected by the photodiode.

Like said, this is mostly used in robotics. The sensor is mounted on a robot and when the robot comes close to a wall or any other obstacle the photodiode sends a signal to the robots controller to alter direction.

The sensor

This is how the sensor looks in real life. Let's have a look at the different parts of it.

On the left you can see the photodiode at the top and the IR led beneath it. They are next to eachother so the there is a maximum chance of detecting any reflection.

In the middle there is a screw with witch you can adjust sensitivity. That means by turning the screw you can adjust the maximum distance at what the reflection is detected.

Next to the header pins there are two leds. The top led is the power led and that is always on whenever the sensor has power. The led at the bottom is off and goes on when an obstacle is detected.

A test setup

To test the sensor I build a simple setup on a breadboard. I connected the sensor to a Raspberry Pi Pico. Here is what the breadboard looks.


The led (I used a blue one) is connected with a current limiting resistor to GND and to GPIO15.

Pico's GND (pin38) is connected to the sensor's GND. and Pico's 5V (pin 40) is connected to VCC. The sensors output (OUT) is connected to GPIO16.

Test program in MicroPython

To test the sensor I wrote a simple program in MicroPython.

import time
from machine import Pin

obstac = Pin(16, Pin.IN)
led = Pin(15, Pin.OUT)

while True:
      print(obstac.value())
      led.value(not obstac.value())
      #led.value(0)
      time.sleep(.2)

In the loop the program constant tests the out pin of the sensor. When that pin's output changes from 1 to 0 (obstacle detected) the led on GPIO15 goes on.

Copy this program, paste it in Thonny and save it as main.py on your Pico. It will work on the Pico, Pico W and Pico2.
Saving it as main.py makes the program run immediately when the Pico is powered up so you can do some tests with a powerbank, phone charger or batteries.

If you want to learn about MicroPython on the Raspberry Pi Pico or Pico W please consider buying one of my books that are listed at the bottom of this page.

First test.

This should immediately work. Just hold your hand above the sensor and move it towards the sensor. At a certain moment the led will go on both on the sensor as well as the blue led.

Now you can adjust the screw in such a way that the leds will go on at your desired distance.

Distance

According to some specifications I found you can adjust the screw so the distance at which the led will go on can be set from 2 to 20 cm.

Just be aware that there are environmental issues that might influence the detection distance. Here are some things you might like to take into consideration.
- Temperature may affect the distance
- TL light might affect the distance
- Sun light might affect the distance
- Direct light shining on the photodiode might affect the distance

The only thing I want to say is that you should adjust the sensivity screw in your real-life setup.

Some tests

Here are the results of some tests I performed.


All leds are OFF because there is no obstacle in front of the sensor.


A shining transparant box was put in front of the sensor and at a distance of 8 cm the obstacle was detected.


A black piece of paper was put in front of the sensor and at 6cm it was still not detected.


The black paper was detected at 2.5 cm.
This means that the black paper absorbed (did not reflect) a lot of infrared light so it was not detected until it was at a short distance.


A white piece of printer paper was detected at 8 cm distance. The white color reflects the infrared light more so the paper was earlier detected.


A black shining object was detected at 4cm. This means that the black color absorbed some infrared light but the shing surface reflected also some infrared light.


A book (yes my book about the Pico) with a shining green cover reflected the infrared light at a larger distance so it was already detected at about 8.5cm

Water ???

Solid obstacles: OK
But how about fluids.

If you want to reproduce this test yourself just make sure the electronics don't get wet. Water and electronics don't mix and you might damage your sensor or pico beyond repair.


I filled a white bowl with a small layer of water and help the breadboard above it.
The led stayed off: no obstacle detected.


I used a bottle of water to gradually fill the bowl and yes !!!
At a certain moment the led went on. So water reflects the infrared light.


Concluding

First let me state that obstacles are detected very well.
In the demo program you can lower the delay and that will not affect the working of the sensor. So in real life you should use an interrupt for testing for an obstacle which is the fasted method.

The obstacle avoidance sensor detects obstacles but does not give any indication at what distance the obstacle is 'seen'. The difficulty lies in different materials reflecting the infrared light more or less.
If you need to measure distance then use an HC-SR04 ultrasonic sensor.

This means that you should adjsut the sensivity screw so that the obstacle is detected at a larger distance when you are working with a fast moving object like a remote controlled car. That is because you will need time to shut the motor down. But you also need to take the obstacles material in count as black obstacles are seen later.

I was specially impressed when I realised that the sensor also detected water as an obstacle.
I did not try but wonder if a black bowl would make a difference.

And remeber: you can always use this sensor inverted. Meaning that an alarm is given when an obstacle is removed like the lid of a box is taken off.

Till next time
Have fun

Luc Volders




Friday, September 6, 2024

Raspberry Pi Pico2 with Arduino IDE Linux install

For an index to all my stories click this text

Just 2 weeks ago on August 23 I wrote about the new Raspberry Pi Pico2 that just arrived. For those in the blind: the Raspberry Pi Pico2 is the successor of the Pico, the first micro controller of the Raspberry Pi company. It is a serious challenger for the Arduino boards as it is dead-cheap and widely available. The advantages are obvious: more speed, more memory and more storage and the same footprint.

At the time I wrote that the only languages available where MicroPython / CircuitPython and the Raspberry Pico SDK. No Arduino IDE at that time. You can read that story here:
http://lucstechblog.blogspot.com/2024/08/first-look-at-raspberry-pi-pico2.html

Well as you know things in the electronics world go fast: really fast. And Earle F. Philhower, III managed to get the Pico2 working with the Arduino IDE. He is the one that got the Pico working with the Arduino IDE before the Arduino company managed it themselves. And now he did it again for the Pico2 !! Quite and achievement.

In a few steps I will show how to get the Pico working with the Arduino IDE.

Where to get the latest Arduino IDE.

You will need the latest Arduino IDE, meaning at least version 2.X. The previous versions (1.X) will not compile for the Raspberry Pi Pico2. So make sure you have the latest version. You can download it here:

https://www.arduino.cc/en/software

There are versions available for Windows, Linux and MacOS.
On Windows just download the .EXE file and click on it to install.

I abandoned Windows last year in favour of Linux (Kubuntu).
For Linux there is an AppImage available. However there was (at the time of this writing) a problem with that. Kubuntu just made an update from version 22 to version 24 (I skipped version 23). And the AppImage unfortunately does not work with this new version.

Fortunately there is also a FlatPack version available. And that works flawless. Here is the link: https://flathub.org/apps/cc.arduino.IDE2

At the top of the screen you will find an Install button. Just use that. If you did not install the FlatPack option in your Linux setup I advise you to do so. Here is the link on how to install Flatpack on your Linux environment: https://flatpak.org/setup/

Add the Raspberry Pi Pico boards.

The first step is to open the board manager and search for Pico.



We need the version: Raspberry Pi Pico/RP2040 by Earle F. Philhower, III
Next to the INSTALL button there is a drop-down menu in which you can chose what version you want to install. Always chose the latest version. At the time of this writing that is version 4.0.1. Starting at version 4.X there is support for the Pico2.


After a few seconds the Arduino IDE will show that the Pico boards are installed.

When I pressed the upload button with the empty sketch I got an error message. The program compiled flawless but there was an upload error.

I used the empty sketch for testing. A better choice would be to use the Circle sketch from the examples drop-down menu. This example makes your mouse draw a circle on your screen when you press the boot button.

So I decided to unplug the Pico2 from the USB cable and plug it in anew while pressing the boot button. Just like you do when you install MicroPython for the first time.
Well that did not work either.

The right way.

First look in the drop down menu for the Export Compiled Binary entry and click on that.

The empty sketch compiles and is saved to a folder. But which folder ????

Click on the drop-down menu entry called Show Sketch Folder.

The folder shows the .ino file (which is the source code) and a build folder. Click on that folder.

Inside the build folder there is another folder called rp2040.rp2040.rpipico2 Click on that folder.

And just look at that. There is an uf2 file. Just what we need for the Pico.

Now plug the Pico in again while pressing the boot button.

The Pico2 will show up as a new device in your file manager. Open that device in a new window.

Now drag the uf2 file to the Pico's window and copy it there.
The Pico's window will close and your done. The program will run immediately.

That's it
Till next time
Have Fortunately


Luc Volders