Nema 23 with DQ542MA Stepper Driver

Hello! i am doing a project using nema 23 and dq542ma stepper driver. I made it to work but i want it to stop after 1 full revolution. Here’s my code:

int stepPin = 5;
int dirPin = 6;
int enblPin = 7;
int Distance = 0;

void setup() {
pinMode (stepPin, OUTPUT);
pinMode (dirPin, OUTPUT);
pinMode (enblPin, OUTPUT);
digitalWrite(stepPin, LOW);
digitalWrite(dirPin, LOW);
digitalWrite(enblPin, HIGH);
}

void loop() {
for(int x=0; x<3200; x++){
digitalWrite(stepPin, HIGH);
delayMicroseconds(100);
digitalWrite(stepPin, LOW);
delayMicroseconds(100);
 Distance = Distance + 1;
}

if (Distance == 3200)
{
if (digitalRead(dirPin) == LOW)
{
digitalWrite(dirPin, HIGH);
}
else
{
digitalWrite(dirPin, LOW);
}
Distance = 1600;
delay(2000);
}
}

The gear only moves like 1/4 rotation clockwise and then turns back to its origin counterclockwise. I need it to make 1 full revolution. any help in the code is very much appreciated.

What very odd code.
How many steps do you need for one revoloution?

The distance variable is useless, after the first pass it will never be == to 3200 ever again. This is because you never reset it to zero and when it gets past the first for loop in the code it will have increased to 1600 + 3200.

Just write a loop to give you the number of steps you need and then toggle the direction line when it ends. No need to test for anything after that first loop.

Grumpy_Mike:
What very odd code.
How many steps do you need for one revoloution?

The distance variable is useless, after the first pass it will never be == to 3200 ever again. This is because you never reset it to zero and when it gets past the first for loop in the code it will have increased to 1600 + 3200.

Just write a loop to give you the number of steps you need and then toggle the direction line when it ends. No need to test for anything after that first loop.

how to make it like rotate 1 full plus half? i changed something in my code

