BC Robotics

Getting Started With The ultrasonic:bit For BBC micro:bit

  PRODUCT TUTORIAL

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! 

Requirements:

This tutorial requires several items:

0%

Step 1 - The Hook Up

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! 

7.6%

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. 

15.3%

Step 3 - Add Extensions

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

23.1%

Step 4 - Sonar

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.

30.7%

Step 5 - Starting The Code

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! 

38.5%

Step 6 - Distance Variable

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. 

46.1%

Step 7 - Setting The 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.

53.8%

Step 8 - Add Sonar Block

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.

61.5%

Step 9 - Configuring The Sonar Block

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

69.2%

Step 10 - Display A Number

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

76.9%

Step 11 - A Brief Pause

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

84.6%

Step 12 - Download And Try It Out

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! 

92.3%

Step 13 - Time To Measure

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!  

100%

2 thoughts on “Getting Started With The ultrasonic:bit For BBC micro:bit”

Leave a Reply

Your email address will not be published.

Select the fields to be shown. Others will be hidden. Drag and drop to rearrange the order.
  • Image
  • SKU
  • Rating
  • Price
  • Stock
  • Availability
  • Add to cart
  • Description
  • Content
  • Weight
  • Dimensions
  • Additional information
  • Attributes
  • Custom attributes
  • Custom fields
Click outside to hide the comparison bar
Compare