Seven lines of code to implement an ultrasonic rangefinder (Oled screen display)
[Copy link]
1. Introduction
The library function of shineblink core development board (Core for short) supports US-015 ultrasonic ranging sensor, so you only need to call two APIs to realize the ultrasonic ranging function.
PS: Core can realize Wifi/Ble/NB/Lora/ThreadMesh/RFID/Eth/Usb/RS485/RS232 communication, as well as more than 30 sensors/more than 10 hardware peripherals/more than 10 MCU built-in functions with only five or six lines of code, and these functions can run simultaneously in up to 5 random combinations.
2. Materials
-
A shineblink core development board
-
Ultrasonic ranging sensor module one
-
0.96 inch oled display
-
One TYPE00-0000003F circuit board. Please download the schematic diagram and PCB source file of the circuit board from the following network disk address (AD project):
https://yunpan.360.cn/surl_yP9fgP7LVGa
3. Complete code
LIB_0_96_OledConfig("IIC1")
LIB_US015Config()
--Start the big cycle
while(GC(1) == true)
do
--Delay 200 milliseconds
LIB_DelayMs(200)
flag, distance = LIB_US015GetDistance()
if flag == 1 then
--Show measured distance
LIB_0_96_OledPuts("2","1",string.format("distance:%04dmm", distance))
end
end
4. Production process & result display
The development method of Core is very simple and elegant. Use Notepad (or any other editor) to open the main.lua file on the TF card of the Core development board, and then copy the above code into the main.lua file. Since Core has a built-in Lua compiler, you don't need to do any processing, and Core can start working according to the above code.
The following figure shows the effect of the code running after assembly and welding are completed:
V. Conclusion
The above code updates the output value of the ultrasonic ranging sensor every 0.2 seconds. For more technical information about Core, you can visit shineblink.com.
|