Showing posts with label Arduino. Show all posts
Showing posts with label Arduino. Show all posts

Friday, March 28, 2025

Print your own USB connector

For an index to all my stories click this text.

Sometimes I find something on the web that is so usefull that I want to share it with you.

Powering your circuits.

When testing and building a small circuit with just one or two sensors you can use the power that your microcontroller supplies over it's 5V pin. Both the ESP32 and the Raspberry Pi Pico have a 5V and 3V3 power out pin.

But when I need more power I use an USB breadboard connector to power the project.


An example is this circuit where two TM1637 7 segment displays are connected to a Raspberry Pi Pico. The Raspberry pi pico is powered through it's USB port and on the left-top side you can see a breadboard USB connector attached to a USB power plug.

Nothing wrong with this but I always seem to run out of breadboard USB connectors.

3D printer to the rescue

Then I found this on the web:
https://www.instructables.com/Minimalist-USB-Connector-No-Special-Hardware-Requi/


This is a 3D printed USB connector. The photo shows the assembled version.
But does it work ????


Works like a charm !!!
As you can see the USB connector is plugged into a powerbank and the multimeter shows that a nice 5V is supplied.

The STL files

The connector consists of 2 parts.

You can find the STL files in the original story which you can find here:
https://www.instructables.com/Minimalist-USB-Connector-No-Special-Hardware-Requi/


The first part is the bottom in which you need to insert the wires. The second part is the top which you should glue and press on to the bottom part when the wires are placed. The top part has ridges that help keep the wires in place.


This is how the files look in my slicer. I use Cura.


Set the quality at 0.2mm and supports on. As these are small parts you could set adhesion also on.

Assembling

I used solid copper wire with a diameter of 0.5mm. That worked very well.

I took a slightly different approach to assembling as what the original developer described.

First I stripped the wires.


Then I pulled the stripped part through the holes.

Then I glued the lid on.
The last step was to cut off the excess wire.

The blue wire at the top is GND.
The red wire at the bottom is VCC (5V).
Please test before you actually put it to use to make sure the wires are not twisted. If you mix the wires up you might/will blow up your circuit so test, test, test !!!


The wires are thick enough to fit direct into a breadboard.

You can make the wires any length you want. Short to connect to a powerbank and fit on your desk. Or make them long to connect to a wall outlet with an USB power plug.

I have printed several of these. They are ready to use in my drawer. A really great design.

Til next time
Have fun

Luc Volders

Friday, November 29, 2024

Raspberry Pi Pico 2W with Arduino IDE

For an index to all my stories click this text

This week the Raspberry Pi Pico 2 W became available.


The Pico 2 is the successor of the original Pico but now with more memory and a lot faster. You can find a description here:
http://lucstechblog.blogspot.com/2024/08/first-look-at-raspberry-pi-pico2.html

The Pico 2 W is the much anticipated version with Wifi !!!


Unfortunately at the moment of this writing MicroPython is not yet available. But The Arduino language is !!!

Installing the Raspberry Pico 2 W in the Arduino IDE is actually extremely easy.

Make sure you are using the Arduino IDE version 2 or higher.


First step is to click on the Tools drop down menu and select Board and then Boards Manager




In the Boards Manager look for raspberry.
We are especially looking for the Raspberry Pi Pico/RP2040/RP2350 entry by Earle F. Philhower III


In the drop-down menu choose the latest version. Make sure that it is at least version 4.3.0 and install this version or update your previous version by clicking on the appropriate button.
Installing will take a minute, but if things get busy it might take a bit longer as there is a lot of info that needs to get downloaded from the internet.


Now open the Tools menu again, choose the boards section and there choose Raspberry Pi Pico/RP2040/RP2350 and at the top of the list you can see the Raspberry Pi Pico 2W entry.

I did some small tests and everything seems to work like promised, including the wifi section.

Thank you for building this Earle !!!!

Those working with Kubuntu Linux (like me) can find a tutorial on how to program the Pico here:
http://lucstechblog.blogspot.com/2024/09/raspberry-pi-pico2-with-arduino-ide.html

Till next time
have fun

Luc Volders

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

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












Friday, July 12, 2024

Install Linux programs with Appimage

