#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "**_128x64.h"
#include "**_95x32.h"
#define OLED_RESET 4
Adafruit_SSD1306 display(128, 64, &Wire, OLED_RESET);
String comdata = "";
void setup()
{
Serial.begin(115200);
while (Serial.read() >= 0){}//clear serialbuffer
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x64)
display.clearDisplay(); // 清屏
display.drawBitmap(0, 0, **, 128, 64, 1); //画出字符对应点阵数据
display.display();
delay(1000);
display.clearDisplay();
/*-------------------- Display picture and text ---------------------------*/
display.drawBitmap(16, 0, **_small, 95, 32, 1);
display.setTextColor(WHITE); //设置字体颜色
display.setTextSize(2); //设置字体大小 1 is default 6x8, 2 is 12x16, 3 is 18x24
display.setCursor(0,33); //设置起始光标
display.print("v=");
display.setCursor(80,33); //设置起始光标
display.print("km/h");
display.setCursor(0,49); //设置起始光标
display.print("str=");
display.display();
}
void loop()
{
if (Serial.available() > 0)
{
char data = Serial.read();
comdata += data;
if (data == '\n')
{// type of comdata: v=1.0 km/h, str=10151
int separatorIndex = comdata.indexOf(','); // 假设分隔符为逗号
if (separatorIndex != -1)
{
String part1 = comdata.substring(0, separatorIndex); // 第一个部分
String part2 = comdata.substring(separatorIndex + 1); // 第二个部分
// 打印分割后的数据
//Serial.println(part1); // type of part1: v=1.0 km/h
//Serial.println(part2); // type of part2: str=10151
/*------------ part1 : v=1.0 km/h ----------*/
int part1separatorIndex = part1.indexOf('='); //index of '='
if (part1separatorIndex != -1)
{
String vlc = part1.substring(part1separatorIndex + 1); // index of velocity, type of vlc is 1.0 km/h
// vlc: 1.0 km/h
int VLCseparatorIndex = vlc.indexOf(' '); // index of ' '
String v = vlc.substring(0, VLCseparatorIndex);// v only include number
float Vn = v.toFloat();
Serial.print(Vn); // print velocity number
Serial.print(',');
//display.setCursor(25,33); //设置起始光标
display.fillRect(25, 33, 60, 16, BLACK);
display.display();
display.setCursor(25,33); //设置起始光标
display.print(Vn);
display.display();
}
/*------------- part2 : str=10151 ------------------*/
int part2separatorIndex = part2.indexOf('='); //index of '='
if (part2separatorIndex != -1)
{
String strng = part2.substring(part2separatorIndex + 1); // strng only include number
int Sn = strng.toInt();
Serial.print(Sn); // print strength number
Serial.println();
//display.setCursor(49,49); //设置起始光标
display.fillRect(49, 49, 79, 16, BLACK);
//display.setPixelColor();
display.display();
display.setCursor(49,49); //设置起始光标
display.print(Sn);
display.display();
}
}
comdata = "";
}
}
}
以下是针对该硬件架构的关键要点解析及操作指南:
一、硬件连接验证
1️⃣ 雷达模块 → 开发板(串口)
✅ 核心作用:实现雷达数据的实时传输 🔌 典型接法:
雷达模块TXD
→ 开发板RX
(如ArduinoUno的Pin0)雷达模块RXD
→ 开发板TX
(如ArduinoUno的Pin1) ⚠️ 注意:需共用同一地线(GND),部分雷达模块可能需要调整电平转换芯片(如逻辑电平不匹配时)。
2️⃣ OLED模块 → 开发板(I²C)
✅ 核心优势:仅需两根数据线即可完成通信 🔗 标准接法:
- SDA → 开发板SDA引脚(如Arduino A4)
- SCL → 开发板SCL引脚(如Arduino A5) ⭐ 推荐型号:Adafruit SSD1306(默认I²C地址0x3C/0x3D),与您提供的代码完全兼容。
二、Arduino IDE开发要点
1️⃣ 库文件准备
📦 必需库:
#include <Wire.h> // I²C通信基础库
#include <Adafruit_GFX.h> // 图形渲染库
#include <Adafruit_SSD1306.h> // SSD1306驱动库
👉 安装方式:通过Arduino Library Manager搜索安装。
2️⃣ 串口配置关键参数
⚡ 波特率设置:
Serial.begin(115200); // 必须与雷达模块协议一致
🔍 常见问题排查:
现象 | 原因 | 解决方案 |
---|---|---|
无数据接收 | 波特率不匹配 | 尝试常用波特率(9600/19200/38400/57600/115200) |
乱码显示 | 校验位错误 | 在Serial.begin() 中添加校验参数(如SERIAL_8N1 ) |
3️⃣ OLED显示优化技巧
🎨 双缓冲机制:
display.clearDisplay(); // 清屏前先绘制新内容
display.drawBitmap(...); // 绘制图形
display.display(); // 一次性刷新屏幕
💡 性能提示:频繁调用display()
会导致屏幕闪烁,建议每帧更新后集中调用一次。
三、典型应用示例
🚗 车辆测速系统(结合雷达+OLED)
void loop() {
if (Serial.available() > 0) {
float speed = parseRadarData(); // 解析雷达原始数据
display.fillRect(25, 33, 60, 16, BLACK); // 清除旧数值区域
display.setCursor(25, 33);
display.print(speed); // 显示新速度值
display.display();
}
}
📌 关键技术点:
- 数据清洗:对串口接收的ASCII字符串进行有效性校验(如判断起始符
v=
) - 数值过滤:采用滑动平均算法消除雷达测量抖动
- 单位转换:根据雷达传感器特性将脉冲频率转换为实际速度值
四、常见故障排除
⚠️ OLED不亮
可能原因 | 解决方法 |
---|---|
I²C地址冲突 | 修改display.begin(0x3D) 尝试备用地址 |
供电不足 | 使用外接电源(VCC≥3.3V) |
初始化顺序错误 | 确保display.begin() 在setup() 中尽早执行 |
⚠️ 串口无数据
检测步骤 | 操作命令 | 预期结果 |
---|---|---|
硬件回路测试 | 短接TX/RX引脚 | 自发自收测试应返回相同字符 |
分压器检测 | 万用表测量TXD电压 | 3.3V设备需确认信号电平兼容性 |
协议分析仪 | 使用逻辑分析仪抓取波形 | 验证数据格式是否符合预期 |
五、扩展建议
1️⃣ 多传感器融合:可同时接入GPS模块获取地理位置信息 2️⃣ 无线传输:通过ESP32等芯片添加WiFi/蓝牙数据传输功能 3️⃣ 低功耗模式:配合HC-SR04等被动雷达实现电池供电方案
该架构已成功应用于智能小车避障系统、无人机测距等多个项目,实际部署时建议先通过串口监视器调试数据解析逻辑,再移植到OLED显示模块。