How to drive stepper motor using push buttons with driver DM860A

Hello.I building machine and i have problems with push buttons.

I need code for two push buttons

1 to move stepper motor forward and second wan to move backward.
and motor mast keep moving until release buttons.

or code for joystick.

I have Arduino UNO
Stepper motor driver DM860A
Nema 34 stepper motor 878oz.
Power Supply 200Watts-60VDC

I have done many codes to move automatically stepper and it work perfectly
i cannot get this work with push buttons or joystick

Sorry for my English and Thanks

Post your code so we can see where you hve got to.

Post a link to the datasheet for your stepper driver.

Maybe have a look at the 2nd example in this Simple Stepper Code

...R

Thank you Robin2 for reply.

Second wan code working but make only single step.
I have arduino 1.0 ver. ON LINUX and i changed INPUT_PULLUP to INPUT.
How can i change speed.
And why if i pres push button and wait about 10 sec arduino reset.

byte buttonCWpin = 10; is connected on 1 push button
byte buttonCCWpin = 11; is connected on second wan push button

byte directionPin = 9; is connected on DIR+5v motor driver DM860A
byte stepPin = 8; is connected on PULL+5v motor driver DM860A

Ahmo:
Second wan code working

I don't understand.

Please post the complete program you are actually using.

...R

// 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) {
prevStepMillis += millisBetweenSteps;
digitalWrite(stepPin, HIGH);
digitalWrite(stepPin, LOW);
}
}

Ahmo:
I have arduino 1.0 ver. ON LINUX and i changed INPUT_PULLUP to INPUT.

It may not work properly with INPUT unless you use an external pullup resistor. Why not use a newer version of the IDE. There is no problem having multiple versions on your PC if you need V1.0 for another project

How can i change speed.

The speed is controlled by the variable millisBetweenSteps

And why if i pres push button and wait about 10 sec arduino reset.

That makes me wonder if you are trying to power the motor from your Arduino - don't.

Make a pencil drawing showing how everything is connected and post a photo of the drawing.

Please modify your Reply #4 and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum

...R

Ok.I have 1.6 ver. of Arduino software.

And this is code

// 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) {
 prevStepMillis += millisBetweenSteps;
 digitalWrite(stepPin, HIGH);
 digitalWrite(stepPin, LOW);
 }
}

motor nema 34 and power supply are fine.Working properly with another codes and projects what i have.

Hello ROBIN :slight_smile:

I fixed the problem.
it is wiring and resistors.

Code is perfect only a dont know how to turn motor with full speed.
if i change unsigned long millisBetweenSteps = 25 or 10 or 250 is to slow

How many steps per second would be needed for full speed?

...R

I dont know :frowning:

how can i check?

I have Nema 34 stepper motor ,878oz. in,bipolar

and motor driver DM860A

Ahmo:
I dont know :frowning:

That does not make sense. How can you say it is not reaching full speed if you don't know what full speed is?

You probably need to accelerate the motor to get very high speeds. It is unlikely to be able to straight from stationary to max speed.

...R

Don't be mad on me :slight_smile:

i am new in this stuff

i using this code for first machine as you can delayMicroseconds(950);

for second machine i need similar speed with pus buttons.

int LASKE =      1580;
int NOSTA =      1580;
int EkaUros =    1580; 
int ToinenUros = 450;
int Valmis = 2038    ;  // uvijekdodati 6 na sabranu cifru Ekauros+ToinenUros

void setup()
{
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  
}

