How to read multiple sensors through the serial.

Hi guys
I hope you can help me.
I have connected 3 sensors to an Arduino, and I´m sending the data to the serial port simultaniously – no problems so far.
Now I would like Matlab to read the serial and plot 3 graphs, 1 from each sensor.
My problem is, that I dont know how to seperate the serial so each of the 3 dataset is placed in its own variable so I afterwards can make the plots.
This is my code:
obj = serial('COM17'); %creating the object
set(obj,'BaudRate',9600) %setting baudrate
fopen(obj); %open port
set(obj,'terminator','cr') %providing the terminator
pressure1=0; % a variable for y axis
time1=now; % time is in x axis
while 2>1
time2=now;
x=[time1 time2];
pressure2 = fscanf(obj) ;
pressure=[pressure1 pressure2];
datetick('x','HH:MM') %change the axis
pause(0.5);
pressure1=pressure2;
time1=time2;
end

5 Comments

raki - does the data from all three sensors go through the same serial port? If so, can you tag the data that each is sending out to that you know which sensor is sending which piece of data?
Sir, the code for arduino is shown below. How can i plot these 3 sensor values in 3 different axes created in matlab guide.
unsigned int pres;
unsigned int temp;
unsigned int fluidrate;
void setup()
{
Serial.begin(9600);
}
void loop()
{
pres = analogRead(0);
temp = analogRead(1);
fluidrate = analogRead(2);
Serial.println(pres);
Serial.println(temp);
Serial.println(fluidrate);
}
Can you tag the data from this sensor? I have no experience with Arduino so can only offer suggestions. Are there alternatives to Serial.println?
arduino code is
unsigned int pres;
unsigned int temp;
unsigned int fluidrate;
void setup()
{
Serial.begin(9600);
}
void loop()
{
pres = analogRead(0);
temp = analogRead(1);
fluidrate = analogRead(2);
Serial.print(pres);
Serial.print("\t");
Serial.print(temp);
Serial.print("\t");
Serial.print(fluidrate);
Serial.print("\n");
delay(1000);
}
The data in serial monitor of arduino is
257 7 14
273 23 46
327 77 154
340 90 180
345 95 190
352 102 204
Matlab code is
clear all
clc
arduino=serial('COM8','BaudRate',9600);
fopen(arduino);
x=linspace(1,100);
numcols = 1;
y = {};
for i=1:length(x);
data =fscanf(arduino,'%f');
IncomingString = char(data);
IncomingString = regexp(IncomingString, '\s*', 'split');
pres(i,1)= IncomingString(1,1);
temp(i,1)= IncomingString(1,2);
fluidrate(i,1)= IncomingString(1,3);
end
fclose(arduino);
Plotpres = str2double(pres);
figure(1)
plot(Plotpres);
title('pressure'); xlabel('Samples'); ylabel('pres in bar');
Plottemp = str2double(temp);
figure(2)
plot(Plottemp);
title('temperature'); xlabel('Samples'); ylabel('temp in C');
Plotfluidrate = str2double(fluidrate);
figure(3)
plot(Plotfluidrate);
title('fluidrate'); xlabel('Samples'); ylabel('fluidrate in l/min');
I am getting error ??? Error using ==> regexp The first argument (STRING) must be a one-dimensional array of char or cell arrays of strings.
Error in ==> monitorsystem at 11 IncomingString = regexp(IncomingString, '\s*', 'split');
Please help me to get the output
Raki - the error messages seems very clear. regexp is expecting a one-dimensional array of characters and yet it is probably getting something different. Put a breakpoint at this line, run your code, and when the debugger pauses at this line check to see what the IncomingString looks like.

Sign in to comment.

Answers (0)

Categories

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

Asked:

on 10 Apr 2015

Edited:

on 6 Jan 2017

Community Treasure Hunt

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

Start Hunting!