无垠的广袤
华东师范大学-学术研究/教师
【萤火工场CEM5861G-M11】OLED 实时显示
【萤火工场CEM5861G-M11】OLED 实时显示
本文介绍了 萤火工场 CEM5861G-M11 24GHz 毫米波雷达模块实现检测距离的 OLED 实时显示的项目设计。
硬件连接
这里使用树莓派 Pico 作为主体开发板,同时驱动 CEM5861G-M11 雷达模块和 OLED 显示屏;
CEM5861G-M11 连接
CEM5816G-M11 | Raspberry Pi Pico | Note |
---|---|---|
RXD (R) | 0 (TXD) | Receive |
TXD (T) | 1 (RXD) | Transmit |
VCC (V) | 3.3V | Power |
GND (G) | GND | Ground |
OLED 连接
OLED | Raspberry Pi Pico | Note |
---|---|---|
SCL | 5 (SCL) | Serial Clock Line |
SDA | 4 (SDA) | Serial Data Line |
VCC | 3.3V | Power |
GND | GND | Ground |
流程图
代码
打开 Thonny IDE 软件,新建 *.py
文件,添加如下代码
from machine import Pin, I2C, UART
import utime
from ssd1306 import SSD1306_I2C
import framebuf
# 初始化OLED显示屏
sda = Pin(4)
scl = Pin(5)
i2c = I2C(0, sda=sda, scl=scl, freq=400000)
oled = SSD1306_I2C(128, 64, i2c)
# 初始化串口
uart = machine.UART(0, baudrate=115200, tx=machine.Pin(0), rx=machine.Pin(1))
# 距离数据存储
distance_history = []
max_history_length = 20
current_distance = 0.0
# 清空OLED屏幕
oled.fill(0)
oled.text("Distance Monitor", 0, 0, 1)
oled.text("Initializing...", 0, 16, 1)
oled.rotate(0)
oled.show()
utime.sleep(1)
def parse_distance_data(data):
"""
解析串口数据,提取距离信息
数据格式: 55 A5 00 0E 03 81 00 00 01 01 0D 00 00 00 00 00 CF 6A
第10-11位为距离值(大端格式)
"""
try:
# 将字节数据转换为十六进制字符串列表
hex_data = ['{:02X}'.format(byte) for byte in data]
# 查找帧头55 A5
for i in range(len(hex_data) - 1):
if hex_data[i] == '55' and hex_data[i+1] == 'A5' and (i + 11) < len(hex_data):
# 提取第10-11位字节(索引9和10)
byte10 = int(hex_data[i+9], 16)
byte11 = int(hex_data[i+10], 16)
# 合并两个字节得到距离值(大端格式):cite[3]
distance = (byte10 << 8) | byte11
return distance
return None
except Exception as e:
print("Error parsing data:", e)
return None
def update_display(distance):
"""更新OLED显示屏"""
global current_distance, distance_history
current_distance = distance
distance_history.append(distance)
# 保持历史数据长度
if len(distance_history) > max_history_length:
distance_history.pop(0)
# 清空屏幕
oled.fill(0)
# 显示当前距离值
oled.text("Distance:", 0, 0, 1)
oled.text("{:.2f} cm".format(distance), 0, 16, 1)
# 绘制距离变化曲线
if len(distance_history) > 1:
# 找出距离范围用于缩放
min_dist = min(distance_history)
max_dist = max(distance_history)
range_dist = max(max_dist - min_dist, 1) # 避免除以零
# 绘制坐标轴
oled.hline(0, 50, 128, 1) # X轴
oled.vline(0, 30, 20, 1) # Y轴
# 绘制数据点
for i in range(1, len(distance_history)):
x1 = int((i-1) * 128 / max_history_length)
y1 = int(50 - (distance_history[i-1] - min_dist) * 20 / range_dist)
x2 = int(i * 128 / max_history_length)
y2 = int(50 - (distance_history[i] - min_dist) * 20 / range_dist)
# 确保坐标在屏幕范围内
y1 = max(min(y1, 49), 30)
y2 = max(min(y2, 49), 30)
oled.line(x1, y1, x2, y2, 1)
oled.rotate(0)
oled.show()
def main():
"""主循环"""
print("Starting distance monitoring system...")
while True:
# 检查串口是否有数据
if uart.any():
# 读取串口数据:cite[7]
data = uart.read()
if data:
print("Received:", ''.join('{:02X} '.format(byte) for byte in data))
# 解析距离数据
distance = parse_distance_data(data)
if distance is not None:
print("Distance:", distance, "cm")
# 更新OLED显示
update_display(distance)
# 短暂延迟,避免CPU过载
utime.sleep_ms(100)
# 启动主程序
if __name__ == "__main__":
main()
保存代码。
效果演示
- 按照示意图完成 OLED、CEM5816G-M11模块、开发板等硬件连接;
- Thonny IDE 运行代码;
- OLED 屏幕显示实时距离信息和演化曲线;
同时 Shell 终端打印接收到的十六进制数据和转换得到的距离信息
总结
本文介绍了 萤火工场 CEM5861G-M11 24GHz 毫米波雷达模块实现检测距离的 OLED 实时显示的项目设计,为相关产品的开发设计和应用提供了参考。
版块:
萤火工场
昨天 15:28
全部评论