My Cart:

0 item(s) - £0.00
You have no items in your shopping cart.


Distance Sensing With A microbit And HC-SR04 Module

Setting up distance sensing with the BBC microbit and a HC-SR04 sensor is a fun and easy project. We have two great sensors for our fabulous customers, one works at a little over 3V and is available here, and the other works better on 5V and is available here.
Distance Sensing -microbit-sonar-hc-sr04-5

Adding distance sensing is a great way of levelling up your robot/buggy projects. It allows you to explore things like autonomy and automatic hazard avoidance.



The other components we use are:

Most of these (with the exception of the 3xAAA Battery Pack) are included in the BBC micro:bit Inventor's Kit.


Distance Sensing With A microbit And HC-SR04 Module:

Laying out the HC-SR04 ultrasonic sensor is quite simple, especially with the 3V sensor. It only has four pins. Left to right these are VCC, Trig, Echo and GND. VCC and GND are for power and Trig, Echo and GND are for your signal. GND is included in with the signal set since it's needed for a baseline. One of my common early mistakes with sensors and things like smart LEDs was not connecting all components to a common ground. I've included a picture and a schematic of my set up.

The easiest pins to use on the microbit are 0, 1, and 2. So I used 0 for Trig and 1 for Echo.

Distance Sensing -microbit-sonar-hc-sr04-1

Adding the battery pack adds only a little complexity, it's worth noting that we have removed the power from the microbit and connected the black wires (GND or Ground) together. This gives the circuit a common baseline 0V, which it is worth noting again, is important when measuring electronic signals.


Once the hardware is assembled you can set up your software.



As you might be aware, the microbit is programmed in the browser, you can head to to start programming your sonic sensor. First, you'll need to add a new package to the standard set. I've used an extension called Sonar, extensions were previously called packages.


Finding the Sonar extension is done by a simple search and then just click add it to your blocks and set up your code as is shown here.



Use the bargraph command to display the sensor data (the variable called item) because it's a simple way to show that the sensor is working. More info on bargraph can be found here: Although amazingly I had to look at the code on GitHub to find out that leaving the bottom value as 0 allows it to auto adjust. Change this to set your maximum point that you want to measure. It will give data in whole numbers (integers) and in the units you select. I believe the sonar block is doing the conversion calculations for us.

By switching to print the sensor data to screen, you can begin to get a feel for the distances you wanted to measure and code for. If you're following along you'll have noticed that there are a lot of zero values, which cause a flickering of bargraph or other output. I thought it was easiest to filter these out with an if statement (found in logic). This was also the easiest way I found to work using LEDs like ZIP LEDs.


More Hardware:

Finally, and my favourite part of this project is adding some outputs, here we use some LEDs like a traffic light. Setting up the LEDs as shown:


And then make the code as shown here:

You can quickly make a distance sensor and feedback system!


With all being correct you can now also experiment with the code and even add some more inputs. Additionally, a servo motor would be great for pushing away items!


Same Day Dispatch
← Previous Post Next Post →

Leave a Reply