Serial Communication btwn Matlab and Arduino

Click For Summary
SUMMARY

This discussion focuses on establishing serial communication between MATLAB and Arduino, specifically addressing issues with data reading. The Arduino code initializes serial communication at 9600 baud and reads a digital input, while the MATLAB code attempts to read this input every second. The problem arises when MATLAB continuously displays the first value received instead of updating with new values. Solutions include using the fread() function for reading bytes and ensuring proper handling of the serial buffer to avoid overruns.

PREREQUISITES
  • Understanding of Arduino programming and digital input reading
  • Familiarity with MATLAB's serial communication functions
  • Knowledge of serial port configuration and baud rates
  • Experience with data handling in MATLAB, particularly with fread() and fscanf()
NEXT STEPS
  • Learn how to implement fread() in MATLAB for efficient byte reading
  • Research the use of BytesAvailable and TransferStatus properties in MATLAB
  • Explore serial buffer management techniques to prevent data overruns
  • Review the Arduino Serial tutorial for best practices in serial communication
USEFUL FOR

Engineers, developers, and hobbyists working with Arduino and MATLAB who are looking to implement or troubleshoot serial communication effectively.

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
6K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
7K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K