Matlab and arduino: serial port connection

In summary, the conversation is about trying to hook up MATLAB to an Arduino and retrieve a single number from the serial port. The individual has provided code for both the Arduino and MATLAB sides, but is having trouble getting only the number "3" to come out. They discuss using different terminators, and ultimately suggest making sure that all serial settings are the same on both ends of the cable. They also question the use of both fread() and fscanf() in the same run.
  • #1
lcr2139
62
1
Hello, I am trying to hook up MATLAB to my arduino. I am trying to write a number to the serial port in the arduino IDE and retrieving the same number in the MATLAB IDE. My arduino code is:

void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}

void loop() {

if (Serial.available()){
Serial.println(3);

delay(100); // delay in between reads for stability
}
}


And my MATLAB code looks like:

%s = serial('COM9');
s = serial ( 'COM9' , 'BaudRate' , 9600, 'terminator' , 'CR' );
%set(s,'BaudRate',4800);
fopen(s);

fprintf(s,'*IDN?')
fread(s)
disp(s)
out = fscanf(s);
fclose(s)
delete(s)
clear s

and the MATLAB output looks like:

10
51
13
10
51
13
10
51
13

How can I change this so only "3" comes out?
Thanks!
 
Physics news on Phys.org
  • #2
Double check that you have the right parity and numbers of stop bits.
 
  • #3
Sorry, I'm pretty new at this. What are parity and numbers of stop bits?
 
  • #4
In http://home.iitb.ac.in/~rahul./ITSP/serial_comm_matlab.pdf the guy uses LF for the terminator. Try it.

J.
 
  • #5
LF gives a Terminator error so I have to use CR.
 
  • #6
Okay, then try the following

Arduino

Code:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600,SERIAL_8N1);
}

void loop() {
  if (Serial.available()){
    Serial.println(3);
    delay(100); // delay in between reads for stability
    }
}

On the Matlab side:

Code:
s = serial ( 'COM9' , 'BaudRate' , 9600, 'DataBits', 8, 'Parity', 'none', 'StopBits', 1, 'terminator' , 'CR' );
fopen(s);
fprintf(s,'*IDN?')
fread(s)
disp(s)
out = fscanf(s);
fclose(s)
delete(s)
clear s

This will make sure that everything serial is set the same on both ends of the cable.

Also, something I have some difficulties understanding: you are using fread() and fscanf() in the same run. Why? What's the content of the variable out?
 

FAQ: Matlab and arduino: serial port connection

How do I establish a serial connection between Matlab and Arduino?

To establish a serial connection between Matlab and Arduino, you will need to connect the Arduino board to your computer using a USB cable. Then, open the Arduino IDE and upload the "StandardFirmata" sketch to the board. In Matlab, use the "serial" function to create a serial port object and specify the port name and baud rate. Finally, use the "fopen" function to open the serial connection.

What is the purpose of using a serial connection between Matlab and Arduino?

The purpose of using a serial connection between Matlab and Arduino is to enable communication between the two devices. This allows you to send and receive data from sensors connected to the Arduino board and use that data in your Matlab code.

How can I send data from Matlab to Arduino through the serial connection?

To send data from Matlab to Arduino through the serial connection, you can use the "fprintf" function. This function allows you to write data to the serial port. Make sure to specify the correct format for the data, such as a string or numeric value, to ensure proper communication with Arduino.

Can I control Arduino inputs and outputs from Matlab using the serial connection?

Yes, you can control Arduino inputs and outputs from Matlab using the serial connection. By sending commands through the serial port, you can toggle digital and analog pins, read sensor values, and control other components connected to the Arduino board.

How do I close the serial connection between Matlab and Arduino?

To close the serial connection between Matlab and Arduino, use the "fclose" function. This will terminate the connection and free up the serial port for other applications to use. It is important to properly close the connection to avoid any potential issues or conflicts with other devices.

Similar threads

Replies
2
Views
15K
Replies
1
Views
3K
Replies
1
Views
7K
Replies
9
Views
2K
Replies
1
Views
6K
Replies
1
Views
1K
Back
Top