MATLAB Serial Communication btwn Matlab and Arduino

Click For Summary
The discussion focuses on establishing serial communication between Matlab and Arduino, specifically for reading a logical input (0 or 1) from the Arduino and displaying it in Matlab. The provided Arduino code successfully sends the input value, but Matlab only displays the first value repeatedly, indicating a potential issue with how data is read. Suggestions include using the fread() function instead of fscanf() to read data byte-by-byte, which may resolve the issue of repeated values. Additionally, implementing a header in the data stream and checking the buffer size in Matlab can help ensure accurate data retrieval. Properly configuring these elements should enable Matlab to reflect real-time changes in the Arduino input.
xentity1x
Messages
9
Reaction score
0
I'm trying to setup serial communication between Matlab and an Arduino. As a test I have a logical input to the Arduino (it can be either 0 or 1). I then want to pass the value of the input to Matlab and display it. Here's the Arduino code I have:
Code:
void setup() {
  Serial.begin(9600);
  pinMode(5, INPUT);
}

void loop() {
  
  
  int NPleft = digitalRead(5);
  Serial.println(NPleft);
  
}

Here's the Matlab code:
Code:
delete(instrfindall)
clear all
s1 = serial('COM3');    % define serial port
s1.TimerPeriod=1;
s1.Terminator='LF';
fopen(s1);
s1.BaudRate=9600;               % define baud rate
s1.TimerFcn=@(x,y)disp(char(fscanf(s1)));

I want Matlab to check the value being sent to it serially every second. What ends up happening is it just displays the first value it gets over and over again. So if the first value of the input is 1, it keeps displaying 1 over and over again regardless if the input goes back to 0. What am I doing wrong?
 
Physics news on Phys.org
Can you try with more simple serial output, like the Hello World program in this Arduino serial tutorial?
http://www.ladyada.net/learn/arduino/lesson4.html

You might want to use a terminal program like Teraterm before moving onto MATLAB (go for the open-source one, not the one from Ayera):
http://ttssh2.sourceforge.jp/

Within MATLAB, I believe that the serial buffer size is 512 bytes, and you should be able to use the 1-second polling as long as you don't have a buffer overrun (receiving more than 512 bytes). You may want to use the BytesAvailable and/or TransferStatus along with a loop to print out new values as they're received (more documentation under the Reading Data section):
http://www.mathworks.com/help/techdoc/matlab_external/f62852.html

Good luck!
 
Last edited by a moderator:
xentity1x said:
I'm trying to setup serial communication between Matlab and an Arduino. As a test I have a logical input to the Arduino (it can be either 0 or 1). I then want to pass the value of the input to Matlab and display it. Here's the Arduino code I have:
Code:
void setup() {
  Serial.begin(9600);
  pinMode(5, INPUT);
}

void loop() {
  
  
  int NPleft = digitalRead(5);
  Serial.println(NPleft);
  
}

Here's the Matlab code:
Code:
delete(instrfindall)
clear all
s1 = serial('COM3');    % define serial port
s1.TimerPeriod=1;
s1.Terminator='LF';
fopen(s1);
s1.BaudRate=9600;               % define baud rate
s1.TimerFcn=@(x,y)disp(char(fscanf(s1)));

I want Matlab to check the value being sent to it serially every second. What ends up happening is it just displays the first value it gets over and over again. So if the first value of the input is 1, it keeps displaying 1 over and over again regardless if the input goes back to 0. What am I doing wrong?

try sending in a header, then read the next byte, use the function fread() not fscanf(). fread() reads one byte at a time. For example

Code:
ser = serial('COM4', 'BaudRate', 38400);
one_byte = fread(ser, 1, 'int8'); %reads exactly one byte
two_bytes = fread(ser, 1, 'int16'); %read exactly two byes and converts them to 1 integer (why?)
 
Last edited:

Similar threads

  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
Replies
7
Views
2K
  • · Replies 1 ·
Replies
1
Views
7K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K