Please note: European delivery is temporarily disabled due to significant delays and other issues (Important info for EU customers)

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. distance-sensing-microbit-sonar-hc-sr04-3 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. distance-sensing-microbit-sonar-hc-sr04-2 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. distance-sensing-microbit-sonar-hc-sr04-10 distance-sensing-microbit-sonar-hc-sr04-7 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: distance-sensing-microbit-sonar-hc-sr04-9 And then make the code as shown here:
You can quickly make a distance sensor and feedback system! distance-sensing-microbit-sonar-hc-sr04-4 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! distance-sensing-microbit-sonar-hc-sr04-8

Kitronik's suggested additional learning

BBC micro:bit Line Following Buggy Build Instructions

This is a step by step guide to building the BBC micro:bit Line Following Buggy. The buggy uses two light dependant resistors to control a line following board on the bottom of the chassis to 'follow' black lines.


Mark Donnison

Mark Donnison

Without some testing, it is difficult to say. However, as you will not be drawing power through the microbit, your limit should be determined by the availability of suitable pins on the microbits edge connector. Maybe this is one for the testing room at some point.



1.How many Ultra Sound sensors can be managed with one microbit controller at the same time? 2.whether car sensors can be used? Thanks

Mark Donnison

Mark Donnison

Hi Michael, I've just tried it and it took around 10 seconds to load and then it was fine. Try waiting a little while before clicking, then when it loads click on the Edit icon in the top right of the embedded editor. It should open for you, let us know if you are still having issues and we will log a ticket for you.

Michael Grazebrook

Michael Grazebrook

I'm trying to use this: Where it says: “And then make the code as shown here:” I'm unable to see the code. I tried clicking on the edit button in the top right and it just brings me to with a message “Network request failed”. The image is an “On start” block and a small stubby brown thing by it. I haven't used blocks before (I normally use Python)

Leave a comment

All comments are moderated before being published