Serial input from Matlab for AccelStepper

Hello Guys. I originally posted this question in motors but I think its more of a programming issue.

I have matlab send my Arduino Uno a number of steps to rotate to the stepper motor to. At the moment I’m trying to debug my code because the stepper motor only moves once, and then stops. I’ve replaced all inputs with actually integers, so I expect the motor to rotate no matter what number I enter into my matlab prompt. I’ve also disconnected the motor and have plopped an LED down into one of the output pins - it should blink each time the board triggers the motor shield. At the moment it only blinks once, when it should blink multiple times as the motor runs.

My Arduino code is

#include <AccelStepper.h>

int matlab_command_steps = 0;
// Define a stepper and the pins it will use
AccelStepper stepper; // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5

void setup()

void loop()
  if (Serial.available() > 0)
	 matlab_command_steps =; // read data from matlab
  if (matlab_command_steps != 0)
   stepper.moveTo(20); // I would like matlab step input to go here eventually
   while(stepper.currentPosition() != 20);
  matlab_command_steps = 0; // reset command

and heres my Matlab code. I’m quite confident its working since I receive my input back from the Arduino.

clear all
arduino=serial('COM5','BaudRate',9600); % create serial communication object on port COM4
fopen(arduino); % initiate arduino communication
    answer_steps=input('Enter number of steps to rotate: '); % ask user to enter steps
    fwrite(arduino, answer_steps, 'uint8', 'sync'); % send steps variable content to arduino
    x = fscanf(arduino); % confirm Arduino received my signal
fclose(arduino); % end communication with arduino

I don’t really know what to do anymore, I’ve tried a lot of variants and nothing is working :(.


Open the serial port. Then, block until all pending serial data (none, that is) has been sent. I fail to see how that is useful.

     matlab_command_steps =; // read data from matlab

You really should correct the comment: // Read ONE character of data from matlab

Or one byte, depending on how matlab is sending data (ASCII or binary).

Given that you block until the stepper gets to the commanded position and run at a slow speed, I also fail to see why you need to use AccelStepper instead of Stepper.

I originally tried using stepper, but it behaved randomly at slow speeds.

I originally tried using stepper, but it behaved randomly at slow speeds.

I've looked at the Stepper library. Nowhere in the library does it call random(), so this does not seem likely.

I removed the flush command, and I'm still seeing the same behaviour. I don't know why the board would only trigger once, the while loop should keep running until 20 steps are completed?

So, forget matlab for a while. Can you run any sketch that makes the motor move the way you want? If so, then the problem is with the software. If not, then the problem is with the (undefined) hardware.

Perhaps it's time for you to define what hardware, besides the Arduino, you are using, and how it is connected.

Hi there,

I am a complete Newbie to all of this so for me it is already comforting to know that it is possible to use Accel Stepper via Matlab ( Did it work out for you in the end?). Is there anything special I have to take care of when installing Arduino IO? Which pde did you upload?

Sorry for cornering you with all these questions but there is so little information on Accel stepper with Matlab that I am desperate in finding people with experience in using that interface combination!Especially because my Prof is slowly getting impatient with me doodling about with stepper motors without any result!

Many thanks in advance for your help!