0
votes

I am facing a problem between Arduino Uno and 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). The frequencies will vary between 10 and 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.

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. However, when the frequency is stable at 20 Hz or 10 Hz the output is stable.

This is the code that runs at 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:

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). I guess I was supposed to see one signal at 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. 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.

1

1 Answers

1
votes

your program have many mistakes.

  1. you have set different boudrate for arduino and 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=f/.05