17浏览
查看: 17|回复: 0

[项目] 【Beetle 树莓派 RP2350】热成像仪

[复制链接]
本帖最后由 无垠的广袤 于 2026-4-2 21:34 编辑

【Beetle 树莓派 RP2350】热成像仪
本文介绍了 DFRobot Beetle RP2350 开发板结合 MLX90640 模块和串口通信,实现网页热成像仪的项目设计。

项目介绍
  • 准备工作:MLX90640 驱动程序、功能测试等;
  • 硬件连接:使用杜邦线连接传感器模块、USB转TTL工具、RP2350开发板;
  • 工程代码:包括板端执行代码、网页设计代码;
  • 效果演示:运行程序和网页,实现热成像画面帧的实时显示。


MLX90640

MLX90640 是一款高性能的 32x24 像素红外热成像传感器,支持非接触式温度测量,广泛应用于工业监控、智能家居和科研实验等领域。

【Beetle 树莓派 RP2350】热成像仪图8


模块设计
为了便于设备连接,设计 PCB 模块。

【Beetle 树莓派 RP2350】热成像仪图7

原理图
【Beetle 树莓派 RP2350】热成像仪图6

实物图
【Beetle 树莓派 RP2350】热成像仪图5

这里使用 MLX90640 模块,详见:MLX90640热成像传感器 - 立创开源硬件平台 .  

准备工作
包括驱动代码和功能测试。

驱动代码
  • 下载 并上传 mlx90640 驱动代码文件至开发板根目录;

【Beetle 树莓派 RP2350】热成像仪图4

功能测试
运行 Thonny IDE 新建文件,添加如下代码
  1. from machine import I2C
  2. import time
  3. from mlx90640 import MLX90640, RefreshRate
  4. # 温度数据缓冲区
  5. temperature_frame = [0.0] * 768
  6. time.sleep(3)
  7. print("FreakStudio:Testing the MLX90640 fractional infrared temperature sensor")
  8. # 初始化 I2C
  9. i2c = I2C(0, scl=5, sda=4, freq=100000)
  10. # 扫描 I2C 设备
  11. devices_list = i2c.scan()
  12. print('START I2C SCANNER')
  13. if len(devices_list) == 0:
  14.      print("No i2c device !")
  15. else:
  16.      print('i2c devices found:', len(devices_list))
  17. mlxaddr = next((d for d in devices_list if 0x31 <= d <= 0x35), None)
  18. # 初始化传感器
  19. try:
  20.      thermal_camera = MLX90640(i2c, mlxaddr)
  21.      print("MLX90640 sensor initialized successfully")
  22. except ValueError as init_error:
  23.      print(f"Sensor initialization failed: {init_error}")
  24.      raise SystemExit(1)
  25. print(f"Device serial number: {thermal_camera.serial_number}")
  26. # 设置刷新率
  27. try:
  28.      thermal_camera.refresh_rate = RefreshRate.REFRESH_2_HZ
  29.      print(f"Refresh rate set to {thermal_camera.refresh_rate} Hz")
  30. except ValueError as rate_error:
  31.      print(f"Failed to set refresh rate: {rate_error}")
  32.      raise SystemExit(1)
  33. thermal_camera.emissivity = 0.92
  34. # 主循环
  35. try:
  36.      while True:
  37.          try:
  38.              thermal_camera.get_frame(temperature_frame)
  39.          except RuntimeError as read_error:
  40.              print(f"Frame acquisition failed: {read_error}")
  41.              time.sleep(0.5)
  42.              continue
  43.          # 统计温度
  44.          min_temp = min(temperature_frame)
  45.          max_temp = max(temperature_frame)
  46.          avg_temp = sum(temperature_frame) / len(temperature_frame)
  47.          print("\n--- Temperature Statistics ---")
  48.          print(f"Min: {min_temp:.2f} °C | Max: {max_temp:.2f} °C | Avg: {avg_temp:.2f} °C")
  49.          # 打印左上角 4×4 样本像素
  50.          print("--- Sample Pixels (Top-Left 4x4) ---")
  51.          for row in range(4):
  52.              row_data = [f"{temperature_frame[row*32 + col]:5.1f}" for col in range(4)]
  53.              print(" | ".join(row_data))
  54.          # 等待下一次测量
  55.          time.sleep(1.0 / (thermal_camera.refresh_rate + 1))
  56. except KeyboardInterrupt:
  57.      print("\nProgram terminated by user")
  58. finally:
  59.      print("Testing process completed")
复制代码

保存代码。

运行测试代码,终端输出初始化传感器信息、温度统计信息等;

【Beetle 树莓派 RP2350】热成像仪图3


硬件连接
包括 MLX90640 模块和 USB 转 TTL 工具与 RP2350 开发板的连接。

MLX90640 模块

[td]
RP2350
MLX90640 module
Note
SCL (Pin 8)SCLSerial Clock
SDA (Pin 7)SDASerial Data
GNDGNDGround
3.3VVCCPower


USB 转 TTL 工具

[td]
RP2350
USB to TTL
Note
RX (Pin 1)TXDReceive
TX (Pin 0)RXDTransmite
GNDGNDGround


实物图
【Beetle 树莓派 RP2350】热成像仪图2



流程图

【Beetle 树莓派 RP2350】热成像仪图1


