Stepper Motor Control - Two Buttons - Help?

Good Afternoon All Again,

Ok so i'm in a pickle again im having a little trouble with the following:

My aim from this is to be able to control a stepper motor with a gearbox under the use of two buttons so I can have control of it going left and right.

The equipment I am using,

Nanotec Stepper Motor
Stepper Motor Driver

any and all help would be great.

  // testing a stepper motor with a Pololu A4988 driver board or equivalent

// this version uses millis() to manage timing rather than delay()
// and the movement is determined by a pair of momentary push switches
// press one and it turns CW, press the other and it turns CCW

byte directionPin = 9;
byte stepPin = 8;

byte buttonCWpin = 10;
byte buttonCCWpin = 11;

boolean buttonCWpressed = false;
boolean buttonCCWpressed = false;

byte ledPin = 13;

unsigned long curMillis;
unsigned long prevStepMillis = 0;
unsigned long millisBetweenSteps = 25; // milliseconds

void setup() {

     Serial.begin(9600);
     Serial.println("Starting Stepper Demo with millis()");

     pinMode(directionPin, OUTPUT);
     pinMode(stepPin, OUTPUT);
     pinMode(ledPin, OUTPUT);
     
     pinMode(buttonCWpin, INPUT_PULLUP);
     pinMode(buttonCCWpin, INPUT_PULLUP);
     
}

void loop() {
   
    curMillis = millis();
    readButtons();
    actOnButtons();
   
}

void readButtons() {
   
    buttonCCWpressed = false;
    buttonCWpressed = false;
   
    if (digitalRead(buttonCWpin) == LOW) {
        buttonCWpressed = true;
    }
    if (digitalRead(buttonCCWpin) == LOW) {
        buttonCCWpressed = true;
    }
}

void actOnButtons() {
    if (buttonCWpressed == true) {
        digitalWrite(directionPin, LOW);
        singleStep();
    }
    if (buttonCCWpressed == true) {
        digitalWrite(directionPin, HIGH);
        singleStep();
    }
}

void singleStep() {
    if (curMillis - prevStepMillis >= millisBetweenSteps) {
            // next 2 lines changed 28 Nov 2018
        //prevStepMillis += millisBetweenSteps;
        prevStepMillis = curMillis;
        digitalWrite(stepPin, HIGH);
        digitalWrite(stepPin, LOW);
    }
}

The motor coil current is 9.5A. The driver is 5A max.

Can you tell s what the problem is?

Thanks for highlighting that one, would this be a better alternative,

nanotec N5

AaronClark_1989:
Thanks for highlighting that one, would this be a better alternative,

Your link seems to refer to two drivers - one with a 10A rating and one with 18A. IMHO the 10A version would not have enough margin over the 9.5amps of the motor. However as motor and driver are made by the same company I suggest you ask their advice.

...R

Thank you Robin,

Further into this part now and we have a motor controller and PSU that will be able to supply what we need to the Stepper Motor.

Now my issue is as follows, I believe I have a code that will work with a normal stepper motor with the use of two buttons but I have a gearbox on the top and believe im not pumping out enough revolutions to get it to turn quickly enough at the touch of a button.

  // testing a stepper motor with a Pololu A4988 driver board or equivalent

// this version uses millis() to manage timing rather than delay()
// and the movement is determined by a pair of momentary push switches
// press one and it turns CW, press the other and it turns CCW

byte directionPin = 9;
byte stepPin = 8;

byte buttonCWpin = 10;
byte buttonCCWpin = 11;

boolean buttonCWpressed = false;
boolean buttonCCWpressed = false;

byte ledPin = 13;

unsigned long curMillis;
unsigned long prevStepMillis = 0;
unsigned long millisBetweenSteps = 100; // milliseconds

void setup() {

     Serial.begin(3600);
     Serial.println("Starting Stepper Demo with millis()");

     pinMode(directionPin, OUTPUT);
     pinMode(stepPin, OUTPUT);
     pinMode(ledPin, OUTPUT);
     
     pinMode(buttonCWpin, INPUT_PULLUP);
     pinMode(buttonCCWpin, INPUT_PULLUP);
     
}

void loop() {
   
    curMillis = millis();
    readButtons();
    actOnButtons();
   
}

void readButtons() {
   
    buttonCCWpressed = false;
    buttonCWpressed = false;
   
    if (digitalRead(buttonCWpin) == LOW) {
        buttonCWpressed = true;
    }
    if (digitalRead(buttonCCWpin) == LOW) {
        buttonCCWpressed = true;
    }
}

void actOnButtons() {
    if (buttonCWpressed == true) {
        digitalWrite(directionPin, LOW);
        singleStep();
    }
    if (buttonCCWpressed == true) {
        digitalWrite(directionPin, HIGH);
        singleStep();
    }
}

void singleStep() {
    if (curMillis - prevStepMillis >= millisBetweenSteps) {
            // next 2 lines changed 28 Nov 2018
        //prevStepMillis += millisBetweenSteps;
        prevStepMillis = curMillis;
        digitalWrite(stepPin, HIGH);
        digitalWrite(stepPin, LOW);
    }
}

If anyone can take a look at this it would be great.

Many Thanks in Advance

Aaron

unsigned long millisBetweenSteps = 100;

Reducing the number in that line will speed up the motor. If the number gets too small the motor may start to skip steps because it cannot speed up from 0 to a high speed. If that occurs then you will need to accelerate the motor to speed. The AccelStepper library enables acceleration or you can write your own acceleration code. Robin2 has a post that shows how to write your own acceleration code, but I would have to find it. Will post a link if I do or hope that he sees this post and can link to it.

This is the link that @groundFungus referred to.

