The PiicoDev Ultrasonic Rangefinder uses sound waves to measure the distance to an object. This is like how bats can Echo locate or how submarines use sonar. The sensor emits a high-pitched sound wave that travels through the air to some object. It reflects off the object and comes back to be received by the sensor. The rangefinder measures the duration of that round trip and uses the speed of sound to calculate a distance. That also means these sensors work best when pointing perpendicular to smooth surfaces. Angled surfaces can reflect sounds away giving spurious readings and soft materials may dampen the reflected sound or not reflect any sound at all. Water is actually a useful surface and so you sometimes see these sensors being used to measure the height of liquid in a tank.
G'day, I'm going to show you how to get started with the PiicoDev Ultrasonic Rangefinder and a Raspberry Pi Pico. We'll connect these two together and experiment with measuring distances. But first, let's take a tour of the Ultrasonic Rangefinder bundle.
The Ultrasonic Rangefinder consists of two parts. There's the ultrasonic sensor and the smart module. The module features two PiicoDev connectors for daisy-chaining with other PiicoDev modules. There are four ID switches for address configuration. Make sure all of these switches are off before proceeding and there's also a user controllable LED on board which turns on by default, kind of like a power LED.
Plug the sensor into the full pin header on the smart module such that it points outwards. If you connect the sensor incorrectly, it may become damaged. To follow along, you'll of course need an assembled PiicoDev Ultrasonic Rangefinder, a Raspberry Pi Pico or Pico W with the headers soldered facing down, a PiicoDev expansion board for Pico, and a PiicoDev cable to connect everything. loop and inside that loop we call the Ranger dot distance function and that returns the distance in millimetres between the sensor and whatever is in front of it and then we print that value to the shell and then we pause for a second and then we go back to the top of the loop and do it all over again
Plug your Pico into the expansion board such that the USB connector is on the same side as the two pin battery connector. Plug your cable into the expansion board and connect the other end to your rangefinder. Now we can connect to a computer with USB. Find the download section in the article and save the Python modules listed. Right click each link and select 'Save Link As'. I'm saving mine to a 'PiicoDev' folder I created in my documents.
I've opened Sonic connected to my Pico. You can see it here in the file pane and I'll select both of these files that I downloaded and right click to upload to the Pico. There they are. Return to the article and find the section for measuring distance. Copy that example code and paste it into a new script. Click run and I'll save this to my Raspberry Pi Pico as 'main.py'.
We immediately see some numbers streaming in the shell and if we right click and show the plotter we can see a plot of those values. This is the distance that the sensor is measuring in millimeters. You can see it's kind of all over the place at the moment. If I bring my hand in we get a much more stable reading. If I move that back and forth we can cause that line to go up and down. So we are measuring the distance between the sensor and my hand and you may also be able to see that the onboard LED is flashing away.
Let's take a closer look at the code. Like all PiicoDev projects, we start by importing the device module. Here we're importing the PiicoDev ultrasonic class and we also import a sleep function. Next we initialize the ultrasonic Rangefinder by calling the initialization function and that returns an object. We're going to call that object 'Ranger'. So anytime we see the word 'Ranger' in this script, we're referring to this physical he could have ultrasonic Rangefinder. Next there's an infinite loop and inside that loop we call the Ranger.distance function and that returns the distance in millimetres between the sensor and whatever is in front of it. Then we print that value to the shell and then we pause for a second and then we go back to the top of the loop and do it all over again. this and now if I move the box closer than 100 millimetres the LED will turn on
Looping through an infinite loop, we call a print statement to print a value to the Shell. In this case, the value is Ranger.distance in millimeters, which is the latest distance sample in millimeters. If you prefer imperial you could read Ranger.distance inch. We are then setting Ranger.led to not Ranger.led, which will toggle the state of the LED. After a 0.1 second delay, we use a ruler and a box as a reflector to look at the response of the sensor. Holding the sensor in line with the start of the ruler, we get a reading in millimetres that is within 10 millimetres of the range. Moving the box back to 40 centimeters, we get a reading of 380 millimeters, which is within about two centimeters.
As a quick remix, we create an easy proximity alert. We compare the distance being measured to some reference and if we're too close, we can turn the LED on or print a message to the Shell. We set the state of Ranger.led to Ranger.distance in millimetres is less than 100, which will evaluate as a true or false and be used to set the state of the LED. Now, if the box is moved closer than 100 millimeters, the LED will turn on.
This script again holds my hand in front of the sensor and here we are at around 240 millimeters. The LED is off and if I move my hand closer, oh there's a little bit of a flickering as I get close to the edge there. But then when I'm closer than 100 millimetres we can see that LED is on, off, on, off, on.
Now it's also possible to run multiple ultrasonic rangefinders on the same PiicoDev bus. Recall that we're already working with a rangefinder that has all its ID switches off. To add a second rangefinder, we first need to set a unique ID. For this example, we'll set ID switches one and three, then daisy-chain the new rangefinder onto the PiicoDev bus.
Now in the code, we can initialize the two different rangefinders. The first one I'm calling range A and passing the ID argument with all zeros to indicate that all the ID switches are off. The second one I'm calling range B. It has ID switches one and three in the on position and so the ID argument has ones in the first and third position. The ID argument matches the switches.
When we run the code, we can now sample and plot two different ranges independently. Now there's all sorts of practical projects you could do from here. Perhaps you could upgrade this project to use an audible alert if something is getting too close to the sensor or to trigger an alert if somebody walks through a door. You could create a musical instrument that you can play without touching.
Be sure to check the article for some project inspiration and there you have it, using sound to measure distance. If you have any questions or just want to share a cool PiicoDev project that you're working on, then tell us about it over in the forums. Until next time, happy making! Wow!
Makers love reviews as much as you do, please follow this link to review the products you have purchased.