Serial Communication btwn Matlab and Arduino

In summary, serial communication between Matlab and Arduino allows for the transfer of data and commands between the two devices. This communication is achieved through a physical connection using a serial port and a specific protocol for data transmission. The process involves setting up the communication parameters, establishing a connection, and then sending and receiving data. This method allows for efficient real-time control and data acquisition, making it a popular choice for many projects and applications.
  • #1
xentity1x
12
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
  • #2
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:
  • #3
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:

1. What is Serial Communication between Matlab and Arduino?

Serial Communication between Matlab and Arduino is a method of data transfer between a computer and a microcontroller. It involves sending and receiving data through a serial port using a specific protocol.

2. How can I establish Serial Communication between Matlab and Arduino?

To establish Serial Communication between Matlab and Arduino, you will need to connect the Arduino board to your computer via a USB cable. Then, you will need to use the "Serial" library in your Arduino code and the "serialport" object in Matlab to send and receive data.

3. What are the advantages of using Serial Communication between Matlab and Arduino?

Serial Communication between Matlab and Arduino allows for real-time data transfer and control between a computer and a microcontroller. This enables you to use Matlab's powerful data analysis and visualization capabilities with the real-time input from sensors and actuators connected to the Arduino board.

4. Can I use Serial Communication between Matlab and Arduino for wireless communication?

Yes, you can use Serial Communication between Matlab and Arduino for wireless communication by using a wireless serial connection, such as Bluetooth or WiFi. However, you will need to make sure that both the Arduino and the computer have compatible wireless modules.

5. Are there any limitations to Serial Communication between Matlab and Arduino?

One limitation of Serial Communication between Matlab and Arduino is the maximum data transfer rate. The baud rate, which determines the speed of data transfer, is limited by the processing power of the Arduino board. Additionally, the length of the cable used for the serial connection can also affect the data transfer rate and reliability.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
Replies
7
Views
1K
  • Computing and Technology
Replies
1
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
1K
  • Electrical Engineering
Replies
5
Views
3K
  • Programming and Computer Science
Replies
1
Views
2K
Back
Top