Controlling Stepper Motor Via Arduino
Controlling Stepper Motor Via Arduino
In the previous lesson we learned how to control the speed and direction of two DC motors using a dual H-bridge
device such as a motor driver module or shield. In this lesson we are going to look at how we would use these drivers
to control the speed and direction of a stepper motor.
Stepper motors are complex systems that require controlled driving via a dedicated driver unit or microcontroller.
Stepper motors have maximum torque at low speeds, so they are very good for use in systems that require high
torque precise positioning such as 3D printers, photocopiers, and CNC machines.
A stepper may have several coils, but these coils are connected within a group called a phase. A motor may have 4
coils, but only 2 phases, etc.
Below is a description of the difference between Unipolar and Bipolar stepper motors from Adafruit Learning. This is
an excellent tutorial that will give you a good understanding of how stepper motors work.
Unipolar drivers, always energize the phases in the same way. One lead, the "common" lead, will always be
negative. The other lead will always be positive. Unipolar drivers can be implemented with simple transistor
circuitry. The disadvantage is that there is less available torque because only half of the coils can be
energized at a time.
Bipolar drivers use H-bridge circuitry to actually reverse the current flow through the phases. By energizing
the phases with alternating the polarity, all the coils can be put to work turning the motor.
Wire configurations
Stepper motors have different numbers of wires depending on the number of phases and type of motor.
The most common are 4-wire, 5-wire, 6-wire, and 8-wire. The differences for these configurations is explained in the
Adafruit Learning tutorial.
In this lesson we will be focussing on a 6-wire unipolar hybrid stepper motor, and we will be driving it in the Bi-Polar
mode. This means that we can disregard the two common (centre tap) wires because they are not needed when
driving this type of motor in this mode.
This drive mode is very economical and provides smooth motion and a good level of torque.
1 0 ON ON OFF OFF
2 2 OFF ON ON OFF
3 4 OFF OFF ON ON
4 6 ON OFF OFF ON
Step Motor Drive Mode - Half Step
In this drive mode one coil is energised for one step and then left on whilst the consecutive coil is energised for the
next step, then that coil remains energised for a step and remains on whilst the next consecutive coil is energised for
a step, and so on. This sequence means that the rotor is pulled to align with the stator coil on the first half step, and
then is pulled to the middle position between the two energised coils on the next half step. This drive mode is a bit
of a combination of the previous two drive modes. It consumes more energy than that of the previous modes
because the coils need to remain energised for each half step, however, this method provides a higher level of
precision because the step degree is halved, therefore providing double the amount of steps per revolution.
1 0 0.0% 100%
9 1 100% 0.0%
A good explanation of this can be found here on the How to Mechatronics Youtube channel.
Instructions:
Unplug your Arduino from the USB port!
Wire up your Arduino and your L298N module as shown in the image above.
Please note: The two jumper connectors for pins ENA and ENB on the L298N module should be placed on.
The L298N module gives us the choice of powering the Arduino via the computer USB port or powering the
Arduino via the L298Ns +5V outlet. This is handy especially for robotics projects where you dont want
your Arduino connected to cables, and where you only want to use one power source.
You do need to be careful here because it is not a good practice connect a V in power supply when you
are drawing power from the USB port, and you must take care to ensure that the polarity is the correct
way around, and carefully choose whether you will use the +5V pin or Vin depending on your supply
voltage.
In this lesson we will be powering our Arduino Uno via the USB cable which is attached to our computer.
Double check to ensure that your battery polarity is the correct way around make sure
that the positive side of the battery is not connected to ground
Browse to locate the Stepper_oneRevolution sketch, open it in the Arduino IDE and then
plug your Arduino into the USB port and upload the sketch.
Open the Serial Monitor and you should see the following printout and be able to
observe the corresponding action of your stepper motor.
The code for this sketch can be found below and also in the stepper library under examples in the Arduino IDE.
#include <Stepper.h>
const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution
// for your motor
The adjusted code for the KEYES Motor Shield can be found below:
#include <Stepper.h>
const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution
// for your motor
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 11, 12, 13);
int ENA = 9;
int ENB = 10;
void setup() {
// set the speed at 60 rpm:
myStepper.setSpeed(60);
// initialize the serial port:
Serial.begin(9600);
pinMode(ENA, OUTPUT);
pinMode(ENB, OUTPUT);
// with the KEYES L298 Shield I needed to call enable pins high in order for the stepper to work.
digitalWrite(ENA, HIGH);
digitalWrite(ENB, HIGH);
}
void loop() {
// step one revolution in one direction:
Serial.println("clockwise");
myStepper.step(stepsPerRevolution*1);
delay(500);
// step one revolution in the other direction:
Serial.println("counterclockwise");
myStepper.step(-stepsPerRevolution*1);
delay(500);
}
References:
http://www.robot-and-machines-design.com/en/Articles/Mechanics/Tips-Guides/556-Motor-Selection-How-To-
Select-A-Motor-Motor-Types.html
https://learn.adafruit.com/adafruit-arduino-lesson-16-stepper-motors/overview
https://arduino-info.wikispaces.com/StepperMotors
Lesson 9C Controlling a Stepper Motor with Two Wires Written by Seven
Vinton
In the first part of this lesson we learnt how to control a stepper motor using the L298N Dual H-Bridge module with
four control pins from the Arduino (usually pins 8,9,10,11). If you have more than one stepper motor to control, then
you will find that you quickly begin to run out of pins. Luckily the digital signals outputting from the Arduino are
comprised in such a way that we can set up a simple circuit to cut our control wires down from four to two.
In the following table you will see that the signals of each pair of wires are the inverse of each other for each step.
Therefore, we can produce the input for IN1 and IN4 directly from the two digital Arduino pins 8 and 9, and then
produce the outputs needed at IN2 and IN3 by supplying a digital pulse which is the inverse of the voltage on pin 8
and 9.
1 high low
2 low low
3 low high
4 high high
The signals in the Two Wire setup will provide all of the signals in the Four Wire setup if we use the following
circuit:
Schematic View
In this circuit we see that a HIGH pulse at Control 1 and a LOW pulse at Control 2 will produce a HIGH signal at IN1
and a LOW signal at IN4, and at the same time it will produce a LOW signal at IN2 and a HIGH signal at IN3. This will
be the case for each step in the sequence because the pulse that is applied to the base leg of each of the two
transistors enables the inverse switching of the signal. This circuit setup is called a NOT gate or Inverter.
For this circuit I have used two 2N2222 transistors which are NPN general purpose low-power switching transistors,
but you can use any similar NPN transistors (this is the same transistor that was used in lesson 6).
In the layout below the +5V logic needed for the transistor circuit is supplied from the +5V out on the H-Bridge
module. The emitter leg of each transistor is connected to GND, the base leg of each transistor is connected via a 1K
resistor to the Arduino signal wire (pin 8 for one, and 9 for the other) and the IN pin of the L298N module (IN1 for
one, and IN4 for the other). The collector leg of each resistor is connected to the remaining IN pin (IN2 for one, and
IN3 for the other) and +5V via a 10K resistor.
Layout View
The part of the code in the Stepper Library <Stepper.h> which performs the sequencing of the signals, exists in the
.cpp file.
void Stepper::stepMotor(int thisStep)
{
if (this->pin_count == 2) {
switch (thisStep) {
case 0: /* 01 */
digitalWrite(motor_pin_1, LOW);
digitalWrite(motor_pin_2, HIGH);
break;
case 1: /* 11 */
digitalWrite(motor_pin_1, HIGH);
digitalWrite(motor_pin_2, HIGH);
break;
case 2: /* 10 */
digitalWrite(motor_pin_1, HIGH);
digitalWrite(motor_pin_2, LOW);
break;
case 3: /* 00 */
digitalWrite(motor_pin_1, LOW);
digitalWrite(motor_pin_2, LOW);
break;
}