Go Down

Topic: Serial communication between Arduino & MATLAB (Read 243 times) previous topic - next topic


Sep 18, 2018, 02:50 pm Last Edit: Sep 18, 2018, 02:54 pm by khchin920
Hi guys, now i'm handling a project which involve control of a stepper motor.

First of all, all the processing will be done in the MATLAB, whereas the Stepper motor which drive by the driver of TB6600, will receive the information from the MATLAB and trigger the stepper motor to rotate. But, not matter what method i used to convert the Serial read value which is in bits back to the integer which suppose be the same as the MATLAB value (ReqS0), and it doesn't show what i want.

Another question is, while the Arduino establish serial communication with MATLAB, the serial monitor will not work. Therefore, i don't know what kind of data is transmitted to the Arduino. Anyway to sort this out?

Here is my MATLAB code:
A0 = evalin('base','A0');
ad=serial('COM4'); % assign serial port object
set(ad, 'BaudRate', 9600); % set BaudRate to 9600
set(ad, 'Parity', 'none'); % set Parity Bit to None
set(ad, 'DataBits', 8); % set DataBits to 8
set(ad, 'StopBit', 1); % set StopBit to 1
set(ad, 'OutputBufferSize', 8);
set(ad, 'InputBufferSize', 8);
set(ad,'Timeout', 5);
%display the properties of serial port object in MATLAB Window
ReqS0 = round(A0/0.1125);



fprintf(ad, '%s',ReqS0);

Here is my Arduino code:
// defines pins numbers
const int stepPin = 5;
const int dirPin = 7;
const int enPin = 6;

unsigned int integerValue=0;  // Max value is 65535
char incomingByte;
int value;

void setup()

void loop()
  if (Serial.available() > 0) {   // something came across serial
    integerValue = 0;         // throw away previous integerValue
    while(1) {            // force into a loop until 'n' is received
      incomingByte = Serial.read();
      if (incomingByte == '\n') break;   // exit the while(1), we're done receiving
      if (incomingByte == -1) continue;  // if no characters are in the buffer read() returns -1
      integerValue *= 10;  // shift left 1 decimal place
      // convert ASCII to integer, add, and shift left 1 decimal place
      integerValue = ((incomingByte - 48) + integerValue);
    Serial.println(integerValue);   // Do something with the value
Even though the method i found from online source, but it still not working... Anyway, thank you for your concern.  :)


I don't know Matlab but I doubt if it uses smileys. You should use the code button </> for code.

For receiving data on the Arduino have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

The technique in the 3rd example will be the most reliable.

You can send data in a compatible format with code like this - I presume you can implement a similar scheme with Matlab
Code: [Select]
Serial.print('<'); // start marker
Serial.print(','); // comma separator
Serial.println('>'); // end marker

Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up