DQ420 MA Stepper Motor Driver - NEMA 17 42BYGH616 - Arduino Code CW/CCW Motion Program

Good day everyone, I would like to verify our project..
We used L298 motor driver to our NEMA 17 stepper that forwards for a specific steps declared on the program, and then reverses to fulfill the project's objectives.

But then. our group have realized, after months of practice, two of our stepper motors gave up with the L298n Motor due to overheating and incompatibility

So the team has decided to change our driver, and Makerlab Electronics has recommended us DQ420MA Stepper Motor to be partnered with our NEMA 17 model 42BYGH616.

I would like to verify to our dear professionals here in our Arduino Forum if this code that we once have on our L298n circuit would work with our microstepping stepper driver DQ420MA.

Thank u for you kindness, and may all of you be blessed in all your projects, manuscripts and careers

Here is our code

//import Stepper.h Library
#include <Stepper.h>

//import Wire.h library
#include <Wire.h>

//import LCD.h library
#include <LiquidCrystal_I2C.h>

//16X2 LCD with module in Makerlab 4 Pins (Vcc, Gnd, SDA, SCL)
LiquidCrystal_I2C lcd(0x27, 20, 4);

//declare variable for steps
int stepsPerRevolution = 200;

//declare pins of Stepper Motor
//Stepper myStepper(stepsPerRevolution, 8,9,10,11);
const int stepPin = 5;
const int dirPin = 2;
const int enPin = 8;

//declare echo pin and trigger pin of ultrasonic sensor
const int IRPin = 12;

//declare variable for state of limit switch
int state = 0;

//declare pins of devices used per phase
const int Brush = 22;
const int Pump1 = 24;
const int Blower = 26;
const int Pump2 = 28;
const int Curing = 30;
const int Fan = 32;

//declare pins of LED indicator
const int Phase1 = 52; //White LED
const int Phase2 = 53; //Blue LED
const int Phase3 = 48; //Green LED
const int Phase4 = 46; //Red LED

void setup() {
Serial.begin(9600);
lcd.init(); // initialize the lcd
lcd.backlight();
lcd.clear();

myStepper.setSpeed(60);
Serial.begin(9600);

pinMode(2,INPUT_PULLUP); //Sets limit switch as INPUT with internal pull up resistor
pinMode(3,OUTPUT); //White LED LS Indicator

//declare devices per phase as Output
pinMode(Brush,OUTPUT);
pinMode(Pump1,OUTPUT);
pinMode(Blower,OUTPUT);
pinMode(Pump2,OUTPUT);
pinMode(Curing,OUTPUT);
pinMode(Fan,OUTPUT);

pinMode(Phase1,OUTPUT);
pinMode(Phase2,OUTPUT);
pinMode(Phase3,OUTPUT);
pinMode(Phase4,OUTPUT);
pinMode(IRPin, INPUT);

pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);

pinMode(enPin,OUTPUT);
digitalWrite(enPin,LOW);
}

void loop() {

//read value of Limit Switch
int temp = digitalRead(2);
int LED = digitalRead(3);
int IRPin = digitalRead(12);

//conditional statement to identify the state of Limit Switch
if(temp == HIGH){
state = 0; //0 = not pressed
digitalWrite(3, LOW);
}
else {
state = 1; //1 = pressed
digitalWrite(3, HIGH);
}
//main code of the machine
switch(LED){
case 0: if(IRPin == 1 && state == 0){
digitalWrite(Brush,LOW);
digitalWrite(Pump1,LOW);
digitalWrite(Blower,LOW);
digitalWrite(Pump2,LOW);
digitalWrite(30,LOW);
digitalWrite(32,LOW);

       Serial.println("Reverse");
       lcd.clear();
       lcd.setCursor(0,0);
       lcd.print("Reversing");
       lcd.setCursor(1,1);
       lcd.print("Going back to start");
      myStepper.setSpeed(60);
      myStepper.step(200);
       break;
       }

case 1: if(IRPin == 0 && state == 1){

      Serial.println("Begin Phase 1");
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Ongoing Phase1:");
      lcd.setCursor(2,1);
      lcd.print("Brushing Phase");
      digitalWrite(Phase1,HIGH);
      digitalWrite(Phase2,LOW);
      digitalWrite(Phase3,LOW);
      digitalWrite(Phase4,LOW);
      
      digitalWrite(Brush,LOW);  //CLOSE
      digitalWrite(Pump1,HIGH); //OPEN
      digitalWrite(Blower,LOW); //CLOSE
      digitalWrite(Pump2,LOW);  //CLOSE
      digitalWrite(30,LOW);     //CLOSE
      digitalWrite(32,LOW);

      delay(500);
      myStepper.setSpeed(60);
      myStepper.step(500); 

      delay(500);
      myStepper.setSpeed(60);
      myStepper.step(-500);

      digitalWrite(Brush,HIGH);    //OPEN
      digitalWrite(Pump1,LOW);     //CLOSE
      digitalWrite(Blower,LOW);    //CLOSE
      digitalWrite(Pump2,LOW);     //CLOSE
      digitalWrite(Curing,LOW);        //CLOSE
      digitalWrite(Fan,LOW);
                
      delay(500);
      myStepper.setSpeed(60);
      myStepper.step(500);
      
      delay(500);
      myStepper.setSpeed(60);
      myStepper.step(-500);

      delay(500);
      myStepper.setSpeed(60);
      myStepper.step(500);

      delay(500);
      myStepper.setSpeed(60);
      myStepper.step(-500);
      
      delay(500);
      myStepper.setSpeed(60);
      myStepper.step(500);

      delay(500);
      myStepper.setSpeed(60);
      myStepper.step(-500);

      delay(500);
      myStepper.setSpeed(60);
      myStepper.step(500);
      
      delay(500);
      myStepper.setSpeed(60);
      myStepper.step(-500);

      delay(500);
      myStepper.setSpeed(60);
      myStepper.step(500);
      
      delay(500);
      myStepper.setSpeed(60);
      myStepper.step(-500);

      delay(500);
      myStepper.setSpeed(60);
      myStepper.step(500);
      
      delay(500);
      myStepper.setSpeed(60);
      myStepper.step(-500);

      delay(500);
      myStepper.setSpeed(60);
      myStepper.step(500);
      
      delay(500);
      myStepper.setSpeed(60);
      myStepper.step(-500);

      delay(500);
      myStepper.setSpeed(60);
      myStepper.step(500);

      delay(500);
      myStepper.setSpeed(60);
      myStepper.step(-500);
      
      delay(500);
      myStepper.setSpeed(60);
      myStepper.step(500);

      delay(500);
      myStepper.setSpeed(60);
      myStepper.step(-500);
      
      delay(500);
      myStepper.setSpeed(60);
      myStepper.step(500);

      delay(500);
      myStepper.setSpeed(60);
      myStepper.step(-500);
      
      delay(500);
      myStepper.setSpeed(60);
      myStepper.step(500);

      delay(500);
      myStepper.setSpeed(60);
      myStepper.step(-500);
      
      delay(500);
      myStepper.setSpeed(60);
      myStepper.step(-500);
      
      delay(500);
      digitalWrite(Brush,LOW);   //
      digitalWrite(Pump1,LOW);   //
      digitalWrite(Blower,HIGH);  
    
      Serial.println("Begin Phase 2");
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Ongoing Phase 2:");
      lcd.setCursor(2,1);
      lcd.print("Drying Phase");
      digitalWrite(Phase1,LOW);
      digitalWrite(Phase2,HIGH);
      digitalWrite(Phase3,LOW);
      digitalWrite(Phase4,LOW);

      myStepper.setSpeed(60);
      myStepper.step(1200);
      delay(300000);
    
      digitalWrite(Blower,LOW);
      digitalWrite(Pump2,LOW);
      digitalWrite(30,LOW);
      digitalWrite(32,LOW);
      myStepper.setSpeed(60);
      myStepper.step(1500);
      
      delay(500);
      Serial.println("Begin Phase 3");
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Ongoing Phase 3:");
      lcd.setCursor(2,1);
      lcd.print("Coating Phase");
      digitalWrite(Phase1,LOW);
      digitalWrite(Phase2,LOW);
      digitalWrite(Phase3,HIGH);
      digitalWrite(Phase4,LOW);
      
      myStepper.setSpeed(60);
      myStepper.step(1500);
      delay(500);
      
      digitalWrite(Pump2,HIGH);
      delay(2000);
      
      myStepper.setSpeed(60);
      myStepper.step(500);
      
      delay(500);
      myStepper.setSpeed(60);
      myStepper.step(-500);

      delay(500);
      myStepper.setSpeed(60);
      myStepper.step(500);

      delay(500);
      myStepper.setSpeed(60);
      myStepper.step(-500);
      
      delay(500);
      myStepper.setSpeed(60);
      myStepper.step(300);
      
      digitalWrite(Pump2,LOW);
      delay(1000);
      
      Serial.println("Begin Phase 4");
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Ongoing Phase 4:");
      lcd.setCursor(2,1);
      lcd.print("Curing Phase");
      digitalWrite(Phase1,LOW);
      digitalWrite(Phase2,LOW);
      digitalWrite(Phase3,LOW);
      digitalWrite(Phase4,HIGH);
      myStepper.setSpeed(60);
      myStepper.step(1500);
     
      delay(500);
      digitalWrite(Curing,HIGH);
      digitalWrite(32,HIGH);
      digitalWrite(Phase4, LOW);
      delay(420000);               //7 minute
      digitalWrite(Curing,LOW);
      digitalWrite(Curing,LOW);
      

      Serial.println("Process Finished!");
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Congratulations!!!");
      lcd.setCursor(3,1);
      lcd.print("Polishing is achieved");
      lcd.setCursor(3,1);
      lcd.print("Please get Copper PCB");
      lcd.setCursor(4,1);
      lcd.print("Immediately");
      
      delay (42000);
      break;
      }
    else (IRPin == 1 && state == 1);{         
      Serial.println("Press start button to begin");
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("PRESS START");
      break;
    }

default: {

      Serial.println("REVERSING");
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("WAIT PATIENTLY");
      lcd.setCursor(1,0);
      lcd.print("REVERSING");
      myStepper.setSpeed(60);
      myStepper.step(-4700);

      myStepper.setSpeed(0);
      myStepper.step(0);
      break;

}
}
}

There's nothing intrinsically wrong there, it's just ... unusual to use a switch/case, where an if/else would suffice.

Also, give your pins names, and use the names - anonymous pin references are tedious to read.

Please remember to use code tags when posting code.

Thank you sir, this is my first time posting in Arduino Forum and I am dedicated to learn the process and thank u I really appreciate the time, i will practice...

I would like to verify sir, will our previous code work on our new microstepping stepper driver? I am currently researching about methods and sample codes about a microstepper driver and it is way too different to our previous code using the L298n driver.

On our previous code, we can program the stepper to make full 5 rotations using this code

< delay(500);
myStepper.setSpeed(60);
myStepper.step(500); >

and then it reverses by changing the stepper's step sign below

< delay(500);
myStepper.setSpeed(60);
myStepper.step(-500); >

Meanwhile, when I do some research, this code has produced one full cycle rotation

<
digitalWrite(dirPin,HIGH); // Enables the motor to move
//in a particular direction
// Makes 200 pulses for making one full cycle rotation
for(int x = 0; x < 1600; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(800);
digitalWrite(stepPin,LOW);
delayMicroseconds(800);
}
delay(1000); // One second delay

... Our project requires multiple forward and reverse motions so if our team uses this method I have researched, our program will be too long huhu... thank u sir po answering

Hi, @kimirella
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".

This will help with advice on how to present your code and problems.

Can you please post a link to data/specs of your stepper and driver?

Thanks.. Tom.. :grinning: :+1: :coffee: :australia:

Thank you sir George,

This is the link for the NEMA 17
https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=&cad=rja&uact=8&ved=2ahUKEwiwo6Gf4cf1AhWEEIgKHZWgAMsQFnoECAYQAQ&url=https%3A%2F%2Fwww.makerlab-electronics.com%2Fstepper-motor-nema17-42bygh616-2%2F&usg=AOvVaw0IY-JMZslCKVnonHAFlaDx

and this is our microstepper motor driver
https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=&cad=rja&uact=8&ved=2ahUKEwijjM6y4cf1AhUEHXAKHZZ5D-0QFnoECAsQAQ&url=https%3A%2F%2Fwww.makerlab-electronics.com%2Fproduct%2Fstepper-motor-driver-dq420ma%2F&usg=AOvVaw2YNi3vlaRJ_32E28rr85kb

thank u sir

//import Stepper.h Library
#include <Stepper.h>

//import Wire.h library
#include <Wire.h>

//import LCD.h library
#include <LiquidCrystal_I2C.h>

//16X2 LCD with module in Makerlab 4 Pins (Vcc, Gnd, SDA, SCL)
LiquidCrystal_I2C lcd(0x27, 20, 4);

//declare variable for steps
int stepsPerRevolution = 200;

//declare pins of Stepper Motor
//Stepper myStepper(stepsPerRevolution, 8,9,10,11);
const int stepPin = 5; 
const int dirPin  = 2; 
const int enPin   = 8;

//declare echo pin and trigger pin  of ultrasonic sensor
const int IRPin = 12;

//declare variable for state of limit switch
int state = 0;

//declare pins of devices used per phase
const int Brush  = 22;
const int Pump1  = 24;
const int Blower = 26;
const int Pump2  = 28;
const int Curing = 30;
const int Fan = 32;

//declare pins of LED indicator
const int Phase1 = 52;      //White LED
const int Phase2 = 53;      //Blue LED
const int Phase3 = 48;      //Green LED
const int Phase4 = 46;      //Red LED


void setup() {
  Serial.begin(9600);
  lcd.init();                      // initialize the lcd 
  lcd.backlight();
  lcd.clear();

  myStepper.setSpeed(60);
  Serial.begin(9600);
  
  pinMode(2,INPUT_PULLUP); //Sets limit switch as INPUT with internal pull up resistor
  pinMode(3,OUTPUT);       //White LED LS Indicator

  //declare devices per phase as Output
  pinMode(Brush,OUTPUT);
  pinMode(Pump1,OUTPUT);
  pinMode(Blower,OUTPUT);
  pinMode(Pump2,OUTPUT);
  pinMode(Curing,OUTPUT);
  pinMode(Fan,OUTPUT);

  pinMode(Phase1,OUTPUT);
  pinMode(Phase2,OUTPUT);
  pinMode(Phase3,OUTPUT);
  pinMode(Phase4,OUTPUT);
  pinMode(IRPin, INPUT);

  pinMode(stepPin,OUTPUT); 
  pinMode(dirPin,OUTPUT);

  pinMode(enPin,OUTPUT);
  digitalWrite(enPin,LOW);
}

void loop() {

//read value of Limit Switch
int temp =  digitalRead(2);
int LED =   digitalRead(3);
int IRPin = digitalRead(12);

  
//conditional statement to identify the state of Limit Switch
if(temp == HIGH){
  state = 0; //0 = not pressed
  digitalWrite(3, LOW);
}
else { 
  state = 1; //1 = pressed
  digitalWrite(3, HIGH);
}
//main code of the machine
switch(LED){
  case 0:  if(IRPin == 1 && state == 0){
          digitalWrite(Brush,LOW);
          digitalWrite(Pump1,LOW);
          digitalWrite(Blower,LOW);
          digitalWrite(Pump2,LOW);
          digitalWrite(30,LOW);
          digitalWrite(32,LOW);

           Serial.println("Reverse");
           lcd.clear();
           lcd.setCursor(0,0);
           lcd.print("Reversing");
           lcd.setCursor(1,1);
           lcd.print("Going back to start");
          myStepper.setSpeed(60);
          myStepper.step(200);
           break;
           }
  case 1: if(IRPin == 0 && state == 1){

          Serial.println("Begin Phase 1");
          lcd.clear();
          lcd.setCursor(0,0);
          lcd.print("Ongoing Phase1:");
          lcd.setCursor(2,1);
          lcd.print("Brushing Phase");
          digitalWrite(Phase1,HIGH);
          digitalWrite(Phase2,LOW);
          digitalWrite(Phase3,LOW);
          digitalWrite(Phase4,LOW);
          
          digitalWrite(Brush,LOW);  //CLOSE
          digitalWrite(Pump1,HIGH); //OPEN
          digitalWrite(Blower,LOW); //CLOSE
          digitalWrite(Pump2,LOW);  //CLOSE
          digitalWrite(30,LOW);     //CLOSE
          digitalWrite(32,LOW);

          delay(500);
          myStepper.setSpeed(60);
          myStepper.step(500); 

          delay(500);
          myStepper.setSpeed(60);
          myStepper.step(-500);

          digitalWrite(Brush,HIGH);    //OPEN
          digitalWrite(Pump1,LOW);     //CLOSE
          digitalWrite(Blower,LOW);    //CLOSE
          digitalWrite(Pump2,LOW);     //CLOSE
          digitalWrite(Curing,LOW);        //CLOSE
          digitalWrite(Fan,LOW);
                    
          delay(500);
          myStepper.setSpeed(60);
          myStepper.step(500);
          
          delay(500);
          myStepper.setSpeed(60);
          myStepper.step(-500);

          delay(500);
          myStepper.setSpeed(60);
          myStepper.step(500);

          delay(500);
          myStepper.setSpeed(60);
          myStepper.step(-500);
          
          delay(500);
          myStepper.setSpeed(60);
          myStepper.step(500);

          delay(500);
          myStepper.setSpeed(60);
          myStepper.step(-500);

          delay(500);
          myStepper.setSpeed(60);
          myStepper.step(500);
          
          delay(500);
          myStepper.setSpeed(60);
          myStepper.step(-500);

          delay(500);
          myStepper.setSpeed(60);
          myStepper.step(500);
          
          delay(500);
          myStepper.setSpeed(60);
          myStepper.step(-500);

          delay(500);
          myStepper.setSpeed(60);
          myStepper.step(500);
          
          delay(500);
          myStepper.setSpeed(60);
          myStepper.step(-500);

          delay(500);
          myStepper.setSpeed(60);
          myStepper.step(500);
          
          delay(500);
          myStepper.setSpeed(60);
          myStepper.step(-500);

          delay(500);
          myStepper.setSpeed(60);
          myStepper.step(500);

          delay(500);
          myStepper.setSpeed(60);
          myStepper.step(-500);
          
          delay(500);
          myStepper.setSpeed(60);
          myStepper.step(500);

          delay(500);
          myStepper.setSpeed(60);
          myStepper.step(-500);
          
          delay(500);
          myStepper.setSpeed(60);
          myStepper.step(500);

          delay(500);
          myStepper.setSpeed(60);
          myStepper.step(-500);
          
          delay(500);
          myStepper.setSpeed(60);
          myStepper.step(500);

          delay(500);
          myStepper.setSpeed(60);
          myStepper.step(-500);
          
          delay(500);
          myStepper.setSpeed(60);
          myStepper.step(-500);
          
          delay(500);
          digitalWrite(Brush,LOW);   //
          digitalWrite(Pump1,LOW);   //
          digitalWrite(Blower,HIGH);  
        
          Serial.println("Begin Phase 2");
          lcd.clear();
          lcd.setCursor(0,0);
          lcd.print("Ongoing Phase 2:");
          lcd.setCursor(2,1);
          lcd.print("Drying Phase");
          digitalWrite(Phase1,LOW);
          digitalWrite(Phase2,HIGH);
          digitalWrite(Phase3,LOW);
          digitalWrite(Phase4,LOW);

          myStepper.setSpeed(60);
          myStepper.step(1200);
          delay(300000);
        
          digitalWrite(Blower,LOW);
          digitalWrite(Pump2,LOW);
          digitalWrite(30,LOW);
          digitalWrite(32,LOW);
          myStepper.setSpeed(60);
          myStepper.step(1500);
          
          delay(500);
          Serial.println("Begin Phase 3");
          lcd.clear();
          lcd.setCursor(0,0);
          lcd.print("Ongoing Phase 3:");
          lcd.setCursor(2,1);
          lcd.print("Coating Phase");
          digitalWrite(Phase1,LOW);
          digitalWrite(Phase2,LOW);
          digitalWrite(Phase3,HIGH);
          digitalWrite(Phase4,LOW);
          
          myStepper.setSpeed(60);
          myStepper.step(1500);
          delay(500);
          
          digitalWrite(Pump2,HIGH);
          delay(2000);
          
          myStepper.setSpeed(60);
          myStepper.step(500);
          
          delay(500);
          myStepper.setSpeed(60);
          myStepper.step(-500);

          delay(500);
          myStepper.setSpeed(60);
          myStepper.step(500);

          delay(500);
          myStepper.setSpeed(60);
          myStepper.step(-500);
          
          delay(500);
          myStepper.setSpeed(60);
          myStepper.step(300);
          
          digitalWrite(Pump2,LOW);
          delay(1000);
          
          Serial.println("Begin Phase 4");
          lcd.clear();
          lcd.setCursor(0,0);
          lcd.print("Ongoing Phase 4:");
          lcd.setCursor(2,1);
          lcd.print("Curing Phase");
          digitalWrite(Phase1,LOW);
          digitalWrite(Phase2,LOW);
          digitalWrite(Phase3,LOW);
          digitalWrite(Phase4,HIGH);
          myStepper.setSpeed(60);
          myStepper.step(1500);
         
          delay(500);
          digitalWrite(Curing,HIGH);
          digitalWrite(32,HIGH);
          digitalWrite(Phase4, LOW);
          delay(420000);               //7 minute
          digitalWrite(Curing,LOW);
          digitalWrite(Curing,LOW);
          

          Serial.println("Process Finished!");
          lcd.clear();
          lcd.setCursor(0,0);
          lcd.print("Congratulations!!!");
          lcd.setCursor(3,1);
          lcd.print("Polishing is achieved");
          lcd.setCursor(3,1);
          lcd.print("Please get Copper PCB");
          lcd.setCursor(4,1);
          lcd.print("Immediately");
          
          delay (42000);
          break;
          }
        else (IRPin == 1 && state == 1);{         
          Serial.println("Press start button to begin");
          lcd.clear();
          lcd.setCursor(0,0);
          lcd.print("PRESS START");
          break;
        }
  default: {          

          Serial.println("REVERSING");
          lcd.clear();
          lcd.setCursor(0,0);
          lcd.print("WAIT PATIENTLY");
          lcd.setCursor(1,0);
          lcd.print("REVERSING");
          myStepper.setSpeed(60);
          myStepper.step(-4700);

          myStepper.setSpeed(0);
          myStepper.step(0);
          break;
  }
}
}

