Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

A working example of a stepper motor

OK, enough of theories. Let’s get to the real case. As an example, I took standard 42mm size stepper motor with a 1.8 deg/step. It’s model number is 103-546-5342. It requires 0.6A in unipolar mode and 0.42 in bipolar –
perfect for our Arduino motor shield.

As we can see connect motor as bipolar connecting Orange and Blue wires to M1 and Red with Yellow to M2 connector. White and Black we can leave unconnected.

Now load Arduino program from Examples->AFmotor->StepperTest:

1 #include <AFMotor.h>
2 // Motor with 200 steps per rev (1.8 degree)
3 // to motor port #1 (M1 and M2)
4 AF_Stepper motor(200, 1);
5 void setup() {
6 // set up Serial library at 9600 bps
7 Serial.begin(9600);
8 Serial.println("Stepper test!");
9 motor.setSpeed(50); // 50 rpm
10 }
11 void loop() {
12 Serial.println("Single coil steps");
13 motor.step(200, FORWARD, SINGLE);
14 motor.step(100, BACKWARD, SINGLE);
15 delay(100);
16 Serial.println("Double coil steps");
17 motor.step(200, FORWARD, DOUBLE);
18 motor.step(100, BACKWARD, DOUBLE);
19 delay(100);
20 Serial.println("Interleave coil steps");
21 motor.step(200, FORWARD, INTERLEAVE);
22 motor.step(100, BACKWARD, INTERLEAVE);
23 delay(100);
24 Serial.println("Micrsostep steps");
25 motor.step(200, FORWARD, MICROSTEP);
26 motor.step(100, BACKWARD, MICROSTEP);
27 delay(100);
28 }

As we can see we have created a stepper motor object with 200 steps per revolution motor attached to 1 connector:

1 AF_Stepper motor(200, 1);

Then in setup, we set motor speed to 50rpm with the following command:

1 motor.setSpeed(50);

And the fun begins by driving motor with step() command using several modes:

Its parameters are simple first number indicates the number of steps to make, second rotation direction (FORWARD, BACKWARD) and finally stepping mode (SINGLE, DOUBLE, INTERLEAVE, and MICROSTEP). Let’s go briefly
what each stepping mode means.

SINGLE – is a single coil stepping (sometimes referred to Wave Drive). It drives motor by energizing one coil at the time. This is not common usage, but handy where power saving is required. You have less torque in this
mode.
DOUBLE – is when two coils are energized (Full Stepping). This gives full torque of the motor.

INTERLEAVE – it is half stepping, when coil pairs are energized simultaneously. So you get double resolution. In our case instead 200 (1.8º) steps per revolution, but 400 (0.9º). Speed is also two times slower.

MICROSTEP – this mode is widely used in many applications as it ensures smooth motor drive. Instead of driving coils with DC signals they are driven with PWM. It provides a smooth transition between steps. Micro-
stepping is excellent for reducing mechanical noise, smooth rotation and avoid resonances. But not to increase resolution more as micro-steps won’t ensure enough holding torque and you lose accuracy due to motor
friction.

There is one more thing to try – stepping with acceleration and deceleration. To try these let’s download another library called AccelStepper. It still requires Afmotor library. So be sure to have them both.

Here is a simplified example of one motor we use:

1 #include <AccelStepper.h>
2 #include <AFMotor.h>
3 // two stepper motors one on each port
4 AF_Stepper motor1(200, 1);
5 // you can change these to DOUBLE or INTERLEAVE or MICROSTEP!
6 // wrappers for the first motor!
7 void forwardstep1() {
8 motor1.onestep(FORWARD, DOUBLE);
9 }
10 void backwardstep1() {
11 motor1.onestep(BACKWARD, INTERLEAVE);
12 }
13 // Motor shield has two motor ports, now we'll wrap them in an AccelStepper object
14 AccelStepper stepper1(forwardstep1, backwardstep1);
15 void setup()
16 {
17 stepper1.setMaxSpeed(300.0);
18 stepper1.setAcceleration(100.0);
19 stepper1.moveTo(500);
20 }
21 void loop()
22 {
23 // Change direction at the limits
24 if (stepper1.distanceToGo() == 0)
25 stepper1.moveTo(-stepper1.currentPosition());
26 stepper1.run();
27 }

Using this library, we can drive stepper motor by accelerating its rotation to maximum speed for zero. It requires a little bit of preparation before the run as we need to set max speed, acceleration and final position (steps to
make). Where is this feature useful?
One of the biggest reason would be inertia. If you rotate stepper at high speed with some load, it can overshoot when stopped. Acceleration ensures that no steps will be missing and no overshooting will occur. And this is
healthy for any mechanical system not to have sharp movements.

You might also like