PM2.5 测量与 MATLAB 实时分析

用 GP2Y1014 传感器和 STM32 测量 PM2.5,并通过 UART 在 MATLAB 中实时绘图(附电路和代码)。

使用的器件和软件:

#GP2Y10141325 #stm32f103ze #matlab #150欧电阻 #220uF电容 #keil uVision5

源代码

MATLAB 代码:

clear;
clc;
clf;
delete(instrfindall);  %delete instrfind (required)
ob=serial('COM5');%set up the UART interface
set(ob,'BaudRate',115200);
set(ob,'BytesAvailableFcnCount',1);
set(ob,'BytesAvailableFcnMode','byte');
set(ob,'TimeOut',0.1);%set the timeout
fopen(ob);%open COM5
pm=fread(ob);%read the data
PM0=hex2dec(pm);
t=0:18;
PM=zeros(1,19);
while(1) 
  pm=fread(ob);
  PM0=hex2dec(pm);
  size(PM0)
  if (length(PM0)==69||length(PM0)==66||length(PM0)==72||length(PM0)==57||length(PM0)==63)
    for i=1:19
      PM(i)=PM0(3*i-2)*100+PM0(3*i-1)*10+PM0(3*i);
    end
  else
    for i=1:19
      PM(i)=PM0(2*i-1)*10+PM0(2*i);
    end
  end
  plot(t,PM);
  axis([0 18 0 696]);
  ylabel('PM2.5(g/cm^{3})');
  pause(0.1);%required
end
MATLAB 中的 PM2.5 实时曲线
PM2.5 测量装置的照片
正在测量 PM2.5