Arduino to MATLAB

Hi,

I want to pass some accelerometer and gyro signals through an Arduino NG into MATLAB for analysis. Does anyone have an API for this? MATLAB does not currently support Arduino.

Thanks, any thoughts on this would be helpful.

Jamie

Is this something you could do by loading a properly-formatted text file into MATLAB? I'm speaking with utter ignorance of what MATLAB allows you to do... I mean, I know it has something to do with math, but other than that... EEK! :)

Anyway, perhaps you could Serial.print() properly formatted text, capture that text with a serial terminal program, save to a file, then load that into MATLAB?

You should just be able to get MATLAB to open the serial port and listen to incoming data. We did this using other serial sensors back in the Uni days, but I don't have a working version of MATLAB anymore to test it with Arduino.

Hi, last year I had to search the web quite a lot on this subject. I needed to analyze an analog signal coming from a couple of hall effect sensors. I used an arduino for the A/D convertion of both sensors. As I don´t have Matlab's "serial communication" toolbox (don't know If it's called like that, but I guess it is..) I decided to try with scilab (an open source Matlab-like application). There is a scilab function here that may help you:

http://www.gds-sw.de/goodies.htm

it's called serial.dll, just select your COM port, bit rate, byte length, parity, etc, and you will be able to reciebe the data. scilab uses almost the same commands as Matlab. I know it's not exactly wat you wanted, but give it a try if you have no other options

P.S: http://www.scilab.org/

good luck

I was wondering if any progress was made on this issue. I was hoping to send some ADC values to MATLAB.

same here, I'd be very interested in this!

Regards,

Till

MATLAB has a serial library, unless you're using a mac. Info in the MATLAB docs http://www.mathworks.com/access/helpdesk/help/techdoc/index.html?/access/helpdesk/help/techdoc/matlab_external/f105659.html. If like me you're using a mac, you're pretty stuffed, as 2007R1 and 2008R1 broke the 3rd party mac serial library. Hope this helps.

thank you very much! I'll see what I can do with this when I have time. Matlab is one of the reasons I am running bootcamp on my mac...

regards,

Till

I had the same problem and managed to solve it using the following generic Matlab Code to read from the Serial Port:

%Basic Comm
s = serial('COM5');
set(s,'BaudRate',9600);
set(s,'DataBits',8);
set(s,'StopBits',1);
fopen(s);
fprintf(s,'*IDN?')
out = fscanf(s);
fclose(s)
delete(s)
clear s

On the other hand I did some code to read signals from the ADXL30 with the arduino.
I programmed the arduino so that it would send a serial stream as the following
X [space] Y [space] Z [accZ] [CR]
where is the value of the A/D comming from the accelerometer.
I enclose the MATLAB side,

%Graph Accelerometer Data




s = serial('COM5');
set(s,'BaudRate',9600);
set(s,'DataBits',8);
set(s,'StopBits',1);
fopen(s);