DQ420MA Stepper Motor Driver Specifications:
(2-phase hybrid stepping motor driver)

Input Voltage: 12-36VDC
Input Current <2A
Output Current: 0.44A- 2.83A
Power Consumption: 40 Watts

Microcontroller used: Arduino MEGA

NEMA 17 MODEL 42BYGH616

Step Angle: 1.8 degrees
Motor Length (L): 40mm
Rate Voltage: 4.1
Rate Current: 0.5
Phase Resistance: 8
Phase Inductance: 15mH
Holding Torque: 2600
Lead wire: 4
Detent Torque: 220

Hi,
Thanks for the info.
A circuit diagram will be helpful too.

Have you sett the current limit on the 420?

Thanks.. Tom.... :grinning: :+1: :coffee: :australia:

Hi,
Thanks for the info.
However your diagram is not a typical schematic and is somewhat hard to follow.


The circuit breaker a A doesn't seem to do anything.
The relay module at B, its output is wired to connect two already connected wires back at the terminal strip.

The four LEDs are connected in parallel, so you will not have individual control of them.
It also looks as though they get their power from the LCD backplane.

Also the 420 is not shown, but the old 298.

It would be better if you used standard circuit symbols and layout, rather than cut and paste images.
If you have built and run your project, then a proper schematic should be available.

Tom... :grinning: :+1: :coffee: :australia:

Thank you sir George, our team will absolutely follow your recommendations and make a proper schematic diagram for our system, thank you for your time and God bless you

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