Getting Started With The ultrasonic:bit For BBC micro:bit
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!
[tutorialStep title=”Step 1 – The Hook Up” image=”https://bc-robotics.com/wp-content/uploads/2019/09/hookup.jpg” imageLink=”https://bc-robotics.com/wp-content/uploads/2019/09/hookup.jpg” imgSide=”left”]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:
- 3V to the 3V pin (red)
- GND to the GND pin (black)
- Trigger to Pin 0 on the micro:bit (green)
- Echo to Pin 2 on the micro:bit (yellow)
Once they are all connected, double check they are all going to the right places![/tutorialStep]
[tutorialStep title=”Step 2 – MakeCode” ]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.[/tutorialStep]
[tutorialStep title=”Step 3 – Add Extensions” image=”https://bc-robotics.com/wp-content/uploads/2019/09/1.jpg” imageLink=”https://bc-robotics.com/wp-content/uploads/2019/09/1.jpg” imgSide=”right”]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” . [/tutorialStep]
[tutorialStep title=”Step 4 – Sonar” image=”https://bc-robotics.com/wp-content/uploads/2019/09/1a.jpg” imageLink=”https://bc-robotics.com/wp-content/uploads/2019/09/1a.jpg” imgSide=”left”]There are a lot of extensions for the BBC micro:bit – we are looking for a specific one that has been designed for ultrasonic sensors. Search for “Sonar” – once you have found it, click on it to add it to your project. [/tutorialStep]
[tutorialStep title=”Step 5 – Starting The Code” image=”https://bc-robotics.com/wp-content/uploads/2019/09/2.jpg” imageLink=”https://bc-robotics.com/wp-content/uploads/2019/09/2.jpg” imgSide=”right”]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! [/tutorialStep]
[tutorialStep title=”Step 6 – Distance Variable” image=”https://bc-robotics.com/wp-content/uploads/2019/09/3.jpg” imageLink=”https://bc-robotics.com/wp-content/uploads/2019/09/3.jpg” imgSide=”left”]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. [/tutorialStep]
[tutorialStep title=”Step 7 – Setting The Variable” image=”https://bc-robotics.com/wp-content/uploads/2019/09/4.jpg” imageLink=”https://bc-robotics.com/wp-content/uploads/2019/09/4.jpg” imgSide=”right”]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. [/tutorialStep]
[tutorialStep title=”Step 8 – Add Sonar Block” image=”https://bc-robotics.com/wp-content/uploads/2019/09/5.jpg” imageLink=”https://bc-robotics.com/wp-content/uploads/2019/09/5.jpg” imgSide=”left”]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. [/tutorialStep]
[tutorialStep title=”Step 9 – Configuring The Sonar Block” image=”https://bc-robotics.com/wp-content/uploads/2019/09/6.jpg” imageLink=”https://bc-robotics.com/wp-content/uploads/2019/09/6.jpg” imgSide=”right”]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). [/tutorialStep]
[tutorialStep title=”Step 10 – Display A Number” image=”https://bc-robotics.com/wp-content/uploads/2019/09/7.jpg” imageLink=”https://bc-robotics.com/wp-content/uploads/2019/09/7.jpg” imgSide=”left”]To display the value we are going to grab another block form the Basic tab. Drag the “show number” block underneath the “set distance” block as shown. We will then go back to the Variables tab and grab our variable “distance” and drag it to replace the 0 in “show number”. [/tutorialStep]
[tutorialStep title=”Step 11 – A Brief Pause” image=”https://bc-robotics.com/wp-content/uploads/2019/09/8.jpg” imageLink=”https://bc-robotics.com/wp-content/uploads/2019/09/8.jpg” imgSide=”right”]Finally, we need to slow the code down a little – jump back to the Basic tab and add a “pause” under our “show number” block. Set it to 1000 so that our sensor takes a reading and displays it every 1000 milliseconds (1 second). [/tutorialStep]
[tutorialStep title=”Step 12 – Download And Try It Out” image=”https://bc-robotics.com/wp-content/uploads/2019/09/9.jpg” imageLink=”https://bc-robotics.com/wp-content/uploads/2019/09/9.jpg” imgSide=”left”]That’s it – hit the download button and follow the onscreen directions to load it to your micro:bit. Once it loads, it will start measuring! [/tutorialStep]
[tutorialStep title=”Step 13 – Time To Measure” image=”https://bc-robotics.com/wp-content/uploads/2019/09/done.jpg” imageLink=”https://bc-robotics.com/wp-content/uploads/2019/09/done.jpg” imgSide=”right”]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! [/tutorialStep]
[info]Have A Question?
If you have any questions, or need further clarification please post in the comments section below; this way future users of this tutorial can see the questions and answers!