Problems connecting DM542 with arduino and code

Hi to all ,my son have project to do and now he cant go forward , buttons on board and limit swithes dosen blink on led somthing is wrong with code ...
An he dosent know how to wire DM542 to arduino to working ok with code.

I have an Arduino Uno, a DM542 stepper driver and a nema 17 stepper motor.

He need the stepper motor to run in one direction until it hits a limit switch then stops. Then, when I press a start button it needs to begin running in the other direction until it hits another limit switch where it will stop. Then pressing the start button should change direction again and so on..

So the start button needs to change direction and start the motor while one of the limit switches is still being pressed.

Thanks in advance for any help.


```cpp

#include <ezButton.h>
#include <AccelStepper.h>

#define DIRECTION_CCW -1
#define DIRECTION_CW 1

#define STATE_GO 1
#define STATE_MOVING 2
#define STATE_STOP 3
#define STATE_STOPPED 4
#define home_switch 9
#define MAX_POSITION 0x7FFFFFFF

ezButton limitSwitch_1(A0);
ezButton limitSwitch_2(A1);
ezButton StartButton(A2);



AccelStepper stepper(1, 7, 6);

int stepperState = STATE_STOPPED;
int direction = DIRECTION_CW;
long targetPos = 0;
int move_finished = 1;     // Used to check if move is completed
long initial_homing = -1;  // Used to Home Stepper at startup

void setup() {
  Serial.begin(9600);

  limitSwitch_1.setDebounceTime(50);
  limitSwitch_2.setDebounceTime(50);
  StartButton.setDebounceTime(50);

  pinMode(home_switch, INPUT_PULLUP);

  delay(2);  // Wait for EasyDriver wake up

  // Set Max Speed and Acceleration of each Steppers at startup for homing
  stepper.setMaxSpeed(2000.0);      // Set Max Speed of Stepper (Slower to get better accuracy)
  stepper.setAcceleration(8000.0);  // Set Acceleration of Stepper


  // Start Homing procedure of Stepper Motor at startup

  Serial.print("Stepper is Homing . . . . . . . . . . . ");

  while (digitalRead(home_switch)) {  // Make the Stepper move CCW until the switch is activated
    stepper.moveTo(initial_homing);   // Set the position to move to
    initial_homing--;                 // Decrease by 1 for next move if needed
    stepper.run();                    // Start moving the stepper
    delay(2);
  }

  stepper.setCurrentPosition(0);    // Set the current position as zero for now
  stepper.setMaxSpeed(1800.0);      // Set Max Speed of Stepper (Slower to get better accuracy)
  stepper.setAcceleration(8000.0);  // Set Acceleration of Stepper
  initial_homing = 1;

  while (!digitalRead(home_switch)) {  // Make the Stepper move CW until the switch is deactivated
    stepper.moveTo(initial_homing);
    stepper.run();
    initial_homing++;
    delay(2);
  }

  stepper.setCurrentPosition(0);
  Serial.println("Homing Completed");
  Serial.println("");
  stepper.setMaxSpeed(1800.0);      // Set Max Speed of Stepper (Faster for regular movements)
  stepper.setAcceleration(8000.0);  // Set Acceleration of Stepper
}

void loop() {

  limitSwitch_1.loop();
  limitSwitch_2.loop();
  StartButton.loop();

  if (StartButton.isPressed()) {  // transition to the move state
    stepperState = STATE_GO;
    Serial.println(F("Going"));
  }

  if (limitSwitch_1.isPressed()) {
    stepperState = STATE_STOP;
    Serial.println(F("The limit switch 1: TOUCHED"));
  }

  if (limitSwitch_2.isPressed()) {
    stepperState = STATE_STOP;
    Serial.println(F("The limit switch 2: TOUCHED"));
  }

  switch (stepperState) {
    case STATE_STOP:
      Serial.println(F("Stopping."));
      stepper.stop();
      stepper.run();
      Serial.println(F("Changing Direction."));
      direction *= -1;
      Serial.println(F("The direction -> "));
      if (direction == DIRECTION_CW) {
        Serial.println(F("CLOCKWISE"));
      } else {
        Serial.println(F("COUNTER-CLOCKWISE"));
      }
      stepperState = STATE_STOPPED;
      break;
    case STATE_STOPPED:
      break;
    case STATE_GO:
      targetPos = direction * MAX_POSITION;
      Serial.println(F("Moving towards:"));
      Serial.println(F("Target Positon "));
      stepper.setCurrentPosition(0);
      stepper.moveTo(targetPos);
      stepperState = STATE_MOVING;
      break;
    case STATE_MOVING:
      stepper.moveTo(targetPos);
      break;
  }

  stepper.run();
}

Are your limit switches normally open or normally closed?

now they are both wired N/C, when they are presed are N/O

now they are both wired N/C, when they are presed are N/O

Then your code is wrong.
isPressed expects the switch to go from open to closed or wired as N/O
I would connect the limit switches to interrupt pins and not use ezbutton. You want the motor to immediatly stop when it hits the switch. Waiting a debounce time before you stop the motor may damage the switches or motor

i dont have any idea more , why the hack buttons wont work , motor spining ok , but problem is on buttons , arduino wont accept them in this code , and led alsow wont blink on arduino board

They need to be wires as normally open NOT normally closed.

led alsow wont blink on arduino board

Your code does not use an LED

Hi i give up with previsu code !
Is it posible to easy modificate this code to work with stepper nema 17 motor , now is write for dc motor and it works super .
If any can do this for me ill be super happy and if it needs ill pay!

const int Motor_ClockWiseBtn = A0;
const int Motor_AntiClockWiseBtn = A1;
const int Up_StopBtn = A2;
const int Down_StopBtn = A3;

const int IN1 = 3;
const int IN2 = 4;
const int IN3 = 5;
const int IN4 = 6;

void setup() {

  Serial.begin(9600);
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);
  pinMode(Motor_ClockWiseBtn, INPUT_PULLUP);
  pinMode(Motor_AntiClockWiseBtn, INPUT_PULLUP);
  pinMode(Up_StopBtn, INPUT_PULLUP);
  pinMode(Down_StopBtn, INPUT_PULLUP);
}

void loop() {

  if (digitalRead(Motor_ClockWiseBtn) == LOW) {

    digitalWrite(IN1, HIGH);
    digitalWrite(IN3, HIGH);
    digitalWrite(IN2, LOW);
    digitalWrite(IN4, LOW);
    Serial.println("Motor rotate Clock wise");
    delay(100);
  }

  if (digitalRead(Motor_AntiClockWiseBtn) == LOW) {

    digitalWrite(IN1, LOW);
    digitalWrite(IN3, LOW);
    digitalWrite(IN2, HIGH);
    digitalWrite(IN4, HIGH);
    Serial.println("Motor rotate Anti Clock wise");
    delay(100);
  }

  if (digitalRead(Up_StopBtn) == LOW) {

    digitalWrite(IN1, LOW);
    digitalWrite(IN3, LOW);
    Serial.println("Motor stops");
    delay(100);
  }

  if (digitalRead(Down_StopBtn) == LOW) {

    digitalWrite(IN2, LOW);
    digitalWrite(IN4, LOW);
    Serial.println("Motor Stop");
    delay(100);
  }
}

Try this

const int Motor_ClockWiseBtn = A0;
const int Motor_AntiClockWiseBtn = A1;
const int Up_StopBtn = A2;
const int Down_StopBtn = A3;

// Connect DIR+ to 3, PUL+ to 4
// DIR- and PUL- to GND
const int DIR = 3;
const int PUL = 4;

bool Go = false;
unsigned int Speed;

void setup()
{
  Serial.begin(9600);
  pinMode(DIR, OUTPUT);
  pinMode(PUL, OUTPUT);

  digitalWrite(DIR, LOW);
  digitalWrite(PUL, LOW);

  pinMode(Motor_ClockWiseBtn, INPUT_PULLUP);
  pinMode(Motor_AntiClockWiseBtn, INPUT_PULLUP);
  pinMode(Up_StopBtn, INPUT_PULLUP);
  pinMode(Down_StopBtn, INPUT_PULLUP);
  Speed = 2500; // Bigger number is slower
  delay(10);
}

void loop()
{
  if (digitalRead(Motor_ClockWiseBtn) == LOW)
  {
    Go = true;
    delay(100);
    digitalWrite(DIR, HIGH);
    delayMicroseconds(10);
    Serial.println("Motor rotate Clock wise");
  }

  if (digitalRead(Motor_AntiClockWiseBtn) == LOW)
  {
    Go = true;
    delay(100);
    digitalWrite(DIR, LOW);
    delayMicroseconds(10);
    Serial.println("Motor rotate Anti Clock wise");
  }

  if (digitalRead(Up_StopBtn) == LOW)
  {
    Go = false;
    Serial.println("Motor stops");
  }

  if (digitalRead(Down_StopBtn) == LOW)
  {
    Go = false;
    Serial.println("Motor Go");
  }


  // Make the motor move one step
  if (Go == true)
  {
    digitalWrite(PUL, HIGH);
    delayMicroseconds(25);
    digitalWrite(PUL, LOW);
    delayMicroseconds(Speed);
  }
}

Code is working
but problem is when one of two limit switch are pressed , with presing button wont start in other direction.
button needs to change direction and start the motor while one of the limit switches is still being pressed.

now working code like they are all 4 buttons

It works exactly the same way as it does for the DC motor and you said that was super.
So I'm confused as to what you actually want.

Jim-p
I apologize, it was my mistake.
I haven't tested before whether it also works when one of the limit switches is pressed, so you can activate it in the opposite direction

So everything OK?
There are no limit switches in that code just 4 pushbuttons.

no 2 buttons need to be replaced with 2 limit switch , and when is one limitswitch press i need with presing 1 button it need to go motor in oposite site

why is there a separate home_switch in addition to 2 limit switches?

So not like the dc motor code but like how you said in post #1

look this over

  • i see no reason to home if you have limit switches.
  • separate cases are avoid the need to provide a short delay to move off a limit switch to start moving
# include <ezButton.h>
# include <AccelStepper.h>

ezButton limitSwitchCw  (A0);
ezButton limitSwitchCcw (A1);
ezButton startButton    (A2);

AccelStepper stepper (1, 7, 6);

enum { ST_STOP, ST_CW , ST_CCW };
int state = ST_STOP;

const int CW  = 1;
const int CCW = -CW;
int dir       = CW;

int pos;

// -----------------------------------------------------------------------------
void loop ()
{
    startButton.loop ();
    limitSwitchCw.loop ();
    limitSwitchCcw.loop ();

    switch (state) {
    case ST_STOP:
        if (startButton.isPressed())  {
            Serial.println ("  Go");
            if (CW == dir)
                state = ST_CW;
            else
                state = ST_CCW;
        }
        stepper.stop ();
        break;

    case ST_CW:
        if (limitSwitchCw.isPressed())  {
            Serial.println ("  Stop CW");
            state = ST_STOP;
            dir   = CCW;
        }
        else  {
            stepper.moveTo (pos += dir);
            stepper.run ();
        }
        break;

    case ST_CCW:
        if (limitSwitchCcw.isPressed())  {
            Serial.println ("  Stop CCW");
            state = ST_STOP;
            dir   = CW;
        }
        else  {
            stepper.moveTo (pos += dir);
            stepper.run ();
        }
        break;
    }
}

// -----------------------------------------------------------------------------
void setup () {
    Serial.begin (9600);

    limitSwitchCw.setDebounceTime  (50);
    limitSwitchCcw.setDebounceTime (50);
    startButton.setDebounceTime    (50);

    stepper.setMaxSpeed     (2000.0);
    stepper.setAcceleration (8000.0);
}

yes

Limit switches must be wired normally open.
When DIR is HIGH motor must rotate clockwise.
Limit SW1 must be at the maximum clockwise rotation.

#define OPEN HIGH
#define CLOSED LOW

const int GoBtn = A0;
const int LimitSW1_pin = A1;
const int LimitSW2_pin = A2;

// Connect DIR+ to 3, PUL+ to 4
// DIR- and PUL- to GND
const int DIR = 3;
const int PUL = 4;

bool Rotating = false;
bool Limit1State = OPEN;
bool Limit2State = OPEN;
unsigned int Speed;


void setup()
{
  Serial.begin(9600);
  pinMode(DIR, OUTPUT);
  pinMode(PUL, OUTPUT);

  digitalWrite(DIR, HIGH);
  digitalWrite(PUL, LOW);

  pinMode(GoBtn, INPUT_PULLUP);
  pinMode(LimitSW1_pin, INPUT_PULLUP);
  pinMode(LimitSW2_pin, INPUT_PULLUP);
  Speed = 2500; // Bigger number is slower
  delay(10);
}

void loop()
{
  byte Go = digitalRead (GoBtn);
  byte LimitSW1 = digitalRead (LimitSW1_pin);
  byte LimitSW2 = digitalRead (LimitSW2_pin);

  if ((LimitSW1 == CLOSED) && (Limit1State == OPEN))
  {
    Rotating = false;
    Limit1State = CLOSED;
    Limit2State = OPEN;
    digitalWrite(DIR, LOW); // Move anticlockwise
    Serial.println("Motor stoped");
  }

  if ((LimitSW2 == CLOSED) && (Limit2State == OPEN))
  {
    Rotating = false;
    Limit1State = OPEN;
    Limit2State = CLOSED;
    digitalWrite(DIR, HIGH); // Move clockwise
    Serial.println("Motor stoped");
  }

  if ((Go == LOW) && (Rotating == false))
  {
    Rotating = true;
    delay(100);
    Serial.println("Motor rotates");
  }


  // Make the motor move one step
  if (Rotating == true)
  {
    digitalWrite(PUL, HIGH);
    delayMicroseconds(25);
    digitalWrite(PUL, LOW);
    delayMicroseconds(Speed);
  }
}

Jim-p

Its working :+1: :muscle:
I would like to pay you for your efforts !

When son finish project ill post it here how it work
Thank you ,

That is enough payment
Have a nice day