You are on page 1of 9

Lesson 9 – Controlling a Stepper Motor – Written by Seven Vinton

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.

What is a stepper motor?
A stepper motor is a type of DC motor that by way of its design can be made to move in precise steps. Within these
types of motors there are several ‘tooth’ end coils (stator) which when energised act as electromagnets to attract
the teeth of the central shaft ‘gear’(rotor). The slight offsetting of teeth on each electromagnet means that as each
electromagnet is energised in turn, the motor shaft steps forward (or backward).

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.

Different types of stepper motors
There are several different variations of stepper motor, and each variation has a different set up of coils, rotors, and
wires.

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.

Normal Step When the current flows to energise each coil in turn. This drive mode is very economical and provides smooth motion and a good level of torque.Double Step This drive mode utilises two consecutive coils for each step. All of the coils must be energised one after the other to achieve a full rotation of the rotor. The motor steps one step each time one of the coils is energised. but this method provides double the amount of torque.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. Stepper motors can be driven in four different ways:  Normal Steps  Double Steps  Half Steps  Micro-Steps Step Motor Drive Mode . Step Number Step Position Coil 1 Coil 2 Coil 3 Coil 4 1 0 OFF OFF OFF ON 2 1 OFF OFF ON OFF 3 2 OFF ON OFF OFF 4 3 ON OFF OFF OFF Step Motor Drive Mode . It is not as economical as the previous mode because two coils must be energised at one time. Step Number Step Position Coil 1 Coil 2 Coil 3 Coil 4 1 0 ON ON OFF OFF 2 2 OFF ON ON OFF 3 4 OFF OFF ON ON 4 6 ON OFF OFF ON .

0 OFF ON OFF OFF 4 1.0 OFF OFF OFF ON 8 3. and then is pulled to the middle position between the two energised coils on the next half step.5 ON OFF OFF ON . this method provides a higher level of precision because the step degree is halved.5 OFF OFF ON ON 7 3.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.5 ON ON OFF OFF 3 1. It consumes more energy than that of the previous modes because the coils need to remain energised for each half step.0 ON OFF OFF OFF 2 0.0 OFF OFF ON OFF 6 2. however. therefore providing double the amount of steps per revolution.Step Motor Drive Mode . and so on. then that coil remains energised for a step and remains on whilst the next consecutive coil is energised for a step. Step Number Step Position Coil 1 Coil 2 Coil 3 Coil 4 1 0.5 OFF ON ON OFF 5 2. This sequence means that the rotor is pulled to align with the stator coil on the first half step. This drive mode is a bit of a combination of the previous two drive modes.

2% 92.Step Motor Drive Mode – Micro-Step The micro-step drive mode utilises current in the form of a ‘Sine Wave’ to energise the motor coils.2% 8 7/8 98.0% A good explanation of this can be found here on the ‘How to Mechatronics’ Youtube channel.5% 7 3/4 92. however. it requires more energy than the other drive modes. Our Lesson Example: In our lesson we use the L298N Dual H-Bridge module to drive our unipolar hybrid stepper motor in bipolar mode using the full step drive mode. This is where it gets a bit complicated because there are different methods of micro-stepping. This method is used when ‘step vibration’ needs to be eliminated.7% 6 5/8 83.1% 3 1/4 38.1% 5 1/2 70. The example given below is for stepping in a bipolar mode: Step Step Coil_1 Coil_2 Number Position Current Current 1 0 0.4% 4 3/8 55. Energy in waves for each phase is shifted 90 degrees to each other which results in a very smooth drive action for the motor. the rotor moves towards the second coil.4% 38.1% 55.7% 70.5% 83. Below is the L298N module in detail: .5% 9 1 100% 0.0% 100% 2 1/8 19.5% 98. As the ratio of current is shifted lower on the first coil and higher on the second coil.1% 19.