For an index to all my stories click this text.

As you might know I switched from using Windows on my desktop machine to Ubuntu. And to be more specific to Kubuntu. Kubuntu has the Windows look and feel but still is full Linux with all it's benefits: loads of software freely available, easy to use, great security so no need for a virus killer, great file system so no need for disk defragmentation etc. etc. etc. This all leads to an enormous speed improvement on the same computer that was running windows.

At first I was a bit sceptical. I did not know if all the software I needed was available. So what I did is I installed Kubuntu on a USB stick and run it from that stick. That way my Windows drive was intact and I could switch anytime I like back to Windows.
Well after working 2 months from a USB stick and never turning back to Windows I was convinced and installed Kubuntu permanently on my harddisk.

Installing software


Installing software in Kubutu is easy. Kubuntu includes a program that is called Discover. Starting that program will show you a list with all available software. Clicking on a title installs that package for you. Easy: no difficult Linux commands to learn.

Everything you need is there: Office, Thonny, audio playes, video players, terminal programs (for ssh into raspberry pi's), Cirkit and Fritzing for schematics, web-browsers, mail programs, Cura and Prusa slicer (for your 3D printer, the Gimp, text editors, Discord etc. etc. etc. All the packages look great and they have that Windows look and feel so no steep learning curves.

Arduino ide

I only had a small problem with the Arduino IDE.



The Arduino IDE is available.

However there is an issue with it.




The version that is available is Arduino 1.8.19
Nothing wrong with that although version 2 is already for some time available.

But what is worse is that Kubuntu incorporates a lower version of Python than the Arduino IDE needs........
That would not bother me so much if it would work with all the Microcontrollers I work with. The Arduino IDE would work with the ESP32 and the Raspberry Pi Pico series but it does not work with the ESP8266 and that is a bummer.

Fortunately there is a solution !!!


Appimage to the rescue

There is a new way to install software packages on Linux. It is called Appimage.
Basically you download an Appimage for a software package and it contains all necessities for many Linus distributions.

AppImages are self-contained apps which can simply be downloaded & run on any Linux distribution.

This is a quote from their website.


So you can just download an Appimage, move it to a directory where you want it, click on it and it runs. And it should run on many different Linux distributions like Ubuntu, Kubuntu, Debian, Mint, Fedora, OpenSuse etc.etc.etc.

Appimagehub

There are universal software packages for all Linux distributions. But where to find the available packages.



Luckily there is Appimagehub.
Here is the link:  https://www.appimagehub.com/browse

On this website there are (at the moment of this writing) 1380 program packages available.


And look what I found in the programming section !!


Clicking on the icon revealed this screen.

Scroll down and there is a download button. Click that button.


A pop-up window appears and after a few seconds you can click the download button.
The file will get downloaded into your download folder.

I created a new directory in my Home folder and moved the file into that directory.



Click on the icon with your right mouse button and change the files attributes to Is executable.


If you now click the icon a pop-up window appears asking if you want to execute the program.
Just click Execute.


And there is the Arduino IDE 2.2.1
As you can see I installed the ESP32, ESP8266 and Raspberry Pi Pico boards and the ESP8266 programs now compile without a problem.

Appimage pro's and cons

This is a great tool but there are some pro's and cons you should be aware off.

The pro's

- Appimage's should work on any computer running Linux.
- Any user can install a program packed as an Appimage. No need to get root privileges or using the terminal with sudo to install software.
- Everything needed to install and run a program is contained in the appimage of that program. So no need to install extra programs or libraries.
- Appimages do not alter your system libraries or congiguration. So it will not cause problems with other programs.

The biggest benefit is of course that appimages run on any Linux distribution.

The con's

- The standard program installation methods like Discover on Ubuntu (Kubuntu) regularly check if there are new updates for your programs. Appimage does not do that. So you will have to check for updates yourself regularly.
- Not all programs or applications are available as an Appimage
- The installed Appimages are not shown in your Application Launcher or program menu. You have to keep track yourself where they are stored.

Removing an Appimage

If you need to remove an Appimage because you do not need the program anymore or want to download an update just find the Appimage in your directory and delete the file. That's all.

Till next time.
Have fun


Luc Volders