# Step Motor With Acceleration and End of Stroke

68

3

Posted

## Introduction: Step Motor With Acceleration and End of Stroke

Acceleration of the step motor and the limit switch. These issues that we are going to discuss in this article, in which I will present you with a motion test assembly. In addition to the concept of end switches, I will also demonstrate an example of stepper acceleration code.

Can I handle stepper motor without accelerating? Yes I can. Just leave the speed constant. So what is the advantage of acceleration? Well, when you mount a router or a CNC machine, they have a weight. Imagine you pushing a spindle of 3cv, 4cv ... Imagine the heavy mechanical structure ... So the engine needs to get out of zero speed and this is gradually increasing because this is good for the health of the equipment. If you do not accelerate and slow down, what happens? You're going to stomp on the mechanism. This takes a hit when the stepper motor is turned on. Imagine that all the micro parts of the mechanism are being hit, millions and millions of times ... This is damaging all the equipment. So, let me warn you: 3d printer, router, router, everything that is CNC machines works with acceleration and deceleration. Hence the importance of this subject.

Today, therefore, I will speak of acceleration and deceleration, with a very simple example, but soon I will record a video showing how to mount a CNC machine step by step.

I know there are basically two types of people who are interested in stepper motors. There are those who want to know how to program a stepper motor because they want to make a robot or a disconnecting machine, for example. As this has no design and no software ready, you will need to know these concepts of acceleration, among others, to assemble this type of equipment. There are still those people who already want to take the steps ready and just follow the recommended, as a kind of cake recipe. In this case, it is possible to take my classes, for example, and follow each phase until finalizing an assembly.

## Step 1: Resources Used

For assembly today we use this list of components:

• Trapezoidal spindle with 8mm diameter and 2mm pitch

• Trapezoidal spindle with 8mm diameter and 8mm pitch

• Flange nut for 8x2 spindle

• Flange nut for 8x8 spindle

• Bearing for 8 mm diameter spindles

• 10mm diameter cylindrical linear guide

• Cylindrical roller bearings for 10mm guides

• Holders for 10mm cylindrical guides

• NEMA 17 Motors

• Shaft Couplings

• Arduino Mega

• Driver DRV8825

• Display Nokia 5110

• Various plastic parts

• Bolts and nuts

• Wood base

• External power supply 12V

• Infrared Sensors

## Step 2: Demonstration

Our assembly today is very similar to that of the Arduino Uno Video with Spindle and Stepper Motor.

What did I need to change? This time I chose an Arduino Mega to use the end of course switch, which you can see on the right side of the project, since the end of travel switch is used in the fastest spindle. Each menu is explained in the source code of this program which is in the ATmega. In the demonstration made in the video we make the engine move 5 centimeters to make evident the acceleration and the deceleration. It then returns 15 centimeters and touches the end of stroke switch, which stops the engine.

## Step 3: End of Course Switch

What is it for? Limit switches are mechanical elements that limit a route by stopping the movement of the engines. They are used in the assembly of machines to protect the mechanics. In CNC machines, the keys also serve as a reference point for, for example, zeroing the coordinates of an axis. There are several types of keys in the market, among them: buttons, infrared sensors, capacitive sensors, among others.

In our assembly we use infrared sensors, as shown in the image above.

## Step 4: Sample Code

Here I have a source code on the arduino that shows Setup and how to move the stepper motor with the end of stroke switch and acceleration. Here is the lib StepMotor, which is a library that I made, which manipulates the 8825, 4988 and TB6600 drivers, and allows you to play larger engines like the Nema 23 or 34, for example.

We then configure the driver pins, the steps per millimeter and around, as well as the microspeed. I also configure the throttle movement with maximum and minimum speed and activate the engine power output ports. Finally, the limit switch pins are activated.

```#include <StepMotor.h>
DRV8825 d1;
void setup() {
//Configuracao dos pinos do driver
d1.pinConfig(2,3,4,5,6,7,8,9);

//configuracao de passos por milimetro
d1.stepPerMm(100);

//configuracao de passos por volta
d1.stepPerRound(200);

//configuracao de micropasso
d1.stepConfig(1);

// tempos em uSegundos
// Aceleração é o decremento de tempo a cada novo passo
d1.motionConfig(20,1500,5000);

//reseta o driver
d1.reset();

//ativa as portas saídas de potencia do motor
d1.enable(HIGH);

//Pinos das chaves de fim de curso e se ativam em nivel alto ou baixo
// d1.endstopConfig(min,max,HIGH ou LOW);
d1.endstopConfig(10,11,HIGH);

}```

