简体   繁体   English

Arduino Uno和Matlab串行通信

[英]Arduino Uno and Matlab serial communication

I am facing a problem between Arduino Uno and Matlab. 我在Arduino Uno和Matlab之间遇到问题。 The idea is to connect a sinusoid generator at the Arduino's analog pin, do A/D conversion and send the results at Matlab for further processing (filtering and FFT). 这个想法是在Arduino的模拟引脚上连接一个正弦波发生器,进行A / D转换,并将结果发送到Matlab进行进一步处理(滤波和FFT)。 The frequencies will vary between 10 and 20 Hz. 频率将在10到20 Hz之间变化。

The first strange thing is that the values at Arduino's serial terminal can be seen only at 19200 baud rate, despite the fact that in the following code the baud rate is defined 9600. When I tried to change the baud rate at the terminal (back to 9600), I see only junk values. 第一件奇怪的是,尽管在以下代码中将波特率定义为9600,但Arduino串行终端的值只能以19200的波特率查看。当我尝试更改终端的波特率时(返回至9600),我只能看到垃圾值。

The second strange thing is that when I change the frequency between the interval of 10 and 20 Hz, the serial prints values which don't look like a sinus signal. 第二个奇怪的事情是,当我在10到20 Hz的间隔之间更改频率时,串行打印出看起来不像正弦信号的值。 However, when the frequency is stable at 20 Hz or 10 Hz the output is stable. 但是,当频率稳定在20 Hz或10 Hz时,输出是稳定的。

This is the code that runs at Arduino: 这是在Arduino上运行的代码:

int values;
float voltage;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

}

void loop() {
    values = analogRead(1); 
    float voltage = values * (1.0/1023);
    Serial.println(voltage, 3);
    delay(200);
  }     

This the code which runs at Matlab: 这是在Matlab上运行的代码:

clc;
clear all;
close all;

s = serial('COM12');
set(s, 'InputBufferSize', 1024);                                   
set(s, 'FlowControl', 'none');
set(s, 'BaudRate', 19200);
set(s, 'Parity', 'none');
set(s, 'DataBits', 8);
set(s, 'StopBit', 1);
set(s, 'Timeout',4);

disp(get(s,'Name'));
prop(1)=(get(s,'BaudRate'));
prop(2)=(get(s,'DataBits')); 
prop(3)=(get(s, 'StopBit'));
prop(4)=(get(s, 'InputBufferSize'));

disp(['Port Setup Done!!',num2str(prop)]);
fopen(s); %opens the serial port

disp('Running');
buf_len = 1024;
index = 1:buf_len;
Raw = zeros(size(index));
Data = zeros(size(index));
Fs = 200;
T = 1/Fs;

while 1

    Raw = fscanf(s,'%f');
    disp(num2str(Raw));

    Data = [Data(2:end),Raw];
    subplot(2,1,1);
    plot(Data);
    xlabel('Number of Samples');
    ylabel('Amplitude');
    axis normal;
    drawnow;

    N = length(Data);
    f = [0:N/2]*Fs/N;
    FFT = 2*abs(fft(Data))/N;
    subplot(2,1,2);
    plot(f, FFT(1:N/2+1));
    xlabel('Frequency');
    ylabel('Amplitude |Xf|');
    axis normal;
    drawnow;

The final strange thing is that despite the fact that the generator produces 20 Hz sinus signal, the FFT graph illustrates a signal at different frequency (8 and 18 Hz). 最后一个奇怪的事情是,尽管发生器产生了20 Hz的正弦信号,但FFT图显示了一个不同频率(8和18 Hz)的信号。 I guess I was supposed to see one signal at 20 Hz. 我想我应该看到一个20 Hz的信号。 The generators output is verified by using an oscilloscope. 通过使用示波器验证发电机的输出。

I would like someone to help me clarify this problem. 我希望有人可以帮助我澄清这个问题。 I am very confused, I have searched many links on the web for WEEKS but nothing yet. 我很困惑,我已经在网上搜索了许多链接以查找WEEKS,但还没有任何内容。 Forgive me for this big post. 原谅我这么大的帖子。 I tried to give as much information as possible. 我试图提供尽可能多的信息。 However, if someone wants to know something more or if I have not mentioned anything feel free to ask. 但是,如果有人想了解更多或我没有提到任何事情,请随时提出。

your program have many mistakes. 您的程序有很多错误。

  1. you have set different boudrate for arduino and PC. 您为arduino和PC设置了不同的波特率。
  2. are u getting correct waveform? 您是否获得正确的波形? u have not mention it. 你没有提到它。
  3. send the hardware circuit also. 也发送硬件电路。
  4. u can try this f = [0:N/2]*500/N; 你可以试试这个f = [0:N / 2] * 500 / N; f=f/.05 F = F / 0.05

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM