Problem driving 4 stepper motors with arduino mega

hi!
I'm doing a project where i need to control 4 stepper motors at the same time and to be moved separately.
the hardware that I'm using is an Arduino mega 3 a ramps cnc shield to for the hardware interface 3 drv8825 to control 4 17 hs4401 stepper motors and a 4X4 key pad to control the motors
(I have one of the driver controlling 2 motors but planning to add another driver to control it(
I'm using these libraries to control the motors BasicStepperDriver.h MultiDriver.h SyncDriver.h <Keypad.h>
I'm trying to change the direction of the motors with the key pad but the code only works on the x motor, (both buttons for the clockwise and counter clockwise turn the motors in the same directions, the stop button works for the 3 motors)
if any has an idea to why this happens or a know of a libraries that can interface with 4 motors independently it would be really helpful!

#include <Arduino.h>
#include "BasicStepperDriver.h"
#include "MultiDriver.h"
#include "SyncDriver.h"

#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
    {'a','b','c','d'},
    {'e','f','g','h'},
    {'i','j','k','l'},
    {'m','n','o','p'}
};

byte rowPins[ROWS] = {32, 47, 45, 43}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {41, 39, 37,35}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

#define MICROSTEPS 2
// Motor steps per revolution. Most steppers are 200 steps or 1.8 degrees/step
#define MOTOR_STEPS 200
// Target RPM for X axis motor
#define MOTOR_X_RPM 90
// Target RPM for Y axis motor
#define MOTOR_Y_RPM 90

#define MOTOR_Z_RPM 90


#define X_STEP_PIN         54
#define X_DIR_PIN          55
#define X_ENABLE_PIN       38
#define X_MIN_PIN           3
#define X_MAX_PIN           2

#define Y_STEP_PIN         60
#define Y_DIR_PIN          61
#define Y_ENABLE_PIN       56
#define Y_MIN_PIN          14
#define Y_MAX_PIN          15

#define Z_STEP_PIN         46
#define Z_DIR_PIN          48
#define Z_ENABLE_PIN       62
#define Z_MIN_PIN          18
#define Z_MAX_PIN          19

#define E_STEP_PIN         26
#define E_DIR_PIN          28
#define E_ENABLE_PIN       24

#define Q_STEP_PIN         36
#define Q_DIR_PIN          34
#define Q_ENABLE_PIN       30

#define SDPOWER            -1
#define SDSS               53
#define LED_PIN            13

#define FAN_PIN            9

#define PS_ON_PIN          12
#define KILL_PIN           -1

#define HEATER_0_PIN       10
#define HEATER_1_PIN       8
#define TEMP_0_PIN          13   // ANALOG NUMBERING
#define TEMP_1_PIN          14   // ANALOG NUMBERING


#define FWR 5
int BCK =-5;
#define STP 0

int mvX = STP;
int mvY = STP;
int mvZ = STP;
BasicStepperDriver stepperX(MOTOR_STEPS, X_DIR_PIN, X_STEP_PIN);
BasicStepperDriver stepperY(MOTOR_STEPS, Y_STEP_PIN, Y_STEP_PIN);
BasicStepperDriver stepperZ(MOTOR_STEPS, Z_STEP_PIN, Z_STEP_PIN);
MultiDriver controller(stepperX, stepperY, stepperZ);

void setup() {
  
  pinMode(FAN_PIN , OUTPUT);
//  pinMode(HEATER_0_PIN , OUTPUT);
//  pinMode(HEATER_1_PIN , OUTPUT);
  pinMode(LED_PIN  , OUTPUT);
  
  pinMode(X_STEP_PIN  , OUTPUT);
  pinMode(X_DIR_PIN    , OUTPUT);
  pinMode(X_ENABLE_PIN    , OUTPUT);
  
  pinMode(Y_STEP_PIN  , OUTPUT);
  pinMode(Y_DIR_PIN    , OUTPUT);
  pinMode(Y_ENABLE_PIN    , OUTPUT);
  
  pinMode(Z_STEP_PIN  , OUTPUT);
  pinMode(Z_DIR_PIN    , OUTPUT);
  pinMode(Z_ENABLE_PIN    , OUTPUT);
  
//  pinMode(E_STEP_PIN  , OUTPUT);
//  pinMode(E_DIR_PIN    , OUTPUT);
//  pinMode(E_ENABLE_PIN    , OUTPUT);
//  
//  pinMode(Q_STEP_PIN  , OUTPUT);
//  pinMode(Q_DIR_PIN    , OUTPUT);
//  pinMode(Q_ENABLE_PIN    , OUTPUT);
  
//   digitalWrite(X_ENABLE_PIN    , LOW);
//    digitalWrite(Y_ENABLE_PIN    , LOW);
//    digitalWrite(Z_ENABLE_PIN    , LOW);
//    digitalWrite(E_ENABLE_PIN    , LOW);
//    digitalWrite(Q_ENABLE_PIN    , LOW);

    stepperX.begin(MOTOR_X_RPM, MICROSTEPS);
    stepperY.begin(MOTOR_Y_RPM, MICROSTEPS);
    stepperZ.begin(MOTOR_Z_RPM, MICROSTEPS);

//    keypad.addEventListener(keypadEvent);
//    stepperX.setEnableActiveState(LOW);
//    stepperY.setEnableActiveState(LOW);
}


void loop () {
  digitalWrite(FAN_PIN, LOW);
  if (millis() %1000 <500) 
    digitalWrite(LED_PIN, HIGH);
  else
   digitalWrite(LED_PIN, LOW);
  char key = keypad.getKey();
  foo(key);
//  if (millis() %1000 <300) {
//    digitalWrite(HEATER_0_PIN, HIGH);
//    digitalWrite(HEATER_1_PIN, LOW);
//    digitalWrite(FAN_PIN, LOW);
//  } else if (millis() %1000 <600) {
//    digitalWrite(HEATER_0_PIN, LOW);
//    digitalWrite(HEATER_1_PIN, HIGH);
//    digitalWrite(FAN_PIN, LOW);
//  } else  {
//    digitalWrite(HEATER_0_PIN, LOW);
//    digitalWrite(HEATER_1_PIN, LOW);
//    digitalWrite(FAN_PIN, HIGH);
//  }
  
//  if (millis() %10000 <5000) {
//    digitalWrite(X_DIR_PIN    , HIGH);
//    digitalWrite(Y_DIR_PIN    , HIGH);
//    digitalWrite(Z_DIR_PIN    , HIGH);
//    digitalWrite(E_DIR_PIN    , HIGH);
//    digitalWrite(Q_DIR_PIN    , HIGH);
  
//  else {
//    digitalWrite(X_DIR_PIN    , LOW);
//    digitalWrite(Y_DIR_PIN    , LOW);
//    digitalWrite(Z_DIR_PIN    , LOW);
//    digitalWrite(E_DIR_PIN    , LOW);
//    digitalWrite(Q_DIR_PIN    , LOW);
//  }
  
  
//    digitalWrite(X_STEP_PIN    , HIGH);
//    digitalWrite(Y_STEP_PIN    , HIGH);
//    digitalWrite(Z_STEP_PIN    , HIGH);
//    digitalWrite(E_STEP_PIN    , HIGH);
//    digitalWrite(Q_STEP_PIN    , HIGH); 
//  delay(1);
    
//    digitalWrite(X_STEP_PIN    , LOW);
//    digitalWrite(Y_STEP_PIN    , LOW);
//    digitalWrite(Z_STEP_PIN    , LOW);
//    digitalWrite(E_STEP_PIN    , LOW);
//    digitalWrite(Q_STEP_PIN    , LOW); 

//    controller.rotate(90*5, 60*15, 60*15);
//    delay(1000);
//    controller.rotate(-90*5,0,0);
//    delay(1000);
//    controller.rotate(0, -30*15,0);
//    delay(1000);
//    controller.rotate(0,0, -30*15);
//    delay(1000);
//    controller.move(200,0,0);
//    delay(1000);
//    controller.rotate(0,200,0);
//    delay(1000);
//    controller.rotate(0,0,200);
//    delay(1000);
//    controller.rotate(0,0, -200);
//    delay(1000);  
//    controller.rotate(0,-200,0);
//    delay(1000);
//    controller.move(-200,0,0);
//    delay(1000);
//    controller.move(-200,-200,-200);
//    delay(1000);
//    controller.move(200,200,200);
//    delay(1000);
    controller.move(mvX,mvY,mvZ);
}

void foo(char key){
  if (key == 'a'){
    mvX =5;
  }
  if (key == 'b'){
    mvX =0;
  }
  if (key == 'c'){
    mvX =-5;
  }
  if (key == 'e'){
    mvY =5;
  }
  if (key == 'f'){
    mvY =0;
  }if (key == 'g'){
    mvY =-5;
  }
  if (key == 'i'){
    mvZ =5;
  }
  if (key == 'j'){
    mvZ =0;
  }
  if (key == 'k'){
    mvZ = -5;
  }
}

my code is based on the ramps 1.4 test and the
BasicStepperDriver example

First, confirm the data that is going into the function. You will need a Serial.begin(9600); ( or the baud rate of your choice) in setup(). Serial prints are your best debugging tool.

Thanks for code tags on your first post.

You could try my MobaTools library. It can drive up to 6 steppers independently. The step pulses are created by timer interrupts in the background, So you don't have to bother with pulse generation in your sketch and all methodes to control the motors are non blocking.
Examples and a documention are provided.

thanks! i will try that now.
I'm certain that some of it works because I can start/stop all 3 motors but only motor x I can change the direction.
I'm not sure if its a library problem or a problem with the code or the drivers themselves. I'll update what I found after debugging.

thanks I'll try to write another code with it!

I wrote a code using the latest version of the MobaTool library but the motors don't run at all, i used the examples to write a code that was supposed to work with my drivers and motors but nothing also tried to add prints to se if the problem is with the keypad but the keys are printed as they should when pressed. this is my code

#include <Arduino.h>
#include "BasicStepperDriver.h"
#include "MultiDriver.h"
#include "SyncDriver.h"
#include <MobaTools.h>
#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
    {'1','2','3','A'},
    {'4','5','6','B'},
    {'7','8','9','C'},
    {'*','0','#','D'}
};

byte rowPins[ROWS] = {32, 47, 45, 43}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {41, 39, 37,35}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

#define MICROSTEPS 2
// Motor steps per revolution. Most steppers are 200 steps or 1.8 degrees/step
const int  MOTOR_STEPS = 200;
// Target RPM for X axis motor
#define MOTOR_X_RPM 90
// Target RPM for Y axis motor
#define MOTOR_Y_RPM 90

#define MOTOR_Z_RPM 90


const byte X_STEP_PIN   =  54;
const byte X_DIR_PIN    =  55;
#define X_ENABLE_PIN       38

const byte Y_STEP_PIN   =  60;
const byte Y_DIR_PIN   =   61;
#define Y_ENABLE_PIN       56

const byte Z_STEP_PIN  =  46;
const byte Z_DIR_PIN  =  48;
#define Z_ENABLE_PIN       62

#define LED_PIN            13
#define FAN_PIN            9

#define FWR 5
int BCK =-5;
#define STP 0

int mvX = STP;
int mvY = STP;
int mvZ = STP;


MoToStepper stepperX( MOTOR_STEPS, STEPDIR );  // create a stepper instance
MoToStepper stepperY( MOTOR_STEPS, STEPDIR );  // create a stepper instance
MoToStepper stepperZ( MOTOR_STEPS, STEPDIR );  // create a stepper instance
void  setup() {
  
  Serial.begin(9600);
  stepperX.attach( X_STEP_PIN, X_DIR_PIN );
  stepperX.setSpeed( MOTOR_X_RPM );              // 30 rev/min (if stepsPerRev is set correctly)
  stepperX.setRampLen( MOTOR_STEPS / 2);
  stepperX.rotate(1);
  stepperY.attach( Y_STEP_PIN, Y_DIR_PIN );
  stepperY.setSpeed( MOTOR_Y_RPM );              // 30 rev/min (if stepsPerRev is set correctly)
  stepperY.setRampLen( MOTOR_STEPS / 2);
  stepperY.rotate(1);
  stepperZ.attach( Z_STEP_PIN, Z_DIR_PIN );
  stepperZ.setSpeed( MOTOR_Z_RPM );              // 30 rev/min (if stepsPerRev is set correctly)
  stepperZ.setRampLen( MOTOR_STEPS / 2);
  stepperY.rotate(1);
}


