In the last part of my tutorial about the A4988 driver, I’m going to explain how to build a controller to adjust speed and rotation direction of a stepper motor.
I used the same setup introduced in a previous post:
- an Arduino Uno;
- a LCD Keypad shield;
- a Pololu A4988 driver mounted on a breadboard;
- a NEMA17 stepper motor.
Using the controller, you can adjust the speed (from 0 to 70 RPM, revolutions per minute) and the rotation direction.
On the LCD are displayed the actual speed, direction and a progress bar:
The 5 available buttons are used to control the motor:
- UP and DOWN adjust the speed;
- LEFT and RIGHT adjust the direction;
- SELECT activates the emergency stop (speed = 0).
To achieve a continuous rotation, your sketch must send the step commands to the Pololu driver at constant frequency.
Let’s see an example: for a speed of 60RPM and for a motor with 200 steps / revolution, Arduino must send to the driver (60*200)/60 = 200 commands / sec or a command every 5ms.
To ensure an accurate timing, I’m going to use the interrupts generated by the Timer1 as explained in this article.
From the Playground you can download a convenient library to configure that Timer:
#include [...] Timer1.initialize(100); Timer1.attachInterrupt(timerIsr);
With the instructions above, you configure the Timer1 to trigger an interrupt every 100 millisecond (0.1ms) and to invoke, at each interrupt, the timerIsr() function.
The “temporal distance” between a step command and the next one depends on the current speed (in the example above for a speed of 60RPM the distance is 5ms). Given that the possible speeds are only 15 (from 0 to 70RPM with a 5RPM increment), I calculated that distance (in ticks of 0.1ms) for the different speeds and stored the values in an array:
Read More: Allegro A4988 and Arduino (3)