Let's measure distance using our PiicoDev distance sensor and a Micro:bit V2. I'll show you how to connect these two together and run some example code to measure distance, and then we'll even make a little interactive musical instrument. I'll show you how.
To follow along, you'll of course need a Micro:bit V2, a PiicoDev distance sensor and adapter for Micro:Bit, and a PiicoDev cable to connect everything together.
Start by connecting your Micro:bit into the adapter, making sure the buttons are facing up. Plug the PiicoDev cable into one of the connectors, and connect the other end to the distance sensor. And I'm going to use a PiicoDev platform to keep everything nice and secure. And connect to your computer with a USB cable.
In the article for this tutorial, find the download section and download the three files that we'll need. Right click each link and select save as. Save these somewhere that makes sense. I'm saving mine to a PiicoDev folder I've made in my documents.
Open Thonny and in the files pane, navigate to where you saved those files. If you need help getting started with Thonny for Micro:Bit, we have a guide for that too. If you prefer not to install Thonny, you can still program your Micro:bit by navigating to python.MicroBit.org and program in your browser. We have a getting started guide for using that with Micro:bit as well.
Go to run and select interpreter, and make sure we have MicroPython for BBC Micro:bit selected. And we may have to stop and restart the backend. And I can see the Micro:bit has connected.
Select all three files by selecting the top one with a left click and hold shift, click the bottom to select all three.I'll right-click upload to micro bit. And this is ready to run.
In the shell, if I press control D, we'll reboot the Micro:bit and we now have live distance data streaming up the shell. And in the plot, we have a plot of our distance in millimeters. If I bring my hand over the sensor, I can vary the distance and make a nice wave pattern. How nice.
Let's have a look at what's going on with this example. In the shell, press control C to halt the script and we'll open main.py from inside the micro bit. So we'll work with the file stored on the micro bit. These other two files are just drivers for this sensor.
Double click main.py and we can see that in the first line, we import the device module to drive the laser distance sensor. And we also import a function called sleep MS to create a delay in milliseconds. We initialize the distance sensor as dist sensor. And then in an infinite loop, we just call dist sensor dot read to read the distance in millimeters. And we assign that to the variable dist. Then in the print statement below, we convert dist into a string so we can easily print it with the millimetres unit. So this prints a string of the distance in millimetres and concatenates that with the string mm for millimeters. And then there is a 100 millisecond sleep.
In the article for this tutorial, you'll find a little bit of a code remix where we create a musical instrument using this distance sensor as an input device. The Micro:bit V2 has an onboard buzzer, so wouldn't it be nice if we could read the distance of our hand to the sensor and use that to change what musical note is played by the buzzer? Let's check it out.
Find that remix code andCopy it into this script. We'll just select all and paste. And now I'll save it with control S.
All right, we can see there's a little bit more going on, but let's just give it a demo for now. Press control D to reboot the Micro:bit and we can see that nothing's happening. Let's take a closer look.
In the infinite loop, the while true loop, nothing happens until the A button is pressed. That's the button labelled A on the left side of the micro bit. And I've put this in so that we don't constantly play musical notes. That would be a bit annoying. But if I hold my hand over the sensor and press A, we get a note. And if I move my hand to a different height, we get another note.
Let's have a closer look at what's going on. The script starts with similar imports. This time from Micro:Bit, we import button A so we can read that button. And we also import music. We import the same PiicoDev functions as before and initialize the sensor. But this time there's an array of notes. And these are the notes that we can play with our musical instrument.
Then if button A is pressed, we read the distance just like before. But this time we divide the distance into 75 millimetre chunks. The distance between each note is 75 millimeters. We round that to the nearest whole number. And then take the minimum of whatever that number is and 7. Because if my hand was all the way up here, we could read a very high number. But I only have eight notes defined in this list. So we take the minimum with 7 because we can also have number 0. Counting from 0, 1, 2, 3, 4, 5, 6, 7. So all this line does is convert the distance in millimetres to the note number between 0 and 7.
We printA little bit of helpful feedback. That's printing this line here where it's converting the distance in millimetres to the note that we're going to play. And then we call music.play to play that particular note.
Let's take it for a spin. So there you have it. Reading distance from a PiicoDev distance sensor and remixing it a little bit to make a fun musical instrument.
If you make anything cool out of this little starter project, we'd love for you to share it on the Core Electronics forums. And until next time, thanks for watching.
Makers love reviews as much as you do, please follow this link to review the products you have purchased.