MATLAB Answers


Arduino-Matlab Serial Communicatoin

Asked by David Zamora on 14 Jul 2018 at 2:42
Latest activity Commented on by David Zamora about 17 hours ago

I have a stepper motor connected to an Arduino Uno MC. Arduino and Matlab are communicating through the serial port, and I want to send to the Arduino the number of steps it needs to rotate. When I do so, the motor doesn't move. From the print statements that I placed in my code, I know Matlab is executing that line, but the stepper motor doesn't do anything. Why is the stepper motor not moving? What is the best way to send an integer number of steps to the Arduino? What's the best way to have Arduino read that value from Arduino? Below is my matlab code and my arduino code, respectively.

% July 2018
% Run and test program block by block
a = serial('com3', 'BaudRate', 115200);   % establish communication with Arduino
fopen(a);           % same as above
global trgt;
trgt = -0.14;
while 1
    temp = fscanf(a);
    magY = str2double(temp);
    s1 = "Magnetometer value: " + magY;
      steps = pidCtrl(magY);
      s3 = "Control Signal: " + steps;
      fprintf(a, num2str(steps));   % send to Arduino
 /* July 2018
 * northTracker.ino
 * This program runs parallel to 'northTracker.m'. 
 * Needs to be programmed and tested block by block with its parallel program
 * to ensure that everything works as expected. 
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_LSM9DS0.h>
#include <Stepper.h>
#include <math.h>
#define LSM9DS0_XM_CS 10
#define LSM9DS0_GYRO_CS 9
#define LSM9DS0_SCLK 13
#define LSM9DS0_MISO 12
#define LSM9DS0_MOSI 11
const int stepsPerRev = 64;
Adafruit_LSM9DS0 lsm = Adafruit_LSM9DS0(1000);
Stepper myStep(stepsPerRev, 8, 10, 9, 11);
sensors_event_t accel, mag, gyro, temp;
String matlabData;
void configureSensor(void) {
void setup() {
  // put your setup code here, to run once:
  if(!lsm.begin()) {
    // runs if there was a problem detecting the IMU
    //Serial.print(F("Ooops, no LSM9DS0 detected ... Check your wiring or I2C ADDR!"));
  //Serial.println(F("Found LSM9DS0 9DOF"));
void loop() {
  // put your main code here, to run repeatedly:
  lsm.getEvent(&accel, &mag, &gyro, &temp); 
  Serial.println(mag.magnetic.y);      // sends magnetometer data to Matlab
  if (Serial.available() > 0) {
    matlabData = Serial.readString();
    int steps = matlabData.toInt();


Is the arduino code getting past the setup routine?

Yes the arduino code is getting past the setup routine.

Sign in to comment.

1 Answer

Answer by Brian Hu
on 17 Jul 2018 at 20:40

Hi David,

From the information and code you provided, there are a few areas which could be causing issues for the motor functionality. Here are several methods you can try which may help you resolve your issue.

1. Make sure you are receiving the expected signal at the serial port on the Arduino. Certain factors, such as baud rate mismatch or library discrepancies between MATLAB and the Arduino can cause transmitted signals to differ from their expected values along the serial transmission.

2. It seems like you may be missing a call to set the “ReadAsyncMode” property of your serial port to ‘manual’ in your MATLAB code. From the documentation, the “readasync” function is ignored if “ReadAsyncMode” is ‘continuous’ for serial port applications. The documentation page for “readasync” is shown below.

3. If you can verify that the Arduino is transmitting and receiving expected values, it might be possible that your stepper motor is not receiving enough power to drive the steps. Depending on the power rating of the stepper motor, it is possible that the microcontroller alone is not able to supply enough power to drive the motor. If this is the case, you can resolve the power issue by either using a motor drive or implementing one through your own circuit design.

4. If you are interested in alternatives for facilitating stepper motor functionality, MATLAB offers a support package for Arduino hardware interfacing which may be able to provide further assistance for your issue. Here is a helpful link for more information.

  1 Comment

Thank you very much Mr. Hu

Sign in to comment.