工程代码
运行 Thonny IDE 新建文件,添加如下代码

  1. from machine import I2C, UART, Pin
  2. import time
  3. from mlx90640 import MLX90640, RefreshRate
  4. import json
  5. # 温度数据缓冲区 (32x24 = 768像素)
  6. temperature_frame = [0.0] * 768
  7. print("MLX90640 Thermal Camera - JSON Mode")
  8. print("Baudrate: 115200")
  9. # 初始化 I2C
  10. i2c = I2C(0, scl=5, sda=4, freq=800000)
  11. # 扫描 I2C 设备
  12. devices_list = i2c.scan()
  13. print('I2C Scanner...')
  14. if len(devices_list) == 0:
  15.      print("No I2C device found!")
  16.      raise SystemExit(1)
  17. mlxaddr = next((d for d in devices_list if 0x31 <= d <= 0x35), None)
  18. if mlxaddr is None:
  19.      print("MLX90640 not found!")
  20.      raise SystemExit(1)
  21. print(f"MLX90640 found at 0x{mlxaddr:02X}")
  22. # 初始化传感器
  23. try:
  24.      thermal_camera = MLX90640(i2c, mlxaddr)
  25.      print("Sensor initialized")
  26. except Exception as e:
  27.      print(f"Init failed: {e}")
  28.      raise SystemExit(1)
  29. print(f"Serial: {thermal_camera.serial_number}")
  30. # 设置刷新率
  31. try:
  32.      thermal_camera.refresh_rate = RefreshRate.REFRESH_4_HZ
  33.      print("Refresh rate: 4Hz")
  34. except:
  35.      thermal_camera.refresh_rate = RefreshRate.REFRESH_2_HZ
  36.      print("Refresh rate: 2Hz")
  37. thermal_camera.emissivity = 0.95
  38. # 初始化UART
  39. try:
  40.      uart = UART(0, baudrate=115200, tx=Pin(0), rx=Pin(1), timeout=100)
  41.      # 清空缓冲区
  42.      while uart.any():
  43.          uart.read()
  44.      print("UART initialized at 115200")
  45. except Exception as e:
  46.      print(f"UART init failed: {e}")
  47.      raise SystemExit(1)
  48. # 等待系统稳定
  49. time.sleep(0.5)
  50. frame_count = 0
  51. last_print = time.ticks_ms()
  52. def send_json_frame():
  53.      """发送JSON格式的温度帧"""
  54.      global frame_count, last_print
  55.      
  56.      # 获取温度数据
  57.      try:
  58.          thermal_camera.get_frame(temperature_frame)
  59.      except Exception as e:
  60.          print(f"Frame error: {e}")
  61.          return False
  62.      
  63.      frame_count += 1
  64.      
  65.      # 计算统计值
  66.      min_temp = min(temperature_frame)
  67.      max_temp = max(temperature_frame)
  68.      avg_temp = sum(temperature_frame) / len(temperature_frame)
  69.      
  70.      # 降采样到 16x12
  71.      downsampled = []
  72.      for y in range(0, 24, 2):
  73.          row = []
  74.          for x in range(0, 32, 2):
  75.              idx = y * 32 + x
  76.              # 取2x2区域的平均值
  77.              val = (temperature_frame[idx] +
  78.                     temperature_frame[idx+1] +
  79.                     temperature_frame[idx+32] +
  80.                     temperature_frame[idx+33]) / 4
  81.              row.append(round(val, 1))
  82.          downsampled.append(row)
  83.      
  84.      # 构建JSON
  85.      data = {
  86.          "f": frame_count,
  87.          "t": time.ticks_ms(),
  88.          "min": round(min_temp, 2),
  89.          "max": round(max_temp, 2),
  90.          "avg": round(avg_temp, 2),
  91.          "data": downsampled
  92.      }
  93.      
  94.      # 确保完整发送
  95.      json_str = json.dumps(data) + '\n'
  96.      
  97.      # 发送前再次清空接收缓冲区(防止回显干扰)
  98.      while uart.any():
  99.          uart.read()
  100.      
  101.      # 发送数据
  102.      bytes_written = uart.write(json_str.encode('utf-8'))
  103.      
  104.      # 每秒打印一次状态
  105.      now = time.ticks_ms()
  106.      if time.ticks_diff(now, last_print) > 1000:
  107.          print(f"Frame {frame_count}: {min_temp:.1f}~{max_temp:.1f}°C, sent {bytes_written} bytes")
  108.          last_print = now
  109.      
  110.      return True
  111. # 主循环
  112. print("Starting main loop...")
  113. time.sleep(0.5)
  114. while True:
  115.      try:
  116.          send_json_frame()
  117.      except Exception as e:
  118.          print(f"Loop error: {e}")
  119.      
  120.      # 帧率控制 (4Hz)
  121.      time.sleep_ms(250)
复制代码

保存代码。


效果演示
  • 使用串口调试助手连接 USB 转 TTL 工具对应端口,配置波特率等参数;
  • 打开串口,接收 JSON 数据帧;

【Beetle 树莓派 RP2350】热成像仪图9

  • 打开 mlx90640_web.html 网页文件,连接目标串口,显示实时热成像帧画面;

【Beetle 树莓派 RP2350】热成像仪图10

  • 可改变 colorbar 呈现不同的颜色梯度;

【Beetle 树莓派 RP2350】热成像仪图11


【Beetle 树莓派 RP2350】热成像仪图12


【Beetle 树莓派 RP2350】热成像仪图13


总结

本文介绍了 DFRobot Beetle RP2350 开发板结合 MLX90640 模块和串口通信,实现网页热成像仪的项目设计,为相关产品的快速开发和应用设计提供了参考。



您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4 备案 沪公网安备31011502402448

© 2013-2026 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail