// 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); }