Let's get started measuring distance with the PiicoDev® Ultrasonic Rangefinder. We'll connect this sensor to a Raspberry Pi and begin measuring distance - then remix the code to change how it works!

Transcript

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.

Now I'm going to show you how to get started with the PiicoDev Ultrasonic Rangefinder and a Raspberry Pi. We'll connect these two together and get some example code running to measure distance. We'll also remix that code but first a bit more about the rangefinder. The PiicoDev 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, four ID switches for address configuration (make sure all of these switches are off before proceeding), and a user controllable LED onboard 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 single board computer, a PiicoDev adapter for Raspberry Pi, and a PiicoDev cable to connect everything together. ultrasonic Rangefinder

Connect your adapter to the Raspberry Pi. On a Raspberry Pi 4, there will be an arrow that points towards the ethernet socket. Connect your cable to the adapter and connect the other end to your ultrasonic Rangefinder. Boot up your Pi and make sure that it's ready to run PiicoDev projects.

First, go to the Preferences > Raspberry Pi Configuration menu and under the Interfaces tab make sure that I2C is enabled. In Thonny go to Tools > Manage Packages and search for Picadev. Make sure that you install or upgrade PiicoDev as necessary.

We're ready to measure distance. In the guide, find the Measure Distance example and copy all of that code into a new script in Funny. I'll save the script to a PiicoDev directory in my home directory, Measure Distance.py. Click the Green Run button and you ought to see something coming out in the Shell. These are the range measurements in millimeters.

If I hold my hand in front of the sensor, you can see it's at about 16 to 20 centimetres away. If I right click and show the plotter, we can see that my hand doesn't make a very good Target for this. It's not super reliable because I'm kind of soft and Squishy. I've got this nice smooth reflecting box and we can see that this makes a much nicer Target. You can see that line going up and down much more smoothly as I bring it closer or farther from the sensor. You might also notice that the LED on board the sensor is flashing.

Let's take a closer look at the code. We begin like all PiicoDev projects do by importing the device module in this case, he could have ultrasonic. We also import a function to create a delay. We initialize the ultrasonic Rangefinder by calling the initialization function and that returns an object. We call that object Ranger, so whenever you see the word Ranger in this script we're referring to this physical ultrasonic Rangefinder.

Ultrasonic Rangefinder has an infinite loop and we print the attribute Ranger.distance millimeters. This just returns the latest distance sample in millimeters. If you prefer imperial  you could print distance inch and on the next line we just toggle the state of the LED by setting the Ranger.led property to not Ranger.led. This will read the state of the LED and invert it, toggling the LED and creating a continuous flashing.

Running the script again, I'm holding this box about 100 millimetres from the sensor and we're reading about 94 millimeters. I can slide this box back to let's go for 300 and you can see we're within about 10 or 20 millimetres and that makes sense. We're assuming a speed of sound in air to make these distance measurements and of course that will vary with local weather conditions like air pressure and relative humidity. It also changes with temperature.

Let's create a visual indicator when the Ranger is measuring less than 100 millimeters. We can turn the LED on if something gets too close to the sensor. That's pretty easy to do here. We're already setting the state of the LED and we're sending it to not LED to toggle it. We could put an expression here that evaluates to true or false. In this case, we could set it to Ranger.distance in millimetres is less than 100. Because this expression will evaluate to true or false, we can just pass that directly into Ranger.led.

If we run the code, we can see that the LED is off to begin with. If I bring my target in slowly, up there it is.

We're just on the changeover point and now we're definitely closer than 100 millimeters. As I pull it back, the LED turns off again. 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 1 and 3. 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.

Comments


Loading...
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.