Let's connect a PiicoDev Distance Sensor to a Raspberry Pi Pico and perform some distance measurements readings. This tutorial will guide you through wiring, uploading example MicroPython code in Thonny, and remixing the example to include new functionality. The PiicoDev Distance Sensor uses a VL53L1X which can measure indoor ranges up to about 4m with 1% repeatability!

Transcript

"Let's measure distance using our PiicoDev distance sensor and a Raspberry Pi Pico. We'll wire these two together, get some example code working to measure distance in millimeters, and then we'll remix the code to do some decision making. Let's get started.

To follow along, you'll need a Raspberry Pi Pico with the pin soldered facing down, a PiicoDev distance sensor and expansion board for Raspberry Pi Pico, and a PiicoDev cable to connect everything together.

Plug your Pico into the expansion board so that the USB connector is on the same side as the 2-pin battery connector, and just double check that pin number 0 on the expansion board is to the left of the USB connector. At the bottom of the expansion board, connect your PiicoDev cable, and then connect the other end to your distance sensor. And I'm going to keep everything secure on a PiicoDev platform. Then connect to your computer via USB.

In the article for this tutorial, find the Downloads section and download the three files that we'll need for this tutorial. Right click each link and select Save As. I'm saving mine to a PiicoDev folder in My Documents.

Next open Thonny and use the File pane to navigate to where you saved your files. If you need help getting started with Thonny, we have a guide for that too. Select the three files with a shift and a click, and then Upload To. And we can see on the Raspberry Pi Pico, those three files are now uploaded.

Jump into the shell and press Ctrl D for a restart, and we're now reading distance in millimeters. We even have a plot on the right hand side. If I bring my hand over the sensor, we can get a very stable reading, and I can get that to climb up and fall down.

In the shell, press Ctrl C to halt the execution, and we'll have a look at what's going on in the example code. Let's work with the file that's uploaded to the Pico.

Under"Pi Pico itself. If we want to save a copy of this code on our computer, we can do so by clicking on the file menu and selecting save as. We can then save the file anywhere on our computer and have a copy of the code that we can edit and modify without having to connect to the Pico.

Now, let's say we want to take the code we've written and turn it into a program that we can run on the Pico without having to have it connected to a computer. We can do this using a tool called Thonny. Thonny is a code editor that has some built-in features that make it easy to write and run Python code on the Pico.

To get started, we'll need to download and install Thonny. We can do this by going to the Thonny website and downloading the appropriate version for our operating system. Once we've installed Thonny, we can connect our Pico to the computer and open Thonny. Thonny should automatically detect our Pico and display it in the bottom right corner of the screen.

To create a new file, we can click on the "New" button in the toolbar or go to the file menu and select "New". We can then enter our code into the editor window just like we did with main.py. Once we've finished writing our code, we can save it by clicking on the "Save" button or going to the file menu and selecting "Save".

To run our code on the Pico, we can click on the "Run" button in the toolbar or go to the "Run" menu and select "Run current script". Thonny will then upload our code to the Pico and run it. We should see the output of our code in the shell window just like we did before.

And that's how you can use Thonny to write and run Python code on the Raspberry Pi Pico. With Thonny, you can quickly and easily develop your own programs and experiment with different ideas without having to worry about uploading code to the Pico manually.Pi Pico.

To back up our changes to our computer, we can control C to stop the execution, and then right click that main.py file and download it to our computer, which is going to override the example that we originally downloaded.

And now if we open the file in our PiicoDev directory, it's updated with the changes that we made.

And there you have it, a quick example using the PiicoDev distance sensor, and then also including a little bit of branching logic so you can change your program's behavior.

If you make anything cool out of this starter project, we'd love for you to share it over on the Core Electronics forums. That's also the best place to ask technical questions.

Thanks for watching.

Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.