Super Simple Arduino Code for Stepper Driver


#1

Hi Everyone, I have made my own code for driving a Stepper Motor with the ‘2H Stepper Driver’.
If you would like to try it out for yourself just copy & paste my code into the Arduino IDE.

//really basic code for the ‘MakeBlock 2H Microstep Driver’

//step controller
int dirPin = 2;
int pulPin = 3;
int enblPin = 4;

void setup(){

pinMode(dirPin, OUTPUT);
pinMode(pulPin, OUTPUT);
pinMode(enblPin, OUTPUT);
digitalWrite(enblPin, HIGH);

}

void loop(){

slide(‘L’, 500, 500); //go Left for 500 steps at speed 500, (the lower the number is the faster the motor will go)
delay(2000);

slide(‘R’, 500, 500); //go Right for 500 steps at speed 500
delay(2000);

}

void slide(int dir, int steps, int sspeed){

if (sspeed < 50) sspeed = 10; //keeps the speed above 10

if (dir == ‘L’) digitalWrite(dirPin, HIGH);
else if (dir == ‘R’) digitalWrite(dirPin, LOW);

while (steps > 0){
digitalWrite(pulPin, HIGH);
delayMicroseconds(10);
digitalWrite(pulPin, LOW);
delayMicroseconds(10);
delayMicroseconds(sspeed);
steps–;
}
}


#2

I think the code is feasible except a an error . The ‘digitalWrite(enblPin, OUTPUT)’ should probably change to ‘digitalWrite(enblPin, LOW)’.


#3

Well spotted. I did test the code before sticking it on here and it worked fine. I think the Arduino IDE reads ‘OUTPUT’ as ‘HIGH’ which is what it should be.
I have edited my original post to include this correction.


#4

Hello guys,

I am trying to use this code with three stepper motors followed by each other for a warehouse unloading process.
int dirPin1 = 4;
int pulPin1 = 5;
int dirPin2 = 13;
int pulPin2 = 14;
int enblPin = 10;
int noofloops=1;

void setup(){

pinMode(dirPin1, OUTPUT);
pinMode(pulPin1, OUTPUT);
pinMode(dirPin2, OUTPUT);
pinMode(pulPin2, OUTPUT);
pinMode(enblPin, OUTPUT);
digitalWrite(enblPin, HIGH);

}

void loop(){
if(noofloops==1)
{
slide(‘L1’, 500, 500); //go Left for 500 steps at speed 500, (the lower the number is the faster the motor will go)
delay(2000);
noofloops=0;
}
else if (noofloops==0)
{
slide(‘L2’, 500, 500); //go Left for 500 steps at speed 500, (the lower the number is the faster the motor will go)
delay(2000);
noofloops=2;
}
}

void slide(int dir, int steps, int sspeed){

if (sspeed < 50) sspeed = 10; //keeps the speed above 10

if (dir == ‘L1’) digitalWrite(dirPin1, HIGH);
if (dir == ‘L2’) digitalWrite(dirPin2, HIGH);

while (steps > 0){
digitalWrite(pulPin1, HIGH);
delayMicroseconds(10);
digitalWrite(pulPin1, LOW);
delayMicroseconds(10);
delayMicroseconds(sspeed);
steps–;
}
}

This is the code so far but when it comes to the else if part with noofloops == 0 we defined to use the L2 which is linked to
if (dir == ‘L2’) digitalWrite(dirPin2, HIGH);

But somehow it keeps working on the same engine and doesnt want to move the second stepper motor which is plugged in at 13.14…

Thanks alot for the help!


#5

Hi @Ehsan,

I’m not sure about the problem and I have transfer it to our engineer. It seems like that you have just define two stepper motors in the code instead of 3?


#6

Hey. I dont mind having a look for you later today/this week. Could you possibly take a photo of the wiring ro explain how you have wired the two motors and controllers?


#7

Hi @dalbyman, you can visit the Instructables, step 17 to know how to connect the cable with two stepper motors and controllers.

And for more information about stepper motor, you can visit here http://forum.makeblock.cc/t/the-user-guide-and-troubleshooting-of-stepper-motor-and-stepper-motor-driver/453/10


#8

Thanks Johnny, but I was asking how @Ehsan wired it so I could help him with the code - just in case it was wired incorrectly.


#9

Ha, ok, I misunderstand here.


#10