Take the 2-minute tour ×
Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It's 100% free, no registration required.

I want to use sensors on an Arduino to control values in a MATLAB program. What are my options for communicating?

I've used Processing to receive data sent via the serial cable, and that strategy has worked pretty well. Is there something similar I can do using MATLAB? I'm somewhat new to MATLAB, so gentle guidance would be great.

I'm not really looking for something like the ArduinoIO package -- I want to have separate computation executing on the Arduino and occasionally informing my MATLAB program (which will occasionally poll the serial line, I suppose).

share|improve this question
6  
Do you need help writing the code for the Arduino, or help writing MATLAB code to read data input from the serial stream? –  anorton Feb 13 at 2:54
1  
This question appears to be off-topic because it is about programming in Matlab. –  The Guy with The Hat Feb 13 at 21:57

3 Answers 3

MATLAB Support Package for Arduino (aka ArduinoIO Package) allows you to do it fairly easily.

Sample usage:

%-- connect to the board

a = arduino('COM9')

%-- specify pin mode

a.pinMode(4,'input');

a.pinMode(13,'output');

%-- digital i/o

a.digitalRead(4) % read pin 4

a.digitalWrite(13,0) % write 0 to pin 13

%-- analog i/o

a.analogRead(5) % read analog pin 5

a.analogWrite(9, 155) % write 155 to analog pin 9

share|improve this answer
    
If I understand ArduinoIO correctly, all the computation is initiated on the Matlab side and the Arduino just becomes a sensor extension. That isn't what I want. I want to have a program on the Arduino (doing it's own computation) that occasionally communicates with Matlab. –  Bill Nace Feb 15 at 3:26
1  
The MATLAB side simply provides you with the functions to communicate with the Arduino. It's up to you how/when you want the two to communicate. The Arduino can write to the serial port and this allows you to read from it. –  sachleen Feb 15 at 3:28

I don't know a whole lot about MATLAB, but I found some tutorials about reading and writing from the serial port:

s = serial('COM1');
fopen(s)
fprintf(s, 'Your serial data goes here')
out = fscanf(s)

out is now your received data, and you can do whatever you want with it.

To close:

fclose(s)
delete(s)
clear s

It is from Writing and Reading Data (MATLAB/ Data and File Management/ Serial Port Devices).

From your question I assume you know about the Arduino side of things (using Serial).

share|improve this answer

I've established a connection, sending numbers. By just changing Serial.write to Serial.print and fread to fscanf you can send/receive clear ASCII text instead of binary data.

Both matlab and arduino code can be found at this question:

http://stackoverflow.com/questions/24368670/matlab-plot-serial-data-continously

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.