Stepper Motor Acceleration & Speed Control with Arduino

0
793

 In this Arduino Stepper motor tutorial, we use the Accelstepper library to demonstrate stepper motor acceleration rate control. Nema 17 is used as stepper motor and L298N IC is used for stepper motor controller. Arduino code is provided to illustrate how to use the Accelstepper library to set and control acceleration rate for stepper motor. Using this Arduino stepper motor acceleration/deceleration library you can create many electronics applications. There are also many other functionality of Accelstepper libbrary such as speed and position control. Besides this we can configure the stepper motor drive mode, that is, full drive mode and half drive mode is supported by this stepper motor library.

To drive stepper motor without library see the tutorials:

 – Arduino Stepper Motor Control using L298N

Half Drive Stepper Motor Control using Arduino

 Precaution:

The website https://electricianworldnews.com has been stealing content without consent from this ee-diary
weblog(http://ee-diary.blogspot.com) and therefore information there
may not be accurate, so for original content go to Stepper Motor Acceleration & Speed Control with Arduino.

Arduino Stepper Motor Interfacing & Schematic Drawing

The following shows interfacing of Arduino with L298N stepper motor controller, Nema 17 stepper motor and diodes on breadboard.

20210821_193844-9420804

The following video demonstrates stepper motor acceleration & speed control with Arduino.

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

2-7498152

 Stepper Motor Acceleration Control Arduino code

The following Arduino program code rotates the stepper motor at a specific acceleration in 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 is 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 than full step drive. Unlike in 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 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 of that of full drive mode.

//Program for acceleration rate control for Stepper Motor with L298N & Arduino
//Source: https://ee-diary.blogspot.com

#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)+"tt");
//print RPM
Serial.print(String(rpm,2)+"tt");
//print Acceleration
Serial.print(String(a,2)+"ttt");
//print Speed
Serial.println(String(v));
}
stepper.run(); //move to new position
}

The following picture shows the output on serial monitor.

1-3861828

Other stepper motor tutorials are:

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