void loop()

  {
  
  
  
  digitalWrite(2, LOW);
  for(int n = 0; n <  LASKE; n++) {     // DOLE
  digitalWrite(3, HIGH);
  delayMicroseconds(900);
  digitalWrite(3, LOW);
  
}


  delay(3000);
  digitalWrite(2, LOW);
  for(int n = 0; n <  LASKE; n++) {   // CEKAJ
  digitalWrite(3, LOW);
  digitalWrite(3, LOW);
}  
  
  digitalWrite(2, HIGH);
  for(int n = 0; n <NOSTA; n++) {     // GORE
  digitalWrite(3, LOW);
  delayMicroseconds(900);
  digitalWrite(3, HIGH); 
}  
  delay(3000);
  digitalWrite(2, LOW);
  for(int n = 0; n <  LASKE; n++) {   // CEKAJ
  digitalWrite(3, LOW);
  digitalWrite(3, LOW);  
} 
//////////////////////////////////////////////////////////////////////////////////  
   
  digitalWrite(2, LOW);
  for(int n = 0; n <  LASKE; n++) {     // DOLE
  digitalWrite(3, HIGH);
  delayMicroseconds(900);
  digitalWrite(3, LOW);
  
}


  delay(3000);
  digitalWrite(2, LOW);
  for(int n = 0; n <  LASKE; n++) {   // CEKAJ
  digitalWrite(3, LOW);
  digitalWrite(3, LOW);
}  
  
  digitalWrite(2, HIGH);
  for(int n = 0; n <NOSTA; n++) {     // GORE
  digitalWrite(3, LOW);
  delayMicroseconds(900);
  digitalWrite(3, HIGH); 
}  
  delay(5000);
  digitalWrite(2, LOW);
  for(int n = 0; n <  LASKE; n++) {   // CEKAJ
  digitalWrite(3, LOW);
  digitalWrite(3, LOW);  

}
////////////////////////////////////////////////////////////////////////////////  
  
  digitalWrite(2, LOW);
  for(int n = 0; n <  LASKE; n++) {     // DOLE
  digitalWrite(3, HIGH);
  delayMicroseconds(900);
  digitalWrite(3, LOW);
  
}
  delay(3000);
  digitalWrite(2, LOW);
  for(int n = 0; n <  LASKE; n++) {   // CEKAJ
  digitalWrite(3, LOW);
  digitalWrite(3, LOW);
}  
  
  digitalWrite(2, HIGH);
  for(int n = 0; n <NOSTA; n++) {     // GORE
  digitalWrite(3, LOW);
  delayMicroseconds(900);
  digitalWrite(3, HIGH); 
}   
  delay(3000);
  digitalWrite(2, LOW);
  for(int n = 0; n <  LASKE; n++) {   // CEKAJ
  digitalWrite(3, LOW);
  digitalWrite(3, LOW);  

} 
 //////////////////////////////////////////////////////////////////////////// 
   
  digitalWrite(2, LOW);
  for(int n = 0; n <  EkaUros; n++) {    // PRVI UROS DOL
  digitalWrite(3, HIGH);
  delayMicroseconds(950);
  digitalWrite(3, LOW);
  
}
  
  delay(2500);
  digitalWrite(2, LOW);
  for(int n = 0; n <  LASKE; n++) {     // CEKAJ PRVI UROS
  digitalWrite(3, LOW);
  digitalWrite(3, LOW);
} 
 
 digitalWrite(2, LOW);
  for(int n = 0; n <  ToinenUros; n++) { // DRUGI UROS DOL
  digitalWrite(3, HIGH);
  delayMicroseconds(950);
  digitalWrite(3, LOW);
  
}
  delay(2500);
  digitalWrite(2, LOW);
  for(int n = 0; n <  LASKE; n++) {    //  CEKAJ DRUGI UROS
  digitalWrite(3, LOW);
  digitalWrite(3, LOW);
 
 }   
 /////////////////////////////////////////////////////////////////////////// 
 
  
  digitalWrite(2, HIGH);
  for(int n = 0; n <Valmis; n++) {    //  ZAVRSIO
  digitalWrite(3, LOW);
  delayMicroseconds(950);
  digitalWrite(3, HIGH); 

 } 
 
  delay(10000);
  digitalWrite(2, LOW);
  for(int n = 0; n <  LASKE; n++) {  //   CEKAJ ZA  PONAVLJANJE
  digitalWrite(3, LOW);
  digitalWrite(3, LOW);
} 
 }

hire is video on youtube

i need this

THANK YOU FOR HELPING ME :grin:

Are you saying that the first program works with delayMicroseconds(950); ?

If so, why not change the second program so that millisBetweenSteps = 1 as that will be very nearly the same.

If that works you can modify the second program to use microsBetweenSteps if you need to.

...R

Hello Robin2.Thank you so much

I modified push buttons code and it works :wink: :wink: :wink: :wink: :wink:

// 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 curMicros;
unsigned long prevStepMicros = 0;
unsigned long microsBetweenSteps = 950; // microseconds

void setup() {

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

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

pinMode(buttonCWpin, INPUT_PULLUP);
pinMode(buttonCCWpin, INPUT_PULLUP);

}

void loop() {

curMicros = micros();
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 (curMicros - prevStepMicros >= microsBetweenSteps) {
prevStepMicros += microsBetweenSteps;
digitalWrite(stepPin, HIGH);
digitalWrite(stepPin, LOW);
}
}

Glad it works. Thanks for the feedback.

...R

Hello.

I need help with push buttons.
i need to control stepper motor nema 34.

is it possible control stepper motor with 1 push button.
4 different position.

example:

  1. push button motor moves cw using delayMicroseconds with #define DISTANCE 1200

  2. push button motor moves ccw using delayMicroseconds with #define DISTANCE 1800

3.push button motor moves cw using delayMicroseconds with #define DISTANCE 2000

  1. push button motor moves ccw using delayMicroseconds with #defne DISTANCE 1000

if is not possible

then maybe 4 push buttons can do a job

sorry for my english

This is code what i have and it works.Thanks to Robin2
2 push buttons clockwise and counterclockwise

byte directionPin = 9;
byte stepPin = 8;

byte buttonCWpin = 10;
byte buttonCCWpin = 11;

boolean buttonCWpressed = false;
boolean buttonCCWpressed = false;

byte ledPin = 13;

unsigned long curMicros;
unsigned long prevStepMicros = 0;
unsigned long microsBetweenSteps = 950; // microseconds

void setup() {

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

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

void loop() {
 
 curMicros = micros();
 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 (curMicros - prevStepMicros >= microsBetweenSteps) {
 prevStepMicros += microsBetweenSteps;
 digitalWrite(stepPin, HIGH);
 digitalWrite(stepPin, LOW);
 }
}

Ahmo:
is it possible control stepper motor with 1 push button.
4 different position.

example:

  1. push button motor moves cw using delayMicroseconds with #define DISTANCE 1200

  2. push button motor moves ccw using delayMicroseconds with #define DISTANCE 1800

3.push button motor moves cw using delayMicroseconds with #define DISTANCE 2000

  1. push button motor moves ccw using delayMicroseconds with #defne DISTANCE 1000

For that sort of thing you need a different approach for the button code.

You need a variable that holds a value (say 0 to 4). The first push changes it to 1. The second push changes it to 2. The 5th push will change it from 4 back to 0. (Not sure if you need 0 to do nothing).

Then your stepper code can check the value in the variable to decide what it should do.

...R

Can you make for me 1 example code?

Please