Simple acceleration code.

...R

Robin2:
This is the link that @groundFungus referred to.

Simple acceleration code.

...R

Good Morning Robin,

Thank you for linking me to the code:

Would it be possible to alter the code to be used with to push buttons so it can give you control of both directions I know this is asking for a bit.

Any and more help would be great.

AaronClark_1989:
Would it be possible to alter the code to be used with to push buttons so it can give you control of both directions

Yes

I know this is asking for a bit.

I don't propose to write the code for you but I will try to help if you get stuck. Post the program that represents your best attempt and tell us in detail what it actually does and what you want it to do that is different.

If you just want someone to write a program for you please ask in the Gigs and Collaborations section of the Forum and be prepared to pay. (I don't participate there).

...R

Robin2:
Yes
I don’t propose to write the code for you but I will try to help if you get stuck. Post the program that represents your best attempt and tell us in detail what it actually does and what you want it to do that is different.

If you just want someone to write a program for you please ask in the Gigs and Collaborations section of the Forum and be prepared to pay. (I don’t participate there).

…R

Thank you,

I will keep you posted, and update to see where I get to on writing this code.

Looking into the code that you have done already I believe that I have it set up correctly, But i have this stepper motor to test: 17HS19-1684S-PG100 https://www.oyostepper.com/images/upload/File/17HS19-1684S-PG100.pdf and i cant think for the life og me what I could be doing wrong as this is not moving from what I can tell in the slightest.

// testing a stepper motor with a Pololu A4988 driver board or equivalent

// this version uses micros() to manage timing to allow high step rates to be tested
// and illustrates a simple method for accleration and deceleration

byte directionPin = 9;
byte stepPin = 8;

unsigned long curMicros;
unsigned long prevStepMicros = 0;
unsigned long slowMicrosBetweenSteps = 6000; // microseconds
unsigned long fastMicrosBetweenSteps = 1500;
unsigned long stepIntervalMicros;
unsigned long stepAdjustmentMicros;
int numAccelSteps = 100; // 100 is a half turn of a 200 step mmotor
int numSteps = 1000;
int stepsToGo;
byte direction = 1;

void setup() {

    Serial.begin(115200);
    Serial.println("Starting Stepper Demo with acceleration");

    pinMode(directionPin, OUTPUT);
    pinMode(stepPin, OUTPUT);

    stepAdjustmentMicros = (slowMicrosBetweenSteps - fastMicrosBetweenSteps) / numAccelSteps;
    stepIntervalMicros = slowMicrosBetweenSteps;
    stepsToGo = numSteps;
    digitalWrite(directionPin, direction);
}

void loop() {

    moveMotor();

}

void moveMotor() {
    if (stepsToGo > 0) {
        if (micros() - prevStepMicros >= stepIntervalMicros) {
            prevStepMicros += stepIntervalMicros;
            singleStep();
            stepsToGo --;
            if (stepsToGo <= numAccelSteps) {
                if (stepIntervalMicros < slowMicrosBetweenSteps) {
                    stepIntervalMicros += stepAdjustmentMicros;
                }
            }
            else {
                if (stepIntervalMicros > fastMicrosBetweenSteps) {
                    stepIntervalMicros -= stepAdjustmentMicros;
                }
            }
        }
    }
    else {
        direction = ! direction;
        digitalWrite(directionPin, direction);
            // next two lines just for the demo
        delay(2000);
        Serial.println("Changing direction");
        stepsToGo = numSteps;
        prevStepMicros = micros();
    }
}

void singleStep() {

    digitalWrite(stepPin, HIGH);
    digitalWrite(stepPin, LOW);

}

AaronClark_1989:
and i cant think for the life og me what I could be doing wrong as this is not moving from what I can tell in the slightest.

Make a simple and clearly labelled pencil drawing (NOT Fritzing, please) showing exactly how you have everything connected and post a photo of the drawing. See this Simple Image Posting Guide

Does the motor work at slow speed with my simple non-accelerating code. If not, then start with that. Keep things as simple as possible when debugging.

...R

Please find my pencil drawing below:

Pencil diagram

At present nothing is appear to be working as i have a stepper with a gearbox attached I can tell something is working as I can see the lights on the UNO flashing.

The wiring to the DIR+/DIR- connections should be a twisted pair - you've drawn them separate and coming from two different boards.

Ditto for PUL+/PUL-.

Each signal wire needs to run along with its return wire or you'll risk picking up interference - with high current supplies and motor controllers there's a lot of interference waiting to be picked up.

With a multimeter in voltage setting you can snoop on a motor winding - if its stepping slowly the displayed
value will be changing and reversing regularly. If its not stepping it won't change much nor reverse.

You can use a meter on the DIR signal lines to check they are getting driven. Check the polarity. The PUL lines have very short pulses that a multimeter cannot see though.

What microstepping ratio have you set up? You should check all the switch selections are what you want.

The DM860T has EN+ and EN- inputs - you may need to drive these to enable it (or not - the manual ought to say). Its another opto isolated input pair.

AaronClark_1989:
Please find my pencil drawing below:

Please make the image visible in your Post. I gave you a link to the instructions.

...R

Robin2:
Please make the image visible in your Post. I gave you a link to the instructions.

...R

There we go image is now visible

As @MarkT has said, you may need to set the ENABLE pins.

You could probably make the wiring simpler by connecting (eg) the PUL- to GND and the PUL+ to the Arduino I/O pin

Be VERY CAREFUL never to disconnect the wires between the motor and the stepper driver while the driver is powered up. The driver will be instantly destroyed.

...R

PS ... for the future don't show a breadboard in a diagram as it is difficult to know what goes on inside it.