8

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).

3
  • 6
    Do you need help writing the code for the Arduino, or help writing MATLAB code to read data input from the serial stream?
    – apnorton
    Commented Feb 13, 2014 at 2:54
  • 1
    This question appears to be off-topic because it is about programming in Matlab. Commented Feb 13, 2014 at 21:57
  • The serial port is your best bet. I would define a simple message format bases on your needs. If you only are doing simple stuff like valve on/off: I would define a start byte followed by a fixed number of data bytes. If you want to get more complicated send a length byte first, followed by that many data bytes. Please remember: don't send serial bytes nonstop, always have a few breaks (sleep cycles) in the data which allows the serial chips to clock recover.
    – benathon
    Commented Mar 31, 2015 at 7:04

4 Answers 4

5

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

2
  • 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
    Commented Feb 15, 2014 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
    Commented Feb 15, 2014 at 3:28
3

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).

0

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:

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

-1

clc clear all global value global value1 global val5 global val6 global val7 global val8 global val9 a=arduino('COM4'); value = zeros(); value1 = zeros(); val5 = zeros(); val6 = zeros(); val7 = zeros(); val8 = zeros(); val9 = zeros(); for i=1:1000

val1(i)=analogRead(a,0)/2; %incomming

val2(i)=analogRead(a,1)/2; %heater

val3(i)=analogRead(a,2)/2; %heat sink

val4(i)=analogRead(a,5)/2; %atmsphr

val8(i)=analogRead(a,6)/2; %pressure

val9(i)=analogRead(a,7)/2; %pressure

val(i)=analogRead(a,0)/2;

value(i)= (val1(i)+val4(i)+val3(i))/3;

value1(i) =(val8(i)-val9(i)); %pressure

val5(i)=analogRead(a,0)/2 %incoming

val6(i)=analogRead(a,4)/2 %outgoing

val7(i) = value(i)-val6(i) plot(value, 'r'); title('heat sink temprature');

hold on

plot(val5, 'k');

title('incoming air temprature');
plot(val6, 'g');

title('outgoing air temprature');

 plot(val4, 'k');
 title('atmospheric temperature');

plot(val7,  'c');
title('change in temprature');

pause(6)

figure
plot(value1,  'r');
title('change in pressure'); 

pause(6)

end

1
  • 1
    Providing a code-answer without an explanation on what it does it's not a valid answer here on StackExchange. Can you edit your post providing some informations? Commented Mar 31, 2015 at 7:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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