ad space

Stepper Motor Acceleration & Speed Control with Arduino

In this Arduino Stepper motor tutorial, it is shown how one can control the acceleration and speed of a stepper motor using Arduino Uno. Nema 17 is used as the stepper motor and L298N IC is used as a stepper motor controller. Here we will use the accelstepper library to demonstrate stepper motor acceleration rate control. Arduino program code is provided which shows how to use the accelstepper library to set and control the acceleration rate for the stepper motor. There are other stepper motor libraries for Arduino but the accelstepper library provides much more functionality such as speed control, position control, acceleration/deceleration control, multiple simultaneous steppers motor control, 2, 3 and 4 wire steppers, plus 3 and 4 wire half steppers control and API based functionalities.

Arduino Stepper Motor Interfacing & Schematic Drawing

The following shows the circuit schematic drawing of the interfacing of Arduino, Nema 17 stepper motor, L298N and diodes.

Stepper Motor Arduino Potentiometer circuit diagram

In the above circuit diagram, a 10KOhm potentiometer is connected to the A0 analog pin of the Arduino Uno. This potentiometer is used in this tutorial to control the accceleration and deacceleration of the stepper motor. Arduino Uno receives the varying voltage from the A0 pin and converts it to maximum and minimum speed. To drive the Nema 17 stepper motor with sufficient current and power and for directional control we need to use motor controller like L298N or L293D. Here L298N motor driver is used. Following shows a picture of L298N motor driver IC and the pin-out diagram.

L298N motor driver IC
L298N motor driver IC pins diagram

The L298N motor driver is interfaced to the Arduino Uno as shown in the above circuit diagram. Internally, the L298N has two full bridge drivers. 

Enable pins

The pin 10 of Arduino is connected to the enable pins, ENA and ENB, of both the full bridge drivers. 

Input pins

The two input of the first full bridge driver IN1 and IN2 are connected to Arduino pins 9 and 8 respectively. Similarly, the two inputs of the second full bridge driver IN3 and IN4 are connected to Arduino pins 7 and 6 respectively. There are two sense pins, SenseA and SenseB, on the L298N IC. They are for current sensing purpose which is not required here and therefore both are grounded. 

Voltage Supply pins

There are two seperate voltage pins in the L298N driver IC. One is the Vcc which should be connected to +5V(with Arduino). The another is the Vss pins which is meant for power supply to the motor. Vss is connected to +12V supply which is the voltage required by the Nema 17 stepper motor. The ground pin should connected to the common ground, that is, negative terminal of the power source. The Arduino ground pin should also be connected to the same ground.

Output pins

The outputs from the L298N are connected to diode protection circuits as shown in the above circuit diagram. The OUT1 and OUT2 which is the output of the first full bridge driver is connected to first pair of Nema7 inputs. Similarly the OUT3 and OUT4, the output of the second full bridge driver is connected to second pair of Nema7 inputs. 

The following shows Nema 17 stepper motor and its terminals.

nema17 stepper motor

 

bipolar stepper motor terminal

 The following shows picture shows how Arduino Uno is connected to the L298N stepper motor driver, Nema 17 stepper motor and the 1N40007 protection diodes using breadboard.

Arduino Nema 17 Stepper motor

Stepper Motor Acceleration Control Arduino code

The following Arduino program code rotates the stepper motor at a specific acceleration in a clockwise direction for one revolution and then reverses the direction of rotation. Here when the stepper motor moves from one position +p to position 0 and then to position –p then it is called rotation. p is half the number of steps in a revolution, which is 100 for full-step and 200 for half-step. In the stepper motor code below, the maximum rotor speeds for full drive mode are set at 500 steps/second and 1000 steps/second for half drive mode. Although the maximum speed is different for the two-mode the RPM(Revolution Per Minute) is the same because the half-step drive has twice the number of steps per revolution as the full-step drive. Unlike in the stepper library used in the tutorial Nema 17 Stepper Motor Speed and Direction Control with Arduino where the speed is given by RPM, the speed is measured in steps/s in the AccelStepper library. But we can use the following formula to convert speed in RPM to motor speed in steps/s:

 

RPM × steps/revolution = 60 × motor speed

 

In the example stepper motor code, the acceleration rate used in full drive mode is 500 steps/s^2 and 1000 steps/s^2 for half drive mode because the number of steps per revolution is twice that of full drive mode.

//Program for acceleration rate control for Stepper Motor with L298N & Arduino
//Source: https://www.ee-diary.com/2021/08/stepper-motor-acceleration-speed.html


#include <AccelStepper.h>


//define pins for stepper motor
int en = 10;
int in1 = 9;
int in2 = 8;
int in3 = 7;
int in4 = 6;

int fullstep = 4; //number of stages in full drive
int halfstep = 8; //number of stages in half drive
int stepdrive = fullstep; //select step drive mode

//define stepper motor with step mode and inputs
AccelStepper stepper(stepdrive, in1, in2, in3, in4);

int steps = (stepdrive/4)*200; //number of steps per revolution
long last = 0;
int lag = 500; //time (ms) interval for display
int dir = 1; //direction of rotation
float rpm, v, oldspeed, a;
int nsteps;

void setup(){
  Serial.begin(9600); // define Serial output baud rate
  pinMode(en, OUTPUT);
   digitalWrite(en, HIGH);
   stepper.setMaxSpeed((stepdrive/4)*500); //max speed 500(steps/s) or 1000(steps/s)
   stepper.setAcceleration(800); //acceleration rate(steps/s^2)
    Serial.println("Outputs:");
    Serial.println("                           (+ve=clockwise, -ve=counter clockwise)");
   Serial.println("No. of Steps      RPM        Acceleration(m/s^2)       Speed(m/s)");
    Serial.println("-------------------------------------------------------------------\n");
}

void loop() {
   stepper.moveTo(dir*steps/2); //move to position ±100 or ±200
   if(stepper.distanceToGo()==0) 
   dir = -dir;  //change direction of rotation
   if(millis()>last + lag) //lag time elapsed since last print
      {
      v = stepper.speed(); //get motor speed (steps/s)
      nsteps = v*lag/pow(10,3); //No. of steps taken during lag time
      rpm = 60.0*v/steps; //RPM
      a = (v - oldspeed)*1000.0/lag; //Acceleration
      oldspeed = v; //update speed value
      last = millis(); //update last print time

      
        //Outputs
        //print No. of Steps
        Serial.print(String(nsteps)+"\t\t");
        //print RPM
        Serial.print(String(rpm,2)+"\t\t");
        //print Acceleration
        Serial.print(String(a,2)+"\t\t\t");
        //print Speed
        Serial.println(String(v));
      }
   stepper.run(); //move to new position
}

The following picture shows the output on serial monitor.

Stepper Motor Acceleration & Speed Control with Arduino

Video Demonstration

Watch the following video that demonstrates how the Arduino controls Nema 17 stepper motor acceleration and speed with Arduino.

So in this way we can control the acceleration, de-acceleration, and speed of a stepper motor like Nema 17 using Arduino Uno with L298N motor driver and accelstepper library.

To drive stepper motor without library see the tutorials:

 - Arduino Stepper Motor Control using L298N

- Half Drive Stepper Motor Control using Arduino

Post a Comment

Previous Post Next Post