The micro:bit is quite the amazing little device – it is packed with features and best of all, it is easy to use. However, there are some sensors that just don’t work well with the micro:bit directly. The micro:bit runs at a 3V logic voltage and works with any sensors running 3V as well – but some sensors require a higher voltage. One of these higher voltage sensors is the common Ultrasonic Distance Sensor. These distance sensors have been a staple of Arduino projects in the past but they do not work properly when powered with 3V through the micro:bit. The ultrasonic:bit takes care of this problem – all you have to do is hook it up and measure distance!
About the Board:
The ultrasonic:bit takes all of the complicated wiring out of using an ultrasonic distance sensor with the BBC micro:bit. The board simply steps up the 3V from the micro:bit and provides the sensor the 5V power source it needs to operate correctly. Since the 5V output data from the sensor would normally damage the micro:bit, the ultrasonic:bit also handles changing this 5V signal back down to a 3V signal that is safe for the micro:bit. Not sure what all of that means? No worries! Just hook it up like any normal 3V sensor and the board will take care of the rest!
This tutorial requires several items:
Connecting the ultrasonic:bit to the BBC micro:bit is easy! We will just need to connect 4 wires between the two devices. Connect the wires as shown in the image:
Once they are all connected, double check they are all going to the right places!
Once you have double checked all of your connections you can plug your BBC micro:bit into your computer. Open up a web browser on your Computer / Tablet and head on over to https://makecode.microbit.org/ and open the blocks editor.
We are going to create a simple program that continuously takes a distance reading and displays it on the LED matrix. Fortunately, there is an extension for Make Code that triggers the Ultrasonic Sensor and returns a value. We are going to start by adding this extension by clicking the settings button in the top right and selecting “Extensions” .
We will start by removing the “on start” block – as we wont need it. Select the block and hit the delete key to remove it. This will leave us with the forever block – the forever block continuously runs – which is exactly what we are going for, as we want our micro:bit to continuously measure distance. Now we can start adding some blocks!
The Sonar code block returns the distance in the form of a number – we need to store this somewhere. Create a new variable by selecting the Variables Tab and clicking “Make a Variable”. We will call this variable “distance”. Enter that into the box and click Ok. Now, anytime we need to distance reading from the sensor we can just request it from this variable.
To relate our variable to the number we get from the distance sensor we need to add a “Set variable” block. We are going to set the variable “distance” to a number using the Sonar library. First, add the block: “Set distance to” and position it inside the forever block as it looks in the example photo.
Next, we need to open the Sonar Tab on the side bar, and add the Sonar block inside the “Set Distance to” block. Click and drag the Sonar block right over the zero and drop it in. Now, the reading from the sonar sensor is connected to the distance variable. In other words, distance is equal to Sensor Output.
Now that the Sonar block is added, we need to configure it to match the pins we connected everything to on our micro:bit . We connected the trigger (ping trig) to P0 and echo to P2 so select the correct values from the dropdown boxes. The last box allows you to choose whether the returned value is in centimeters, inches, or microseconds (time the signal takes to bounce back and forth).
Once it is running, the number will display on the LED matrix. If this number is two digits or more, it will scroll from right to left. Going forwards you could use this module to build a simple collision alert (if the distance is below a certain value, flash all LEDs) or use it to guide a small robot – let your imagination run wild!
2 thoughts on “Getting Started With The ultrasonic:bit For BBC micro:bit”
Michael Carroll
Is the name of the circuit board for the “1 x ultrasonic:bit Distance Sensor”? I cannot find where to purchase it? Can you Please send me the exact name of the board. I tried Kitroniks, RobotSHop, PISHop;Dfrobot?
Thanks;
MIchael
Michael Carroll
Found on your website. Sorry to bother you.