writeDigitalPin of Matlab is too slow for stepper control

Hi everyone,

I am trying to build a peristaltic pump with the following equipment;

E1) Hy-DIV268N-5A motor driver.

E2) JK42HS40-1304F stepper motor.

E3) Arduino Uno

I would like to be able to control the arduino using Matlab, because i have other devices which i can control using matlab and i need to sync the control of the pump with other devices, and the easiest way to this is controlling everything by the same program (ie. Matlab). One other thing i need is, i need to send some user input to arduino to change the speed of the stepper.

Requirements:
R1) Matlab controllable
R2) User input should be sent to arduino
R3) I do not want to add anything else to the above list equipment
R4) I should be able to send a pulse to the motor driver with a period of 2ms (1ms high, 1ms low)

Matlab and Simulink have both different toolboxes for controlling arduino. I tried to control the stepper with writeDigitalPin function of the Matlab toolbox but it is too slow. Can't go beyond around 30-40 ms per step. Simulink is much faster(satisfied R1,R3, and R4 but not R2) but i do not know how to use simulink to get user input.

Thus i tried the Arduino 1.6.8 and managed to control the arduino using serial monitor function. So this satisfied R2,R3 and R4 but not R1.

Is there a way to satisfy all the requirements listed above by using nothing else but E1, E2, and E3? thanks in advance...

You need to handle the motor driving locally on the Arduino - any external control program should
be sending high-level commands, not toggling pins. Does Matlab allow you to pass data to an
Arduino function that you write?

As @MarkT has said you need to arrange for Matlab to send a message telling the Arduino how many steps to do and at what speed. Then let the Arduino work out the details.

...R
Stepper Motor Basics
Simple Stepper Code
Serial Input Basics

I want to control step motors also with matlab but I have been unable to do it.

Can someone explain me please how can I achieve that?

What do I have: Stepper Motor Wantai WT57STH115-4204A with 4.2A(U) and 1.8deg/step --> 200 steps Driver HY-DIV268N-5A Arduino Mega2560

Step motor is connected with the driver The driver is connected to a power suply and to arduino. In the driver the Dir- is conected to the Pul-

Connection between driver and arduino is:

Dir+ connected to arduino port 8
Pul+ connected to arduino port 9
Pul- connected to arduino port 11

Armindo:
Connection between driver and arduino is:

Dir+ connected to arduino port 8
Pul+ connected to arduino port 9
Pul- connected to arduino port 11

You need to post a link to the datasheet for your stepper driver. My guess is that Pul- should be connected to Arduino GND.

You have not told us anything about what you want the Matlab program to get the stepper motor to do.

Have you carefully studied all the other Replies in this Thread?

...R

Hi Robin2

Thank you for the help

The datasheet can be found here.
http://wiki.kreitek.org/_media/proyectos:cnc:div268n-5a-datasheet.pdf

Note: As it is (the ports) I can put the step motor working with the arduino IDE, but not with matlab.

3 Step motors (i.e. 3 independent columns) - Each one can make movements up/down independently what is important is to control de position.

In the 1 column I will have another step motor doing horizontal movement. In this case both step motor sometimes will have to move to a position at the same time others only 1 at once.

The 2 column I will move up/down independently and will have another step motor working as a spindle (is not like a real spindle e just to move at slow velocity).

The 3 column will move just up/down independently.

I will have also a hot air gun. Afterngoes ON for a wile it should go OFF then the position of the 3 column should change and a cylinder like the one below should go ON.

Note: will be possible to use instead of software just switches where each switch position (e.g. 1, 2, 3) will correspond to a step motor position (x, y, z) and other switch will increase and decrease the step motor velocity?

Armindo:
The datasheet can be found here.

The datasheet shows all the - connections going to GND

The rest of your Reply makes no sense to me - probably because I know nothing about Matlab.

What I was hoping you would do is provide an example of the data you expect Matlab to send to the Arduino. It won't matter to the Arduino how Matlab (or any other program) creates the data.

...R

Can I make a code in arduino IDE that is loaded into the arduino just one time, and after that I can use to operate the system just switchs that change steper motors position ans velocity?

If so can you provide me an exemple about how to perform that?

Armindo:
Can I make a code in arduino IDE that is loaded into the arduino just one time,

When you load a program onto an Arduino it stays there until you overwrite it by uploading something else.

and after that I can use to operate the system just switchs that change steper motors position ans velocity?

I'm sorry, but I don't understand that. Are you thinking of abandoning Matlab and using physical switches to control the Arduino? If so, what type of switches and what do you want the switches to do?

If so can you provide me an exemple about how to perform that?

No. We will try to help with your programs.

if you want someone to write a program then ask in the Gigs and Collaborations section and be prepared to pay.

...R

I'm sorry, but I don't understand that. Are you thinking of abandoning Matlab and using physical switches to control the Arduino? If so, what type of switches and what do you want the switches to do?

Probably because no one is able to clarify how to to this in matlab the only things that I can see are related with the use of adfruit. The only information regarding arduino and matlab with the kind of driver that I have is about people asking for help but no answers....

My initial idea was to use some switch but a friend told me that would not be a good idea why i dont know.

In relation to Switch i basically whanted that:

Switch in pos1 = step motor go to position X
Switch in pos2 = step motor go to position Y
Switch in pos2 = step motor go to position Z
and so on.

Then another Switch would control the velocity:
Switch in pos1 = step motor go to velocity X
Switch in pos2 = step motor go to velocity Y
Switch in pos2 = step motor go to velocity Z
and so on.

What switchs to use I have no idea (ideally with 6 or more positions) but would be a good to have your opinion about that.

This can be done with arduino?

Armindo:
What switchs to use I have no idea (ideally with 6 or more positions) but would be a good to have your opinion about that.

This can be done with arduino?

Twelve position rotary switches are cheap and common.

And, yes, you could program an Arduino to respond to switches as you suggest.

However it is a very inflexible system. Why not use Matlab (or any other PC programming system) to send (say) two numbers to the Arduino <1234, 67> meaning the number of steps and the speed?

...R

Since I didnt see any matlab code that worked with arduino (without adfruit) here it is an example: Note however that the step motor will rotate very slowly I dont believe that would be possible to change that using matlab.

The Pins and drive were defined as idicated in the firts post of this topic.

DirectionPin  = 'D8';
SteperPinPlus = 'D9';
NumberSteps   = 200;


ArduinoObj = arduino('com3','Mega2560');    % get the arduino object
configurePin (ArduinoObj,'D8','DigitalOutput');
configurePin (ArduinoObj,'D9','DigitalOutput');


writeDigitalPin(ArduinoObj,DirectionPin,1);
for ii = 0 : NumberSteps
   writeDigitalPin(ArduinoObj,SteperPinPlus,1);
   pause(0.001);
   writeDigitalPin(ArduinoObj,SteperPinPlus,0);
end

pause(0.001);

writeDigitalPin(ArduinoObj,DirectionPin,0);
for nn = 0 : NumberSteps
   writeDigitalPin(ArduinoObj,SteperPinPlus,1);
   pause(0.001)
   writeDigitalPin(ArduinoObj,SteperPinPlus,0);
end

For the ones that might be interested in using matlab they may also be interested in this matlab library: ArdLAB and to look into the Grbl firmware to be used with matlab.