Slow running stepper motor with matlab

I used two microstep drivers and two stepper motors. According to the code, they are rotating at the same speed in the counterclockwise (CCW) direction. Now, I am attempting to control the stepper motor operation using Matlab. Unfortunately, the stepper motor speed is lower compared to when the same code is operated with Arduino IDE.

matlab code

close all
clear
clear a;

% Connect Arduino board
a = arduino('COM13','Uno');

% Set pin modes for motor 1
configurePin(a, 'D9', 'DigitalOutput');  % PUL for motor 1
configurePin(a, 'D8', 'DigitalOutput');  % DIR for motor 1

% Set pin modes for motor 2
configurePin(a, 'D3', 'DigitalOutput');  % PUL for motor 2
configurePin(a, 'D2', 'DigitalOutput');  % DIR for motor 2

while true
    writeDigitalPin(a, 'D8', 1); % Set motor 1 direction
    writeDigitalPin(a, 'D2', 1); % Set motor 2 direction
    
    for x = 0:799
        writeDigitalPin(a, 'D9', 1); % Set motor 1 pulse
        pause(0.0001);
        writeDigitalPin(a, 'D9', 0);
        pause(0.0001);
        
        writeDigitalPin(a, 'D3', 1); % Set motor 2 pulse
        pause(0.0001);
        writeDigitalPin(a, 'D3', 0);
        pause(0.0001);
    end
    
    pause(0.0001);
end

Matlab must communicate with the Arduino over the serial port, and that takes time.

when the same code is operated with Arduino IDE

What do you mean by "the same code"? Matlab code doesn't run on an Arduino.

1 Like

I mean comparing the running of the code for 800 micro steps for the stepper motor in Arduino IDE gives a rotational speed higher than for the same 800 micro steps in matlab.

arduino code

int x;

void setup()
{
  pinMode(9, OUTPUT); // set Pin9 as PUL for motor 1
  pinMode(8, OUTPUT); // set Pin8 as DIR for motor 1
  pinMode(3, OUTPUT); // set Pin3 as PUL for motor 2
  pinMode(2, OUTPUT); // set Pin2 as DIR for motor 2
}

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

for (x = 0; x < 800; x++)
{
    digitalWrite(9, HIGH); 
    delayMicroseconds(1000); 
    digitalWrite(9, LOW); 

    delayMicroseconds(1000); 

    digitalWrite(3, HIGH); 
    delayMicroseconds(1000); 
    digitalWrite(3, LOW); 

}
  delay(10);

}

Don't you need to take that up with Matlab?

https://uk.mathworks.com/support/contact_us.html

https://uk.mathworks.com/matlabcentral/?s_tid=gn_mlc

Use shorter delays in the Matlab code.

You seem to send a single command for each and every step-pulse from mathlab to an arduino where my assumption is that the arduino runs some kind of mathlab-receiving code that then switches IO-pins LOW/HIGH on each received command.

This is counter-productive to how systems are divided into subsystems where each sub-system does certain things.

You should organise your system in that way that mathlab is sending commands
"rotate 800 steps clockwise"
and the arduino is doing the details for creating the step-pulses for the 800 steps.

On what kind of device is mathlab running in your project?

best regards Stefan

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.