void loop () {
  digitalWrite(FAN_PIN, LOW);
  if (millis() %1000 <500) 
    digitalWrite(LED_PIN, HIGH);
  else
   digitalWrite(LED_PIN, LOW);
  char key = keypad.getKey();
  if (key){
  Serial.print("key pressed = ");
  Serial.println(key);
  }if (key == '1'){
    stepperX.rotate(1);    
  }
  if (key == '2'){
    stepperX.rotate(0);    
  }
  if (key == '3'){
    stepperX.rotate(-1);    
  }
  if (key == '4'){
    stepperY.rotate(1);    
  }
  if (key == '5'){
   stepperY.rotate(0);    
  }if (key == '6'){
    stepperY.rotate(-1);    
  }
  if (key == '7'){
   stepperZ.rotate(1);    
  }
  if (key == '8'){
   stepperZ.rotate(0);    
  }
  if (key == '9'){
    stepperZ.rotate(-1);    
  }
//  foo(key);
//    controller.move(mvX,mvY,mvZ);
}

void foo(char key){
  if (key == '1'){
    stepperX.rotate(1);    
  }
  if (key == '2'){
    stepperX.rotate(0);    
  }
  if (key == '3'){
    stepperX.rotate(-1);    
  }
  if (key == '4'){
    stepperY.rotate(1);    
  }
  if (key == '5'){
   stepperY.rotate(0);    
  }if (key == '6'){
    stepperY.rotate(-1);    
  }
  if (key == '7'){
   stepperZ.rotate(1);    
  }
  if (key == '8'){
   stepperZ.rotate(0);    
  }
  if (key == '9'){
    stepperZ.rotate(-1);    
  }
}

I tried adding the prints, the keys are read correctly but motors z and y still don't move backwards, the forward and stop buttons work fine but the backwards button moves the motor forward (if I press it when the motors is stopped then the motor starts moving but in a clockwise direction )

I uploaded your sketch to my Mega, and the motors run fine - apart from some minor problems:

  • The speed parameter in SetSpeed must be RPM*10, so the motors run ten times too slow with '90'.
  • There is a typo in setup:

but that's only relevant for the initial turning af the motors.

What about the enable pins? I don't know your board. The driver itself enables the motor if the enable-pin is left open. Is this the same for your shield? You did not initialize the enable pins in your sketch.

P.S. I uncommented all the includes for the BasicStepperDriver, because I don't have this installed and the includes are useless in this sketch:

//#include "BasicStepperDriver.h"
//#include "MultiDriver.h"
//#include "SyncDriver.h"

But I don't think that makes a difference ..?

Regarding your original sketch. You did not pass the dir-pins as parameter for the Y and Z motor, instead you passed the step pins twice:

thank you very very mutch!!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.