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???/strong>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 module | octopus:bit |
---|---|
VCC | VCC |
GND | GND |
TRIG | P14 |
ECHO | P15 |
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:
Programming
Step 1
Click to open https://makecode.microbit.org/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???a href=”https://makecode.microbit.org/_CtF2K5HTkarf”>https://makecode.microbit.org/_CtF2K5HTkarfOr you can download the code into micro:bit directly through the web page below.
Result
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.