out ='a'
figure
hold on
xacc =[];
yacc =[];
zacc =[];
for i = 1 : 5000
    out = fscanf(s);
    if(sum(size(out))>0)
        out2 = reshape(out(1:(round(size(out,2)/5)-1)*5),[5 (round(size(out,2)/5)-1)]);
        out2 = out2';
        out2 = str2num(out2(1:end,2:end)); 
        if out(1) == sprintf('X') 
            xacc = [xacc out2(1:3:end)'];
            plot(xacc);
            yacc = [yacc out2(2:3:end)'];
            plot(yacc,'r');
            zacc = [zacc out2(3:3:end)'];
            plot(zacc,'g');
        end
        drawnow
    end
end


fclose(s)
delete(s)
clear s

This code only reads the signals from the serial port, stores them in different variables (depending on the axis) and plots them.

The main problem with Matlab ,though, is that is not fast enough to process all the datam, you’ll see (if you try the code) that the graphs are drawn pretty slow (I got a celeron but I do not think that’s the problem.

If you are interested in the Arduino code for that, let me know.

Dolphin

Hey,

did you use the code? let me know if it helped!!!

I will be posting the arduino part here https://webfiles.uci.edu/cagell/news/news.htm.

Dolphin

Reading serial and writing to serial is very easy in matlab, but it sometimes hang, serial port already in use and then u need to restart matlab.

The matlab documentation is very good, just type doc serial.

s = serial('COM1','BAUD',4800);
fid = fopen(s);

%do whatever u want to fid here.....
fclose(s);

Hello, I'm looking to use matlab to interact with a Diecimla board. I think its on serial com port 3 on my machine, but i'm not quite sure where to go from here.

for a test I was thinking of maybe having some kind of random number generator uploaded onto the board, then send that as the output.

My goal here is to use matlab to control an input to the board via the usb. But just a little help with my beginner problem would be enough for me to figure the rest out hopefully.

Never mind I'll find something else... This isn't going to work. [smiley=sad.gif]

If I have time before school starts up again, I'll try to get Matlab talking with Firmata. I would also like to see a Firmata library for Labview. If anyone else has time, please start and post your progress in the playground!

Hello Dolphin: I tried your code. The Arduino Side didnt work really, I hat to change printByte to Serial.print and came to the following piece of code:

int ledPin = 13;
int xaccPin = 0;
int yaccPin = 1;
int zaccPin = 2;
int value = 0;

void setup() {
  //beginSerial(9600); // Sets the baud rate to 9600
  Serial.begin(9600);

  pinMode(ledPin, OUTPUT);
  pinMode(xaccPin, INPUT);
  pinMode(yaccPin, INPUT);

}

void readAcceleration(int axe){
  value = analogRead(axe);

  if (axe == xaccPin){
    Serial.print('X',BYTE);
  }
  if (axe == yaccPin){
    Serial.print('Y',BYTE);
  }
  if (axe == zaccPin){
    Serial.print('Z',BYTE);
  }

  /* printInteger(value); */
  Serial.print(value);
  Serial.print(' ',BYTE);
}

void loop() {
  readAcceleration(xaccPin); //reads and represents acceleration X
  readAcceleration(yaccPin); //reads and represents acceleration Y
  readAcceleration(zaccPin); //reads and represents acceleration Y
  digitalWrite(ledPin, HIGH);
  delay(1200);
  digitalWrite(ledPin, LOW);

}

The code for Matlab did something to the serial port: The Arduino stopped blinking as soon as I executed the code in Matlab. And nothing was displayed in the graph. Whats the prob there? Thanks!

I know it's been a while since the last post on this topic, but I did find a solution which actually did work! Lowell Toms posted a great example on the Matlab File Exchange including two examples both the Arduino sketch and Matlab script. Ran on Mac OSX 1.5.8 with R2010a, and Arduino 0018.

Hope that helps!

http://www.mathworks.com/matlabcentral/fileexchange/26711-interacting-with-an-arduino-using-the-matlab-serial-commands

UPDATE: There are now 2 standard ways to interact with the Arduino From MATLAB and Simulink:

MATLAB Interface for Arduino You can download the code for free here : http://www.mathworks.com/academia/arduino-software/arduino-matlab.html

This lets you control your Arduino board over USB from a MATLAB session, in either Windows, Mac or Linux. For example you can instantiate an Arduino object from MATLAB using:

a=arduino('COM5');

and then do stuff like:

av=a.analogRead(5);

a.pinMode(13,'output'); a.digitalWrite(13,1);

delete(a)

or if you have a ladyada motor shield:

a.servoAttach(1); a.servoWrite(1,45);

a.motorSpeed(4,200) a.motorRun(4,'forward');

a.stepperSpeed(1,10) a.stepperStep(1,'forward','double',100);

You can also download these slides: http://www.mathworks.com/matlabcentral/fileexchange/27843 or look at this video to learn more: http://www.mathworks.com/company/events/webinars/wbnr43537.html

Arduino Target for Simulink This lets you use Simulink to create programs that run directly on the Arduino board. You can download the code for free here : http://www.mathworks.com/academia/arduino-software/arduino-simulink.html

The target (available for Windows only) allows a Simulink model (and possibly also Embedded Matlab Code) to be automatically converted to C/C++ code, compiled and downloaded to the Arduino. This is especially useful for control application that need to run at high sampling rate, and in cases where you don't want the Arduino to be connected to a host computer.