This document introduces a portable distance meter project based on the Ai-Thinker Rd-03_V2 radar module, which receives and prints serial data, displays distance information on an OLED, and visualizes historical evolution curves.
Project Overview- Receive and print distance measurement data through UART.
- Drive an OLED display to show distance information.
- Display historical evolution curves on the OLED.
Connect the Rd-03_V2 radar module pins to the serial port pins of the target development board.
Read data transmitted from the Rd-03_V2 module via the serial port and print it in the terminal, including the process flow, code, and demonstration.
Code Examplefrom machine import UART, Pinimport time
uart = UART(0, baudrate=115200, tx=Pin(0), rx=Pin(1))print("Serial Monitor Started")
while True:
if uart.any():
data = uart.readline()
if data:
text = data.decode('utf-8')
print(text, end='')
time.sleep_ms(10)
Drive a 4-wire I²C OLED to display distance information, including initialization, data parsing, and dynamic updates.
Code Examplefrom machine import UART, I2C, Pinimport ssd1306import framebufimport time
uart = UART(0, baudrate=115200, tx=Pin(0), rx=Pin(1))
i2c = I2C(0, sda=Pin(4), scl=Pin(5))
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
# Display logo
buffer = bytearray(b"...") # logo data
fb = framebuf.FrameBuffer(buffer, 128, 45, framebuf.MONO_HLSB)
oled.fill(0)
oled.blit(fb, 0, 0)
oled.rotate(0)
oled.show()
time.sleep(1)
def update_display(distance):
"""Update OLED Display"""
oled.fill(0)
oled.text("Distance:", 0, 0)
oled.text(f"{distance} cm", 75, 0)
oled.show()
print("Radar System Started ...")
while True:
if uart.any():
data = uart.readline()
if data:
try:
text = data.decode('utf-8')
print(text, end='')
if text.startswith("distance:"):
distance = int(text.split(":")[1])
update_display(distance)
except Exception as e:
print(f"Error: {e}")
time.sleep_ms(10)
Effect
The logo display part requires converting an image file into a binary array. You can use Python to achieve this:
# image_converter.pyfrom io import BytesIOfrom PIL import Image
IMAGE_PATH = "logo_aithinker.bmp"
WIDTH = 128
HEIGHT = 45
im = Image.open(IMAGE_PATH).convert('1')
im_resize = im.resize((WIDTH, HEIGHT))
buf = BytesIO()
im_resize.save(buf, 'ppm')
byte_im = buf.getvalue()
header_len = len(str(WIDTH) + ' ' + str(HEIGHT)) + 4
image_data = byte_im[header_len:]
hex_escape = "".join([f"\\x{byte:02x}" for byte in image_data])
print("Copy the following code into your Pico program:")print(f'buffer = bytearray(b"{hex_escape}")')
Configure parameters such as input image path, name, and output size as needed, then run the script to generate a byte array ready for use.
For more details, see img2bytearray.
Evolution Curve DisplayHistorical values can be stored and visualized on an OLED as an evolution curve based on real-time radar distance data.
Code Examplefrom machine import UART, I2C, Pinimport ssd1306import framebufimport time
uart = UART(0, baudrate=115200, tx=Pin(0), rx=Pin(1))
i2c = I2C(0, sda=Pin(4), scl=Pin(5))
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
buffer = bytearray(b"...") # logo byte array
fb = framebuf.FrameBuffer(buffer, 128, 45, framebuf.MONO_HLSB)
distance_history = []
MAX_HISTORY = 20
graph_height = 35
graph_width = 128
graph_y = 45
oled.fill(0)
oled.blit(fb, 0, 0)
oled.text("Radar Module", 20, 47)
oled.text("Rd-03 V2", 40, 56)
oled.show()
time.sleep(1)
def update_display(distance):
"""Update OLED Display with Distance Curve"""
global distance_history
distance_history.append(distance)
if len(distance_history) > MAX_HISTORY:
distance_history.pop(0)
oled.fill(0)
oled.text(f"Distance: {distance:3d} cm", 0, 48)
draw_distance_curve()
oled.show()
def draw_distance_curve():
if len(distance_history) < 2:
return
min_dist = min(distance_history)
max_dist = max(distance_history)
dist_range = max_dist - min_dist if max_dist > min_dist else 1
oled.hline(0, graph_y, graph_width, 1)
oled.vline(0, graph_y - graph_height, graph_height, 1)
for i in range(1, len(distance_history)):
x1 = (i - 1) * (graph_width // MAX_HISTORY)
x2 = i * (graph_width // MAX_HISTORY)
y1 = graph_y - int((distance_history[i-1] - min_dist) * graph_height // dist_range)
y2 = graph_y - int((distance_history[i] - min_dist) * graph_height // dist_range)
y1 = max(graph_y - graph_height, min(graph_y, y1))
y2 = max(graph_y - graph_height, min(graph_y, y2))
oled.line(x1, y1, x2, y2, 1)
oled.pixel(x1, y1, 1)
oled.pixel(x2, y2, 1)
range_text = f"Range: {min_dist}-{max_dist}cm"
oled.text(range_text, graph_width - len(range_text)*8, graph_y - graph_height - 8)
print("Radar System with Distance Curve Started ...")
while True:
if uart.any():
data = uart.readline()
if data:
try:
text = data.decode('utf-8')
print(text)
if text.startswith("distance:"):
distance = int(text.split(":")[1])
update_display(distance)
except Exception as e:
print(f"Error: {e}")
time.sleep_ms(10)
This document demonstrates how to design a portable distance measurement system using the Ai-Thinker Rd-03_V2 radar module.
It includes serial data reception and display, OLED visualization, and dynamic evolution curve rendering — providing a solid reference for product development and rapid prototyping.
Comments