G'day, I'm going to show you how to get started reading distance using your PiicoDev distance sensor and a Raspberry Pi. We'll wire these two together, download the necessary packages and start taking some readings. Let's get started.
To follow along, you'll need a Raspberry Pi setup to run like a desktop computer, a PiicoDev adapter for Raspberry Pi, of course the PiicoDev distance sensor and a PiicoDev cable. I recommend cables 100mm or longer for Raspberry Pi projects. It's best if you're already comfortable using a Raspberry Pi like a desktop computer. If you need help with that, check out the Raspberry Pi for beginners workshop.
Connect the adapter to your Raspberry Pi, making sure the arrow is pointing towards the ethernet socket or a USB socket if you're using a Raspberry Pi 3. Connect one end of your PiicoDev cable into one of the ports of the adapter and connect the other end to your distance sensor. And I've just mounted all my hardware onto a PiicoDev platform for Raspberry Pi, just to keep everything stable for this tutorial.
Make sure you have the I2C interface enabled on your Raspberry Pi. Go to the Pi menu, preferences, Raspberry Pi configuration. Under the interfaces tab, make sure you have I2C enabled.
Navigate to the tutorial page, find the example download link next, right click the link and save link as. And I'll save this into a folder that I have in my home directory called PicoDev. And I'll name this file distance, just for some context. When that's finished downloading, you can find that wherever you saved it. Double click to open in Thonny.
Let's make sure we have the PiicoDev packages installed. Go to tools, manage packages.And search for PiicoDev with two I's. Close the packages and just click install or upgrade if you need to, to get to the latest version.
And this example is ready to run. Just hit the green run current script button. And you should see distance data being streamed into the shell. If I move my hand, that distance data will change. And I've got my plotter open to the right here. So you can also see that wave that I'm creating by moving my hand up and down. If you'd like to see the plotter and you don't see it, open the view plotter option.
Let's take a quick tour of the example code. We have the first line, which just imports the package that will help us drive the sensor. We also import a function to create a delay so that we can pause the script for some amount of time. And then we create a variable called distance sensor and make that an instance of the PiicoDev distance sensor. So when we want to read the distance, this is the object we'll refer to.
There's an infinite loop, the while true. And then we call our distance sensor with dot read to read the distance in millimeters. This gets saved or assigned to a variable called dist. Next up, we call print to show the data on screen and we convert dist, which is a number into a string so that it can be printed. We then concatenate that with plus and we concatenate that with a space MM string. And that's just for millimeters. And that's how we can get this number with a space and millimetres showing in the shell. Finally, there's just a 0.1 second sleep before the next loop.
Let's remix this code so that it gives us some kind of message depending on how close something might be. We'll stop the script.And we'll create an if condition, an if statement. If dist is less than, let's say, 50 millimeters, then print really close. And we can say else if, else if dist is less than, I don't know, let's say 150 millimeters. We can say pretty close. Else if dist is less than, let's go with 500. And we can print close.
And then finally, if none of those conditions are met, we can say else print far. Okay. And I'm just going to remove this other print statement just with a comment. So I can comment that with a hash symbol or use alt 3 to comment that out.
And if I run the script now, it's constantly plotting or constantly printing far. And then if I bring my hand closer and closer, we're now at the close threshold. Now we're pretty close and we're really close. And we can step back through.
And so by creating this simple if else chain, we've set up different thresholds that we can create different behavior with. And there you have it, some instant gratification reading distance with a PiicoDev distance sensor and also doing a little bit of decision making as well.
If you have any questions, just leave a comment in this article or open a thread on the Core Electronics forums. We're full-time makers and here to help. Until next time, catch you later.
Makers love reviews as much as you do, please follow this link to review the products you have purchased.