This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

DRV8421BEVM: To drive stepper motor clockwise and counter clockwise.

Part Number: DRV8421BEVM


Tool/software:

Hi Team,

Hope you are doing well.

I am using DRV8421BDGQR stepper motor deriver IC and design costumer required customized design board to derive the NEMA17 stepper motor. I control it using four pin namely IN1, IN2, EN. I using the Arduino IDE and ESP32 C3 development board and write the sample code to derive the the NEMA17 motor clock wise and counter clock wise. Motor is successfully derive only clock wise direction.

Issue is that stepper motor is not to derive counter clock wise direction. Please guide me to derive the motor clock wise and counter clock wise direction.

I use 12V 2A power adopter, I common all ground like ESP32C3 and power adopter. The ESP32C3 is powered via USB cable connected to PC and ESP32C3 run on 3V3 volt. Also I check on 12V 1A stepper motor.

Please find the attachment of schematic and code snippet.

// Pin Definitions
const int EN_PIN = 1;    // Enable pin for DRV8421B
const int IN1_PIN = 5;   // Input 1 pin for direction control
const int IN2_PIN = 6;   // Input 2 pin for direction control

// Motor Control Parameters
const int STEPS_PER_REVOLUTION = 300;  // Typical for NEMA17 (1.8 degree step angle)

void setup() {
  // Initialize pins
  pinMode(EN_PIN, OUTPUT);
  pinMode(IN1_PIN, OUTPUT);
  pinMode(IN2_PIN, OUTPUT);

  // Initialize Serial for debugging
  Serial.begin(115200);
  // Ensure motor is initially disabled
  digitalWrite(EN_PIN, LOW);

}

// Function to rotate motor clockwise
void rotateClockwise(int steps) {
  // Enable the driver
  digitalWrite(EN_PIN, HIGH);
  
  // Set up for clockwise rotation
  digitalWrite(IN1_PIN, LOW);
  digitalWrite(IN2_PIN, HIGH);
 
  // Step the motor
  for (int i = 0; i < steps; i++) {
    // Alternate between IN1 and IN2 to create stepping
    digitalWrite(IN1_PIN, High);
    delayMicroseconds(3000);
    digitalWrite(IN1_PIN, Low);
    delayMicroseconds(3000);
  }
  
  // Disable the driver
  digitalWrite(EN_PIN, LOW);
}

// Function to rotate motor counter-clockwise
void rotateCounterClockwise(int steps) {
  // Enable the driver
  digitalWrite(EN_PIN, HIGH);
  
  // Set up for counter-clockwise rotation
  digitalWrite(IN1_PIN, LOW);
  digitalWrite(IN2_PIN, HIGH);
  
  // Step the motor
  for (int i = 0; i < steps; i++) {
    // Alternate between IN2 and IN1 to create stepping
    digitalWrite(IN2_PIN, HIGH);
    delayMicroseconds(3000);
    digitalWrite(IN2_PIN, LOW);
    delayMicroseconds(3000);
  }
  
  // Disable the driver
  digitalWrite(EN_PIN, LOW);
}

void loop() {
  // Rotate clockwise for one full revolution
  Serial.println("Rotating Clockwise");
  rotateClockwise(STEPS_PER_REVOLUTION);
  delay(1000);
  
  // Rotate counter-clockwise for one full revolution
  Serial.println("Rotating Counter-Clockwise");
  rotateCounterClockwise(STEPS_PER_REVOLUTION);
  delay(1000);
}

Thanks and Regard

Manish Ruhela

  • Hi Manish,

    Thanks for contacting us.

    See below snippet from the datasheet. For one direction ignore the red arrows and provide IN1 and IN2 as indicated. To drive the motor in the opposite direction swap the signals to IN1 and IN2. What you had for IN1 must be input to IN2 and vice versa. This will spin the stepper in the opposite direction.

    Regards, Murugavel 

  • Hi Murugavel,

    Thanks for your quick response. I analyze this snippet from the datasheet. I write the logic accordingly but still not getting counter clockwise run the motor, I am confusing or may be do something wrong. 

    Can you Please share some demo code snippet It's very helpful for us.

    Thank & Regard

    Manish Ruhela

  • Hi Manish,

    The DRV8421BEVM includes this stepper motor demo, see User's Guide page-7. The EVM firmware runs in a TI MSP430 MCU which can be downloaded from this webpage. See below.

    Regards, Murugavel