Below is the circuit layout that we will use for this lesson: Instructions: Unplug your Arduino from the USB port! Wire up your Arduino and your L298N module as shown in the image above. 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. 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. Please note: The two jumper connectors for pins ENA and ENB on the L298N module should be placed on. . and you must take care to ensure that the polarity is the correct way around. In this lesson we will be powering our Arduino Uno via the USB cable which is attached to our computer. and where you only want to use one power source. This is handy especially for robotics projects where you don’t want your Arduino connected to cables. and carefully choose whether you will use the +5V pin or Vin depending on your supply voltage. Open the Serial Monitor and you should see the following printout and be able to observe the corresponding action of your stepper motor. The L298N module gives us the choice of powering the Arduino via the computer USB port or powering the Arduino via the L298N’s +5V outlet. open it in the Arduino IDE and then plug your Arduino into the USB port and upload the sketch.

} The adjusted code for the KEYES Motor Shield can be found below: .begin(9600). } void loop() { // step one revolution in one direction: Serial. delay(500).println("counterclockwise"). // initialize the serial port: Serial. myStepper. // step one revolution in the other direction: Serial. delay(500). #include <Stepper. void setup() { // set the speed at 60 rpm: myStepper.step(-stepsPerRevolution*1). // 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.h> const int stepsPerRevolution = 200.setSpeed(60).The code for this sketch can be found below and also in the ‘stepper’ library under ‘examples’ in the Arduino IDE. 11).step(stepsPerRevolution*1). 9.println("clockwise"). myStepper. 8. 10.

8. 13).println("clockwise").youtube. 12. // 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.com/resources/wiring-connections/ https://learn. void setup() { // set the speed at 60 rpm: myStepper. // step one revolution in the other direction: Serial. digitalWrite(ENB. pinMode(ENA. OUTPUT). 11. digitalWrite(ENA. myStepper.youtube. pinMode(ENB.com/StepperMotors . } References: http://www. // with the KEYES L298 Shield I needed to call enable pins high in order for the stepper to work. int ENB = 10.html How Stepper motors work https://www. int ENA = 9. } void loop() { // step one revolution in one direction: Serial. delay(500).#include <Stepper.step(stepsPerRevolution*1).h> const int stepsPerRevolution = 200.begin(9600).setSpeed(60). HIGH).step(-stepsPerRevolution*1).adafruit. HIGH).println("counterclockwise").com/adafruit-arduino-lesson-16-stepper-motors/overview https://arduino-info.com/watch?v=bngx2dKl5jU 6-Wire Stepper motor https://www.linengineering. myStepper.com/en/Articles/Mechanics/Tips-Guides/556-Motor-Selection-How-To- Select-A-Motor-Motor-Types. // initialize the serial port: Serial. OUTPUT).wikispaces.robot-and-machines-design. delay(500).com/watch?v=hHe4Fc6uuBs Wiring Schematics http://www.

In the following table you will see that the signals of each pair of wires are the inverse of each other for each step.9. 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. Step wire 1 wire 4 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 .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. Four Wire Setup Step wire 1 wire 2 wire 3 wire 4 1 High low high low 2 low high high low 3 low high low high 4 high low low high Therefore.11). we can produce the input for IN1 and IN4 directly from the two digital Arduino pins 8 and 9. Two Wire Setup (wire 2 and 3 from the table above). If you have more than one stepper motor to control. 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.10. then you will find that you quickly begin to run out of pins.

For this circuit I have used two 2N2222 transistors which are NPN general purpose low-power switching transistors. 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. void Stepper::stepMotor(int thisStep) { if (this->pin_count == 2) { switch (thisStep) { case 0: /* 01 */ digitalWrite(motor_pin_1. LOW). digitalWrite(motor_pin_2. case 1: /* 11 */ digitalWrite(motor_pin_1. The emitter leg of each transistor is connected to GND. break. digitalWrite(motor_pin_2. the base leg of each transistor is connected via a 1K resistor to the Arduino signal wire (pin 8 for one. and at the same time it will produce a LOW signal at IN2 and a HIGH signal at IN3. HIGH). and IN3 for the other) and +5V via a 10K resistor. LOW). exists in the . case 2: /* 10 */ digitalWrite(motor_pin_1. Layout View The part of the code in the ‘Stepper Library’ <Stepper. In the layout below the +5V logic needed for the transistor circuit is supplied from the +5V out on the H-Bridge module. HIGH).cpp file. break. HIGH). and IN4 for the other). digitalWrite(motor_pin_2. This circuit setup is called a NOT gate or Inverter. } . break. break. HIGH).h> which performs the sequencing of the signals. The collector leg of each resistor is connected to the remaining IN pin (IN2 for one. case 3: /* 00 */ digitalWrite(motor_pin_1. and 9 for the other) and the IN pin of the L298N module (IN1 for one.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. LOW). LOW). digitalWrite(motor_pin_2. but you can use any similar NPN transistors (this is the same transistor that was used in lesson 6).