My Cart:

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

0

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.

 

Hardware:

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.

 

Software:

As you might be aware, the microbit is programmed in the browser, you can head to https://makecode.microbit.org/ 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: https://makecode.microbit.org/reference/led/plot-... 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

Resources
← Previous Post Next Post →

4 thoughts on “Distance Sensing With A microbit And HC-SR04 Module”

  • Michael Grazebrook August 13, 2019 at 12:55 pm

    I'm trying to use this:
    https://www.kitronik.co.uk/blog/distance-sensing-microbit-hc-sr04-module/

    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 https://makecode.microbit.org/# 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)

    Reply
    • 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.

      Reply
  • 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

    Reply
    • 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.

      Reply
Leave a Reply