Make An Ultrasonic Distance Tester with Micro:bit
Today, we are going to make an ultrasonic distance tester with micro:bit and ultrasonic sensor module.
1 x BBC micro:bit 1 x Octopus:bit 1 x OLED module 1 x HC-SR04 ultrasonic module
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.
You can refer to the column below for the connection between ultrasonic module and octopus:bit
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.
Plug OLED module into IIC cpnnector on octopus:bit.
Once connected, you can see the following picture showed:
Click to open https://makecode.microbit.org/and enter the programming interface.
Search sonar in ADD Package, then add the ultrasonic library.
Search OLED in ADD Package, then add the library for OLED module.
Initialize OLED screen.
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.
When you finished your program, you can get the whole code from this link：https://makecode.microbit.org/_CtF2K5HTkarfOr 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.