Soft starting a DC motor using timers

Article-series: Arduino, using DC motors #2

When controlling DC motors, sometimes there is a need for a sudden change in speed (for example, starting from 0% to 100% power or changing speed to the opposite). But this mode of engine operation requires very high currents - several times more than simple movement. If, for example, when rotating at a constant speed, the motor consumes a current of about 500 mA, then at the moment of starting this value can reach 2-3 A. Because of this, it is necessary to use a more powerful power supply subsystem and controller.

The problem of inrush currents can be solved by gradually increasing the speed. Those. Instead of instantaneous acceleration, the motor will accelerate gradually, while smoothing out the peak current consumption at the moment of starting.

Let's connect the motor to the motor-shield on the L298P meringue, as in the previous example: Connecting a DC motor to Arduino via the L298P driver

Do not forget that the motor does not have a feedback connection, so to control the current speed we use the additional variable motorPower

byte E1=5; // Motor speed control - connection to output 5

byte I1=4; // Control the direction of rotation - connect to output 4

unsigned long StartTimer; // Timer for soft start

int StartTimeStep=2; // Engine power change interval, in ms

int StartPowerStep=1; // One step change in engine power

int motorPower; // Engine power

void setup()

{

pinMode(E1, OUTPUT); // Set the operation of the corresponding pins as outputs

pinMode(I1, OUTPUT);

motorPower=0; // Initial power - 0

}

void loop()

{

digitalWrite(I1, HIGH); // Pin I1 is set to a high logic level, the motor rotates in one direction

for (motorPower=0;motorPower<255;motorPower+=StartPowerStep) // Increase the speed until we reach the maximum

{

analogWrite(E1, motorPower); // At the ENABLE pin a control signal with a new speed

delay(StartTimeStep);

}

}

The engine now accelerates more smoothly. Accelerating from 0 to 255 will take almost half a second, and setting the change interval to 1 ms will generally take a quarter of a second. The difference is not very noticeable to the naked eye. But such overclocking is much more gentle on the power unit. In addition, we can adjust the acceleration speed to achieve the desired acceleration.

But the use of delay() does not allow using parallel actions, so we implement a soft start using timers, as with smooth movement of servos.

byte E1=5; // Motor speed control - connection to output 5

byte I1=4; // Control the direction of rotation - connect to output 4

unsigned long StartTimer; // time counter for soft start

int StartTimeStep=2; // Engine power change interval, in ms

int StartPowerStep=1; // One step change in engine power

int motorPower; // Engine power

void setup()

{

pinMode(E1, OUTPUT); // Set the operation of the corresponding pins as outputs

pinMode(I1, OUTPUT);

motorPower=0; // Initial power - 0

}

void loop()

{

digitalWrite(I1, HIGH); // Pin I1 is set to a high logic level, the motor rotates in one direction

if (motorPower<255) // Increase the speed until we reach the maximum

if ((millis()-StartTimer)>= StartTimeStep) // Check how much has passed since the last speed change

// if more than the specified interval, increase the speed by one more step

{

motorPower+= StartPowerStep; // increase speed

analogWrite(E1, motorPower); // At the ENABLE pin a control signal with a new speed

StartTimer=millis(); // Start of a new step

}

}

Now the engine accelerates smoothly, and in parallel with acceleration, you can perform any other actions

Еще:

Подключаем двигатель постоянного тока. Микросхема L298P (Arduino, использование двигателей постоянного тока #1)
Плавный пуск двигателя постоянного тока с использованием таймеров (Arduino, использование двигателей постоянного тока #2)
Создание класса для работы с двигателем постоянного тока (Arduino, использование двигателей постоянного тока #3)
Создание собственной библиотеки управления двигателем постоянного тока (Arduino, использование двигателей постоянного тока #4)