Controlling 28BYJ-48 stepper motor with ULN2003 using Matlab

Hi guys...

I'm trying to use 28BYJ-48 stepper motor and I found a guy that made it working on Arduino, but using Matlab with the same functions, makes it very slow.

I'm using this code:

a=arduino('com3','uno);

pins={'D8', 'D9', 'D10', 'D11'};
steps={'1100', '0110', '0011','1001'};

steps_size=size(steps,2);                        %steps = 4

for i = 1:50

    step = steps{mod(i,steps_size)+1};      % step=1,2,3,4

    for j=1:4
        writeDigitalPin(a, pins{j}, str2double(step(j)));
    end

    %pause(0.01);

end

so basically, I'm changing pins D8-D11 (that are connected to ULN2003, so that they display 1100, 0110, 0011, 1001. I think this is how it supposed to work (?)...

Stepper motor is rotating, but even after commenting out pause, it's rotating super super slow.

I was trying to use AdaFruit functions, but I think I'm doing something wrong, or they shouldn't suppose to work with my setup.

Thanks for help.

Is that Matlab code (I don't know Matlab).

You can't expect a PC program to control a stepper motor step by step because of the delays in communication.

You should arrange for the PC program to send a message to the Arduino to move 100 or 1000 (or whatever) steps and then let the Arduino work out the fine details.

...R

1 Like

Yes. I thought that MATLAB generates code that he uploads to Arduino. Yet it seems like you cannot make standalone using MATLAB. You can do it using Simulink

Anyway the bottleneck in my solution was function writeDigitalPin(). It is possible to speed up this function 7x, by editing arduino.m in Matlab, here is the topic if anyone will need it:

https://forum.arduino.cc/index.php?topic=346289.0

siadajpan:
Anyway the bottleneck in my solution was function writeDigitalPin(). It is possible to speed up this function 7x, by editing arduino.m in Matlab, here is the topic if anyone will need it:

Matlab writeDigitalPin() speed (slow) - Interfacing w/ Software on the Computer - Arduino Forum

Hello,

what power supply have you used? I tried this and it still goes very slow.

Thanks
SRM15

Hi guys,
I have the same issues controlling 28BYJ-48 stepper with ULN2003 using MATLAB. I have used code very similar to that given above and made the trick to speed up x7 the process...

I want to control a usb cam and the stepper motor through matlab so as to rotate 2 degrees each time the stepper motor take a picture of an object attached on the stepper motor and continue this process until a full 360 degree rotation is reached i.e. take 180 pictures of the object from respective angles.

In total the stepper motor makes 4096 steps for a 360 degree rotation thus for rotating 2 degrees:

4096 steps equal 360 degrees
x steps equal 2 degrees

x= 22.7555556 steps

in code x is k and has to be an integer (right?) so the problem is that with my code below if I use k=23 (23 steps is approximately 2 degrees) the actual rotation is approximately 380 degrees instead of 360...

and if I use k=22 steps the actual rotation is approximately 300 degrees instead of 360.

I do not mind to have a small error 2-5 degrees ... but 20 degrees or 60 degrees error is huge... any ideas? any suggestions to overcome this problem?
If I buy adafruit motor shield and use the adafruit library to control this motor would it give me a solution? Does adafruit shield support 28BYJ-48 5Volt stepper motor?

% Use cam as the name of the object, 
% type 'webcamlist' to find out which are the available cameras in your
% system, choose the appropriate cam, note 1 is the first available webcam.
cam = webcam(1);
% set camera resolution 
cam.Resolution = '640x480';
%preview cam
preview(cam)


%initializing a counter for numbering the acquire images
counter = 000;  %initialize filename increment

%initializing connection with arduino
a=arduino('com3','Mega2560');

%setting up arduino pins and steps for stepper motor
pins={'D8', 'D9', 'D10', 'D11'};
steps={ '0001','0011', '0010','0110','0100', '1100',  '1000', '1001'};
steps_size=size(steps,2);  %number of steps = 8
configurePin(a,'D8','DigitalOutput');
configurePin(a,'D9','DigitalOutput');
configurePin(a,'D10','DigitalOutput');
configurePin(a,'D11','DigitalOutput');

%pause waiting for preview
 pause(6);
 closePreview(cam);

for i = 1:1:180
%take snapshot
img = snapshot(cam);
savename = strcat('C:\Users\orf\Desktop\New Image Sets\' ,num2str(counter, '%03i'), '.jpg'); %this is where and what your image will be saved
imwrite(img, savename);

%pause
pause(4);

%moving stepper motor 2 degrees(23 steps approximately)
for k = 1:23

    step = steps{mod(k,steps_size)+1};      % step=1,2,3,4,5,6,7,8

    for j=1:4
        writeDigitalPin(a, pins{j}, str2double(step(j)));
    end
end

counter = counter +1;  %counter for images should increment each time you push the button
end

%pause so as to switch off the LED light and grab a background image
clear('cam');

Hi, I'm Brazilian.
Did you get a speed boost?