Nema 17 Stepper Motor Speed and Direction Control with Arduino

Nema 17 stepper motor speed and direction control with Arduino is illustrated. The stepper motor is rotated half of the rotation in clockwise and counter clockwise direction. In each rotation, the speed of the stepper motor is increased using increasing value of RPM(Revolution Per Minute). L298N motor driver is used as stepper motor controller. Arduino code for stepper motor speed and direction control is provided. Video demonstration of the implemented code is provided at the end.

Stepper.h library is used in this example demonstration. For stepper motor control without library in full drive mode see Arduino Stepper Motor Control using L298N and for half drive mode see Half Drive Stepper Motor Control using Arduino. L298N motor driver is used in this demonstration, recommended motor driver is A4988 stepper motor driver. For tutorial on using A4988 stepper motor driver with Arduino see Arduino Stepper Motor Tutorial. 

 

The following picture shows the interfacing of Arduino with Nema 17 stepper motor, L298N and diodes.

interfacing of Arduino with Nema 17 stepper motor, L298N

The following shows circuit drawing of Arduino with Nema 17 stepper motor, L298N motor driver and 1N4004 diodes.

circuit drawing of Arduino with Nema 17 stepper motor, L298N motor driver

Arduino code for Stepper Motor Speed and Direction Control

The following is Arduino code for controlling the speed and direction of stepper motor using stepper.h libary.



//Program for speed and direction control for Stepper Motor with L298N & Arduino
//Source: https://ee-diary.blogspot.com

#include <Stepper.h>

//define pins for stepper motor
int en = 10;
int in1 = 9;
int in2 = 8;
int in3 = 7;
int in4 = 6;
int Tsteps = 200; //total steps for Nema 17 is 200

//define stepper motor total steps and inputs
Stepper stepper(Tsteps, in1, in2, in3, in4);

int dir = 1; //rotation direction
int Trevs;  //revolution time
float t, r; //time for revolution and number of revolution

void setup(){
  pinMode(en, OUTPUT);  
  digitalWrite(en, HIGH);
  Serial.begin(9600); //Serial output baud rate
  Serial.println("Stepper Motor RPM(speed), No.of Revolution & Time required for Revolution");
}

void loop(){
    // increase motor speed from 2 to 32 rpm
    for (int s = 2; s<100; s=s+4){
        stepper.setSpeed(s); //set motor speed
        dir = -dir; // change direction of rotation
        Trevs = millis(); // set start time (ms)
        stepper.step(dir * Tsteps/2); //move number of 1/2 of total steps(200)
        Trevs = millis()-Trevs; //time for half revolution(ms)
        delay(500); //time delay of 0.5secs
        t = Trevs/1000.0; //time required to move steps
        r = s*t/60.0; //number of revolutions
        
        //Outputs
        Serial.println("-----------Outputs------------\n");
        //print RPM
        Serial.println("RPM:"+String(s));
        //print number of revolutions
        Serial.println("No.of Revol.:"+String(r,2));
        //print time required for revolution
        Serial.println("Time Reqd.:"+String(t)+" secs");
        Serial.print("\n"); 
    }

}

In the above Arduino program code, we have included the <Stepper.h> library which is included in the Arduino IDE installation. This stepper library requires us to define the number of steps and the pins used for the stepper motor. The total number of steps(Tsteps) is 200 for Nema 17 stepper motor. The pins used are 9, 8, 7 and 6 whose alias defined are en1, en2, en3 and en4 respectively. Pin 10 of Arduino is used enable input en to the L298N two full H-bridge drivers. dir, Trevs, t and s are variables required down in the code for direction specification, calculate and print RPM(speed), number of revolution and time required for revolution on the Serial Monitor.

In the loop() function we have used incremental RPM value s from 2 to 100. This sets up the speed of rotation of the stepper motor. In each RPM value, we calculate the number of revolution and speed of revolution.

The following shows the output generated on the serial monitor.


The following video demonstrates Nema 17 Stepper Motor Speed and Direction Control with Arduino.

 The following video shows simulation of the stepper motor control using Arduino in Proteus electronics design software.


 

Other Stepper motor tutorials are as follows:

- Stepper Motor Control with Motor Shield and Arduino

- Stepper Motor control using ATmega32

- Stepper Motor Control using ATmega328p

- How to control Stepper Motor using ATmega32 and L293D IC


Post a Comment

Previous Post Next Post