Below is the example of the engine code. The moverMoveTo command is responsible for the movement.

```void loop() {  //move o motor 100mm em direcao crescente
d1.motorMoveTo(100, HIGH);
//aguarda 1 segundo
delay(1000);

//move o motor 100mm em direcao decrescente
d1.motorMoveTo(100, LOW);
//aguarda 1 segundo
delay(1000);
}```

## Step 5: End of Course Switch

Code: In our code we will act directly on the motor movement, disabling it when the limit switch is activated.

```bool DRV8825::canGo(bool direction){

if(LVL) // se: as chaves de fim de curso ativam em nível alto
{

if(direction && !digitalRead(EMAX)) //se: move positivamente E o fim de curso máximo NÃO está ativado
return true;

if(!direction && !digitalRead(EMIN)) //se: NÃO move positivamente E o fim de curso mínimo NÃO está ativado
return true;

}
else //senão: as chaves de fim de curso ativam em nível baixo
{

if(direction && digitalRead(EMAX)) //se: move positivamente E o fim de curso máximo está ativado
return true;

if(!direction && digitalRead(EMIN)) //se: NÃO move positivamente E o fim de curso mínimo está ativado
return true;

}
return false;

}```

## Step 6: Acceleration

The steps of the motors are controlled with high and low pulse between a time interval.

In order for the engine not to start directly at maximum speed, we use acceleration.

The acceleration changes the pulse time at each step to change the engine speed, gradually increasing to the maximum speed to accelerate, and decreasing until it stops to decelerate.

Acceleration can be implemented in several ways.

The code we created exemplifies the use of acceleration in stepper motors.

## Step 7: Acceleration - Code

Simple example of acceleration code.

In this code the pulse time is decreased at each step with a value chosen by the user until halfway or until the engine reaches the maximum speed, ie the shortest pulse time set.

```void DRV8825::motorMoveTo(double distance, bool direction) //(distância, direção, tempo da largura do pulso em microssegundos (metade do tempo em nível alto, metade em nível baixo)){

digitalWrite(DIR, direction);
long accSpeed = SMIN;
long steps = distance*SPM;
long stepsToStop;
bool flag = true;

for(long i = 0; i < steps; i++){

if(i < (steps)/2){

if(accSpeed>SMAX){

if(canGo(direction)){
digitalWrite(STP, HIGH);
delayMicroseconds(accSpeed/2);
digitalWrite(STP, LOW);
delayMicroseconds(accSpeed/2);
}
else{
delayMicroseconds(accSpeed);
}
accSpeed = accSpeed - ACC;
}```

At this point the amount of pulses that the motor has performed to reach the maximum speed in that path is stored.

```else   {
if(flag){
stepsToStop = i;
flag = false;
}

if(canGo(direction)){
digitalWrite(STP, HIGH);
delayMicroseconds(SMAX/2);
digitalWrite(STP, LOW);
delayMicroseconds(SMAX/2);
}
else{
delayMicroseconds(SMAX);
}
}

}```

This amount will be the same amount used to decelerate the engine before it reaches the end of its course.

To decelerate the engine the pulse time is increased with each step until the engine stops.

```else   {
if(i < steps-stepsToStop){

if(canGo(direction)){
digitalWrite(STP, HIGH);
delayMicroseconds(SMAX/2);
digitalWrite(STP, LOW);
delayMicroseconds(SMAX/2);
}
else{
delayMicroseconds(SMAX);
}
}
else
{
if(canGo(direction)){
digitalWrite(STP, HIGH);
delayMicroseconds(accSpeed/2);
digitalWrite(STP, LOW);
delayMicroseconds(accSpeed/2);
}
else{
delayMicroseconds(accSpeed);
}
accSpeed = accSpeed + ACC;
}

}
}
}```

## Recommendations

• ### Large Motors Class

7,827 Enrolled

• ### Pocket-Sized Contest

We have a be nice policy.