All post of ElecFreaksMicro: bit

Make An Ultrasonic Distance Tester with Micro:bit

By February 1, 2018 No Comments

Today, we are going to make an ultrasonic distance tester with micro:bit and ultrasonic sensor module.


Materials Needed

1 x BBC micro:bit
1 x Octopus:bit
1 x OLED module
1 x HC-SR04 ultrasonic module


Background Knowledge

HC-SR04 Basic Principle

HC-SR04 is a kind of ultrasonic distance measuring modules. With this module, we can detect the space time between ultrasonic send and return, then convert it into distance. Here’s the basic principle:

  • Use the IO port TRIG to trigger distance measure with 10us high electric level at least.
  • Automatically send 8 40kHz square wave and check if a signal returns.
  • If a signal has returned, then output a high electric level through the IO port TRIG. The duration of high electric level is the time from ultrasonic send and return.

Distance=(high electric level time x sound space (340m/s))/2

Note: Find in the MakeCode for the already sealed ultrasonic library. You don’t have to write any complicated drive code but just simplly invoke the library.


Hardware Assembly

Step 1

You can refer to the column below for the connection between ultrasonic module and octopus:bit

ultrasonic moduleoctopus:bit


Step 2

Since the driving voltage of SR04 ultrasonic module is 5V, so we must slide the voltage switch on octopus:bit to the end of 5V.

Step 3

Plug OLED module into IIC cpnnector on octopus:bit.


Once connected, you can see the following picture showed:





Step 1

Click to open and enter the programming interface.


Step 2

Search sonar in ADD Package, then add the ultrasonic library.


Step 3

Search OLED in ADD Package, then add the library for OLED module.


Step 4

Initialize OLED screen.


Step 5

Set the pin trig to be P14 and the pin echo to be P15 with cm as unit. And display the data returned on the OLED screen.


Step 6

When you finished your program, you can get the whole code from this link:

Or you can download the code into micro:bit directly through the web page below.


Now you have already successfully created a set of ultrasonic measuring device. Point the ultrasonic head to the object you would like to test, then you will see the distance between on the OLED screen.


VN:F [1.9.13_1145]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.13_1145]
Rating: 0 (from 0 votes)

Author song

More posts by song

Leave a Reply