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.
Good! I am going to show you how to get started with the PiicoDev Ultrasonic Rangefinder and a micro:bit. We'll connect these two together and get some examples running to measure distance and then we'll remix that code to do something fun.
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. 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 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 MicroBit V2 and a Pikadet adapter and cable to connect everything together. Start by plugging your MicroBit into the adapter making sure the buttons are facing up. Connect your PiicoDev cable to the adapter and connect the other end to your rangefinder. Then connect to...
Your computer with USB is returning to the guide, find the download section and download the device drivers. Right click on Picket of Unified and select 'Save Link As' and I'll save this to a PiicoDev directory in my documents. Repeat for PiicoDev Ultrasonic.py.
If only connect to your MicroBit and navigate to where you saved those files, upload both of those to your micro bit. Select them both, right click and upload to microbit. Here they are, we're ready to measure distance. Find the measure distance example in the guide and highlight all of that code, copy it and paste it into a new script in Funny. Click run and if prompted save it to your MicroBit as main.py.
We immediately have some data coming into the shell and you can see that being plotted on the right here. If I hold my hand in front of the sensor, that data settles down and I can actually move my hand back and forth and change that distance reading. If I have a reflective surface here, this is out at about 500 millimetres and I can bring it in to about 100 millimeters. You may also notice that the onboard LED is blinking here.
Let's take a closer look at this example code. We start by importing the PiicoDev Ultrasonic Python module to drive the sensor. We also import a sleep function to create a delay. Next, we initialize the sensor. We call the initialization function and that returns an object. We assign that object to Ranger, so anytime you see the word Ranger in this script, we're referring to this physical he could have ultrasonic Rangefinder.
Next, we have an infinite Loop and in that Loop we print Ranger.distance_millimeters. This is a property of that object that shows the last distance measurement in millimetres and that's the data that we see in the Shell here, that's these numbers. These are all ranges in millimeters. If you prefer.
We can measure distance using the ultrasonic Rangefinder and we can also blink its onboard LED. Let's remix this code to create an audible alert whenever an obstacle gets too close to the sensor. A pretty common feature in a lot of modern cars, they might have a reversing sensor that gives you a beep if you're getting too close to something. We can start by importing music that will drive the speaker. We can print the range that's still fine, but we can also put in an if statement.
Imperial you could use distance inch that would also work. Just to show we can, the next line toggles the onboard LED. We call ranger.led is equal to not ranger.led so that just turns on to off and off to one. Finally, there's a short delay.
I've set up a ruler between the rangefinder and this nice acoustically reflective box to do a bit of an accuracy experiment. The box is sitting at 30 centimetres and we're getting a reading of 291 millimeters. So it looks like the range might be measured from the very edge of the sensor here. If I move this box down to 200 millimetres we get about 210 and at 100 millimetres we're getting about 110 as well. So we're within about a centimeter and that kind of makes sense.
There's a couple of assumptions here. This is a timer flight sensor and the speed of sound in air will change depending on local weather conditions like air pressure humidity. So you can see these sensors are good to within a few centimeters. Let's go for a maximum range in this room here. I'm at about half a meter and I'll bring my target back. I've just gone to about 1.4 1.5 meters and if I move this out of the way then we get some large nonsense value. If I bring this back in we're back at about one and a half meters and all the way back here I'm having trouble going past two and a half meters. Cool science.
If Ranger's dot distance in millimetres is less than 100, then we want to play a tone we call music.dot pitch and we'll create an 880 Hertz tone for 100 milliseconds. To make things a little more simple, I'll just remove this LED blink. Now when I run the code, it's behaving very much the same as before. I can get a stable range, but if I get too close, we get a beef.
Cool, so we've remixed this project into something that's a little more practical. We have an audible proximity alert 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. 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.
Makers love reviews as much as you do, please follow this link to review the products you have purchased.