I have been able to connect MATLAB to my Uno with this line of code: a = arduino('COM4'); through this package: Matlab support package for Arduino.

Right now, MATLAB is my main script that will synchronize all the components [like the Arduino].

I am able to send pulses to my stepper-motor just by using:

void loop() {
    digitalWrite(2, HIGH);
    digitalWrite(2, LOW);

This works fine, and will make the motor move about once every 2 milliseconds. My problem is that I cannot find a way to produce this same delay through MATLAB's interface. I do know that MATLAB has a pause() function, but when I set up a loop in MATLAB like this:

a = arduino('COM4');
for m = 1:400
    a.digitalWrite(2, 1);
    a.digitalWrite(2, 0);

Each step takes WAY longer - about 200 milliseconds each.

What are other options for creating the pause between digital High / Low being sent to the Arduino? It would be nice if I could control the outcome from Matlab, through Arduino.

  • It sounds like you need something like GRBL. It's a stand-alone program for an arduino board that interprets a command language, and produces coordinated stepper motor motions. It's largely intended for CNC machining, but it can probably do what you need. Feb 12, 2014 at 6:49

2 Answers 2


The increased in observed delay is due to the time it takes to send the command from MATLAB to the Arduino have its code execute the requested command. In your case the a.digitalWrite. So actual time between pulses using MATLAB's delay is 0.001 pause plus the time of baud rate times the packet size and decoding then execution.

I would recommend not pursuing this solution of the MATLAB controlling the timing of Step Motors, remotely. As this latent coupling will be a frustrating limitation. Noting that for step motors the timing can not afford such latent execution. Hence all the 3D printers and many CNC with Step Motors use Arduino's which get G-Code (motor move commands with destinations) and the Real-time firmware handles the stepping, within tolerances.

I would suggest using something like grbl firmware with step motor shield and then have the MATLAB send the needed G-code. Where there are others with much more features but tailored to 3d Printing. grbl is simpler and straight to the point of moving motors.


It's not clear from your question whether you are trying to get real-time execution without latency or simply want to adjust the motor delay time from MATLAB. If the latter, a good way is to send delay data (e.g. a number that represents how long you want between pulses) vie serial, which your Arduino program will receive and update the motor delay as desired. Or, bearing in mind RAM limitations, you can send a whole sequence of delays that can run one after another with precise timing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.