void loop() {
for(int x=0; x<27200; x++){
digitalWrite(stepPin, HIGH);
delayMicroseconds(100);
digitalWrite(stepPin, LOW);
delayMicroseconds(100);
Distance = Distance + 1;
}

if (Distance == 27200)
{
if (digitalRead(dirPin) == LOW)
{
digitalWrite(dirPin, HIGH);
}
else
{
digitalWrite(dirPin, LOW);
}
Distance = 0;
delay(2000);
}

the one full revolution is 25600. pls help thanks :smiley:

mikaeehl:
how to make it like rotate 1 full plus half? i changed something in my code

t would help if you tell us what you changed.

Did you try your revised program? What did it do? It is much easier to give useful advice when you tell us the result of your tests.

...R

Robin2:
t would help if you tell us what you changed.

Did you try your revised program? What did it do? It is much easier to give useful advice when you tell us the result of your tests.

…R

hello! i changed the part

for(int x=0; x<3200; x++){ — 3200 to 25600
digitalWrite(stepPin, HIGH);
delayMicroseconds(100);
digitalWrite(stepPin, LOW);
delayMicroseconds(100);
Distance = Distance + 1;
}

if (Distance == 3200) — 3200 to 25600

this create a 1 full revolution clockwise and after 2 secods, counter clockwise. i want it to be 1 full and a half. but i dont know how to do so. thanks!

Any help? Pls this is urgent :frowning: thank you!

mikaeehl:
this create a 1 full revolution clockwise and after 2 secods, counter clockwise. i want it to be 1 full and a half. but i dont know how to do so. thanks!

Well it seems that 25600 steps makes it go 1 revolution. How many steps do you think it might need for 1.5 revolutions? It should be 38400.

But in this line

for(int x=0; x<25600; x++){

the variable x is defined as an int and that means its max value is 32767. You need to change that line to use a long which can hold values up to 231-1

for(long  x = 0; x < 38400L ;  x++){

NOTE the L after the 38400 to make sure the compiler knows that you want a long value

…R

mikaeehl:
Any help? Pls this is urgent :frowning: thank you!

That's being very unreasonable. If about 48 hours go by without a reply then a reminder would be OK.

...R

Robin2:
That's being very unreasonable. If about 48 hours go by without a reply then a reminder would be OK.

...R

I am very sorry, sir. I am getting frustrated with this thing. Please forgive me for being in a hurry with answers. Thank you for you help. it is very appreciated.

I made it into 1.5 revolution. but i was wondering why is there a rotation like 1/2 or 1/4 in the start before it does the 1.5 revolution clockwise and counter clockwise.

Hello! so i got my motor working the way i want it. However, I have some value input for it to work. it is coming from my xbee. for example, i have a reading of 143 in my xbee, and there is some condition like if the reading is < 143, the motor will not work. if the reading is > 143, the motor will start working. Any idea how to do this? Thanks! and God Bless!

Don't split up your project. I have suggested to the Moderator to merge this with your other Thread so all the info is in one place.

You need to keep us up to date by posting the latest version of the code.

...R

Robin2:
Don’t split up your project. I have suggested to the Moderator to merge this with your other Thread so all the info is in one place.

You need to keep us up to date by posting the latest version of the code.

…R

thank you for guiding me here sir. im a newbie here. so here’s my latest code

#include <SoftwareSerial.h>

SoftwareSerial XBee(8, 9); // RX, TX


int stepPin = 5;
int dirPin = 6;
int enblPin = 7;
long Distance = 0;

void setup() {
  XBee.begin(9600);
  Serial.begin(9600);
pinMode (stepPin, OUTPUT);
pinMode (dirPin, OUTPUT);
pinMode (enblPin, OUTPUT);
digitalWrite(stepPin, LOW);
digitalWrite(dirPin, LOW);
digitalWrite(enblPin, HIGH);
}



void loop() {
  {
    if (XBee.available()) // If data comes in from XBee, send it out to serial monitor
    Serial.write(XBee.read());
    
  }
  
for(int x=0; x<6400; x++)
{
digitalWrite(stepPin, HIGH);
delayMicroseconds(100);
digitalWrite(stepPin, LOW);
delayMicroseconds(100);
Distance = Distance + 1;
}

if (Distance == 38400)
{ if 
  (digitalRead(dirPin) == LOW)
      { 
         digitalWrite(dirPin, HIGH);
      }

    else
      {
         digitalWrite(dirPin, LOW);
      }
           
  Distance = 0;
  delay(2000);
    }
}

i just changed the distance to 38400 and the x==3200 to 6400. I cant seem to remove the first rotation before the 1.5 revolution. Sometimes it does 1 full rev before doing the 1.5 rev. What may be the problem in the code? Thanks!

Anyone has an idea? I still cant get to remove the random rotation at the start before it does the 1.5 rev. Your help is very much appreciated. Thank you!

I did not see your Post on 12 Oct - sorry.

I don’t understand why you have this

for(int x=0; x<6400; x++)

wouldn’t it be much simpler like this ?

for(long x=0; x<38400; x++)

and then you would not need the variable called Distance

…R

Robin2:
I did not see your Post on 12 Oct - sorry.

I don’t understand why you have this

for(int x=0; x<6400; x++)

wouldn’t it be much simpler like this ?

for(long x=0; x<38400; x++)

and then you would not need the variable called Distance

…R

ok i changed it already :slight_smile: But still there is this random rotation at the beginning. I am getting a little frustrated here sir. i really cant seem to remove it. :frowning:

Robin2:
I did not see your Post on 12 Oct - sorry.

I don’t understand why you have this

for(int x=0; x<6400; x++)

wouldn’t it be much simpler like this ?

for(long x=0; x<38400; x++)

and then you would not need the variable called Distance

…R

Oh I got it now! the program is continuously running and when i turn on the supply, it starts where the program is left. that’s what makes the random revolution at the beginning! But how do i make it start where it should start, sir? I will be using a relay for the motor. Thanks!

it starts where the program is left.

No it dosn't, when you apply the power the code ALWAYS starts from the beginning.

that's what makes the random revolution at the beginning

If you think that then you are fooling yourself. You get the rotation at the beginning because that is how you wrote your code. As I have tried to tell you before but you ignore me.

mikaeehl:
I am getting a little frustrated here sir. i really cant seem to remove it. :frowning:

It's easy to remove. Once you remove your whole code.... then the motor won't behave like that anymore.

So then we can begin from the beginning.... and focus on methodical, systematic, properly thought-out code to do the job properly.

Start by writing a piece of code that turns the motor clockwise 1.5 revolutions. And once that is achieved.... copy the code to a new file .... and modify the code to make the motor rotate anticlockwise by 1.5 revolutions. Got to focus on this first.

Grumpy_Mike:
No it dosn't, when you apply the power the code ALWAYS starts from the beginning.
If you think that then you are fooling yourself. You get the rotation at the beginning because that is how you wrote your code. As I have tried to tell you before but you ignore me.

sorry sir if you think i ignored you. but i am using external supply to power my motor and the arduino is always powered. they do not share the same source.