I can't illustrate the proper signals by using Arduino Uno and matlab via serial communication

3 views (last 30 days)
Hi everyone!!
I want to establish a communication between Arduino Uno and Matlab. I have a sinus generator connected at the Arduino's A1 analog pin. Arduino does the A/D conversion and then send the data to my PC via serial communication. However, this is the case:
The generator produces a sine wave which frequency is 20Hz and at the matlab I see a signal which frequency is 14Hz. In general, the signals which are produced from the generator and those which are displayed at Matlab are not the same. Another example: The generator generates a sinus signal which frequency is 10 Hz and Matlab shows a signal which frequency is 7Hz. I have been struggling to solve this issue for WEEKS but nothing yet.
This is the code which runs on Arduino Uno board:
void setup()
{ // initialize serial communication at 115200 bits per second:
Serial.begin(115200);
}
void loop()
{
float voltage = 0;
int sensorValue = 0;
sensorValue = analogRead(A1);
voltage = sensorValue * (.5 / 1023);
Serial.println(voltage,4);
delay(2);
}
This is the code which runs on matlab:
s = serial('COM12'); % assigns the object s to serial port
set(s, 'InputBufferSize', 512); % number of bytes in inout buffer % buffersize 256 or below is realtime with no delay
% the default buffer size is 512
set(s, 'FlowControl', 'software');
set(s, 'BaudRate', 115200);
set(s, 'Parity', 'none');
set(s, 'DataBits', 8);
set(s, 'StopBit', 1);
set(s, 'Timeout',5);
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 = 512;
index = 1:buf_len;
Raw_EEG = zeros(size(index));
EEGdata = zeros(size(index));
Fs = 250;
T = 1/Fs;
while 1
for i = 1:1:500
Raw_EEG(i) = fscanf(s,'%f');
end
subplot(2,1,1);
plot(Raw_EEG);
xlabel('Samples');
ylabel('Amplitude');
N = length(Raw_EEG);
f = (1:N)*Fs/N;
FFT = 2*abs(fft(Raw_EEG));
subplot(2,1,2);
plot(f, FFT(1:N));
xlabel('Frequency');
ylabel('Amplitude |Xf|');
axis normal;
grid;
drawnow;
end
The idea is to make A/D conversion at Arduino board and send 500 samples per second at the Matlab for processing. At matlab display the initial signal, make FFT, filter the data and illustrate them. However, as long as I can not illustrate the correct signals I can't move forward. Thus, the filtering is not done yet.
If someone needs more information or if I haven't mention something or anything else answer please. Do I miss something? Do I do something wrong? I don't how to solve this issue. Thank you in advance for reading this big post!!! I am looking forward for your help!
BR,

Answers (0)

Categories

Find more on MATLAB Support Package for Arduino Hardware in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!