HOW to Make An Ultrasonic Radar with ESP32
Description

We know that bats use echoes to determine the direction and the location of their preys. In real life, sonar is used to detect sounds in the water. Since the attenuation rate of electromagnetic waves in water is very high, it cannot be used to detect signals, however, the attenuation rate of sound waves in the water is much smaller, so sound waves are most commonly used underwater for observation and measurement.
In this experiment, we will use a speaker module, an RGB module and a 4-digit tube display to make a device for detection through ultrasonic.
Required Components
|
|
|
|
|
|
|---|---|---|---|---|
|
ESP32 Board*1 |
ESP32 Expansion Board*1 |
Keyestudio HC-SR04 Ultrasonic Sensor*1 |
Keyestudio 8002b Power Amplifier*1 |
Keyestudio DIY Common Cathode RGB Module *1 |
|
|
|
|
|
|
|
Keyestudio DIY TM1650 4-Digit Display*1 |
4P Dupont Wire*3 |
3P Dupont Wire*1 |
Micro USB Cable*1 |
|
Connection Diagram

Test Code
from machine import Pin, PWM
import utime
# definitions for TM1650
ADDR_DIS = 0x48 #mode command
ADDR_KEY = 0x49 #read key value command
# definitions for brightness
BRIGHT_DARKEST = 0
BRIGHT_TYPICAL = 2
BRIGHTEST = 7
on = 1
off = 0
# number:0~9
NUM = [0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f]
# DIG = [0x68,0x6a,0x6c,0x6e]
DIG = [0x6e,0x6c,0x6a,0x68]
DOT = [0,0,0,0]
clkPin = 22
dioPin = 21
clk = Pin(clkPin, Pin.OUT)
dio = Pin(dioPin, Pin.OUT)
DisplayCommand = 0
def writeByte(wr_data):
global clk,dio
for i in range(8):
if(wr_data & 0x80 == 0x80):
dio.value(1)
else:
dio.value(0)
clk.value(0)
utime.sleep(0.0001)
clk.value(1)
utime.sleep(0.0001)
clk.value(0)
wr_data <<= 1
return
def start():
global clk,dio
dio.value(1)
clk.value(1)
utime.sleep(0.0001)
dio.value(0)
return
def ack():
global clk,dio
dy = 0
clk.value(0)
utime.sleep(0.0001)
dio = Pin(dioPin, Pin.IN)
while(dio.value() == 1):
utime.sleep(0.0001)
dy += 1
if(dy>5000):
break
clk.value(1)
utime.sleep(0.0001)
clk.value(0)
dio = Pin(dioPin, Pin.OUT)
return
def stop():
global clk,dio
dio.value(0)
clk.value(1)
utime.sleep(0.0001)
dio.value(1)
return
def displayBit(bit, num):
global ADDR_DIS
if(num > 9 and bit > 4):
return
start()
writeByte(ADDR_DIS)
ack()
writeByte(DisplayCommand)
ack()
stop()
start()
writeByte(DIG[bit-1])
ack()
if(DOT[bit-1] == 1):
writeByte(NUM[num] | 0x80)
else:
writeByte(NUM[num])
ack()
stop()
return
def clearBit(bit):
if(bit > 4):
return
start()
writeByte(ADDR_DIS)
ack()
writeByte(DisplayCommand)
ack()
stop()
start()
writeByte(DIG[bit-1])
ack()
writeByte(0x00)
ack()
stop()
return
def setBrightness(b = BRIGHT_TYPICAL):
global DisplayCommand,brightness
DisplayCommand = (DisplayCommand & 0x0f)+(b<<4)
return
def setMode(segment = 0):
global DisplayCommand
DisplayCommand = (DisplayCommand & 0xf7)+(segment<<3)
return
def displayOnOFF(OnOff = 1):
global DisplayCommand
DisplayCommand = (DisplayCommand & 0xfe)+OnOff
return
def displayDot(bit, OnOff):
if(bit > 4):
return
if(OnOff == 1):
DOT[bit-1] = 1;
else:
DOT[bit-1] = 0;
return
def InitDigitalTube():
setBrightness(2)
setMode(0)
displayOnOFF(1)
for _ in range(4):
clearBit(_)
return
def ShowNum(num): #0~9999
displayBit(1,







