0% found this document useful (0 votes)
238 views9 pages

Controlling Stepper Motor Via Arduino

this document deals with controlling stepper motor with the help of arduino. the pdf also contains the necessary software for the arduino

Uploaded by

Manoj Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
238 views9 pages

Controlling Stepper Motor Via Arduino

this document deals with controlling stepper motor with the help of arduino. the pdf also contains the necessary software for the arduino

Uploaded by

Manoj Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 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.
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 - Normal Step


When the current flows to energise each coil in turn. The motor steps one step each time one of the coils is
energised. All of the coils must be energised one after the other to achieve a full rotation of the rotor.

This drive mode is very economical and provides smooth motion and a good level of torque.

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 - Double Step


This drive mode utilises two consecutive coils for each step. It is not as economical as the previous mode because
two coils must be energised at one time, but this method provides double the amount of torque.

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
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.

Step Number Step Position Coil 1 Coil 2 Coil 3 Coil 4

1 0.0 ON OFF OFF OFF

2 0.5 ON ON OFF OFF

3 1.0 OFF ON OFF OFF

4 1.5 OFF ON ON OFF

5 2.0 OFF OFF ON OFF

6 2.5 OFF OFF ON ON

7 3.0 OFF OFF OFF ON

8 3.5 ON OFF OFF ON


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. This is where it
gets a bit complicated because there are different methods of micro-stepping. Energy in waves for each phase is
shifted 90 degrees to each other which results in a very smooth drive action for the motor. As the ratio of current is
shifted lower on the first coil and higher on the second coil, the rotor moves towards the second coil. This method is
used when step vibration needs to be eliminated, however, it requires more energy than the other drive modes.

The example given below is for stepping in a bipolar mode:

Step Step Coil_1 Coil_2


Number Position Current Current

1 0 0.0% 100%

2 1/8 19.5% 98.1%

3 1/4 38.2% 92.4%

4 3/8 55.5% 83.1%

5 1/2 70.7% 70.7%

6 5/8 83.1% 55.5%

7 3/4 92.4% 38.2%

8 7/8 98.1% 19.5%

9 1 100% 0.0%

A good explanation of this can be found here on the How to Mechatronics Youtube channel.

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.

Below is the L298N module in detail:


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.
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

// initialize the stepper library on pins 8 through 11:


Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
void setup() {
// set the speed at 60 rpm:
myStepper.setSpeed(60);
// initialize the serial port:
Serial.begin(9600);
}
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);
}

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

How Stepper motors work https://www.youtube.com/watch?v=bngx2dKl5jU

6-Wire Stepper motor https://www.youtube.com/watch?v=hHe4Fc6uuBs

Wiring Schematics http://www.linengineering.com/resources/wiring-connections/

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.

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, 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.

Two Wire Setup (wire 2 and 3 from the table above).

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
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;
}

You might also like