MATLAB Answers

data acquisition from arduino to matlab

185 views (last 30 days)
fremond khoo
fremond khoo on 22 Mar 2011
Answered: kalpana on 19 Feb 2015
hello and good day to all the experts and visitors of matlab,
i am a mechanical student and now im facing problem trying to interface matlab and arduino..
my project is to connect and run a two-link robot attaching a potentiometer at each of the motor shaft, i am required to obtain the data from the potentiometer to know the rotation of my motor shaft..but i need to use arduino to get the data then transfer the data to matlab and plot the graph..
now i have successfully acquire the data from arduino to matlab by manually rotating the potentiometer myself and not by the motor.. my stepper motors operate by using MATLAB GUI so since i have to operate my motor to move the potentiometer i cant open another file at the same time to acquire the data from potentiometer.. the problem is how do i acquire the data from arduino to matlab by operating my stepper motor at the same time?..i wish to acquire the real result of my potentiometer while attaching to my moving stepper motor do i do that? i need to combine both the files together?? thanks again for your information^^
the code i put in my matlab is:
% --- Executes on button press in data_pushbutton1.
function data_pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to data_pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Try-catch is to prevent Matlab from crashing when the program is finished
% Initialize serial port
s = serial('COM7');
%set(s, ‘ Terminator’, ‘LF’); % Default terminator is \n
set(s,'BaudRate', 9600);
set(s,'DataBits', 8);
set(s,'StopBits', 1);
s.ReadAsyncMode = 'continuous';
% Various variables
numberOfDatas = 200;
data = zeros(1, numberOfDatas);
i = 1;
% Main graph figure
hold on;
title('Incomming Data from External Device');
xlabel('Data Number');
ylabel('Analog Voltage (0-1023)');
% Start asynchronous reading
% Get the data from the serial object
data(i) = fscanf(s, '%d');
% Plot the data
plot(i, data(i), 'm*');
% Ensure there are always 10 tick marks on the graph
xlim([i-10 i]);
set(gca,'xtick',[i-10 i-9 i-8 i-7 i-6 i-5 i-4 i-3 i-2 i-1 i])
% Draw and flush
%Increment the counter
% Give the external device some time…
% Some of these crash the program – it depends. The serial port is left
% open, which is not good.
fclose(s); % bad
%clear s;
fprintf(1, 'Sorry, you"re going to have to close out of Matlab to close the serial port\n');

Answers (2)

Rolfe Dlugy-Hegwer
Rolfe Dlugy-Hegwer on 14 Jun 2012
If you are running the R2012a version of MATLAB/Simulink, you can install support for Arduino Mega 2560, and then use External mode to send data back to MATLAB/Simulink. External mode is not available for Arduino Uno. To install support for Arduino Mega 2560, enter targetinstaller in the MATLAB Command Window. This action opens Target Installer.In Target Installer, follow the instructions for installing support for Arduino. After you have completed this process, attach scopes to the Analog Input or Digital input block outputs. When you run your model on Arduino, External mode sends data to the scopes in the Simulink model on your host computer.

kalpana on 19 Feb 2015
I want to plot the real time data in MATLAB window of any sensor connected to the Arduino board, Please help to make connection between Arduino and MATLAB.

Community Treasure Hunt

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

Start Hunting!