Stepper Motor Controller With Arduino

A stepper motor is a type of motor that,(w3circuit.blogspot.com) instead of continuously rotating in one direction, allows very small turns and stop and change direction very quickly, without inertia problems. This makes these motors very suitable for assemblies where precision movements are required. In this article I will show how to build a simple driver to drive one of these motors through the Arduino board, along with an example program written in CSharp that allows to move the motor from the computer where we connect the board.

The model of Arduino board that I have used is the Arduino MEGA, which provides a large number of inputs and outputs, although you can use any other model of the board connected to the computer with a USB connection.

Mega Arduino Board
Mega Arduino Board

There are many types of engines step by step, which I will use in this article is the unipolar type, which is the easiest to control, since it is only necessary to activate in a certain order the different coils so that the motor is scroll in the desired direction.

The engine that I used is recycled. I have taken it from an old hard drive, from a PC that carried the 8086 processor nothing less, but it is similar to others that can be found in stores without problems.

Stepper motor
Stepper motor

The problem in this case is to find out which are the cables that polarize the coils and which is the common cable. This is quite simple, you only need a multimeter to measure the resistance between each pair of cables. The resistance will be the same between two coil terminals, but it will be half between one of these terminals and the common terminal.(w3circuit.blogspot.com)

In this case, I have a common terminal and four cables that connect to each coil. To find out the order in which we must polarize these cables, simply connect the common terminal to ground and try to pass a pulse of current through the other cables until we get four movements in the same direction followed. In the case of this engine, I used 12V, which is the most normal in this type of engines.

As the work of control and timing will be done through software, either on the Arduino board, or in the PC program, all we need is a series of switches that allow us to control the 12 volts needed by the motor with 5 volts of the Arduino outputs. This is the plate that I have mounted:

Motor control plate
Motor control plate


Each switch is composed of a 2N 2222 transistor, a BA 158 diode and a 1K resistor, as simple as that. This is the scheme of the plate:

Motor control plate
Motor control plate

On the board you can see 5 transistors. In this example we only need 4, so I only show the scheme for this amount, the board also allows moving 5-terminal engines, the only thing to do is add another switch to the scheme.

The inputs S1 to S4 will be connected to 4 output pins of the Arduino board. I used pins 22, 24, 26 and 28 of mine, but this is indifferent.



The program for the Arduino board is as follows:

int pin1 = 22;
int pdir = LOW;

void setup () {
  // Initialize pins
  for (int ix = 22; ix <= 28; ix + = 2) {
    pinMode (ix, OUTPUT);
    digitalWrite (ix, LOW);
  }
  Serial.begin (9600);
}

void loop () {
  if (Serial.available ()> 1) {
    int val = Serial.read ();
    int vt = Serial.read ();
    if (val & 1) {// move in one direction
      for (int ip = 0; ip <vt; ip ++) {
        digitalWrite (pin1, HIGH);
        delay (100);
        digitalWrite (pin1, LOW);
        pin1 + = 2;
        if (pin1> 28) {
          pin1 = 22;
        }
      }
    }
    if (val & 2) {// move in the other direction
      for (int ip = 0; ip <vt; ip ++) {
        digitalWrite (pin1, HIGH);
        delay (100);
        digitalWrite (pin1, LOW);
        pin1 - = 2;
        if (pin1 <22) {
          pin1 = 28;
        }
      }
    }
  }
}

In the serial port we will write two bytes, the first one will be to indicate the direction of the movement, with bits 0 to 1 to move in one direction and bit 1 to 1 to move in the opposite direction. The two bits can be passed to 1, so the motor would sweep in one direction and return to the starting point.(w3circuit.blogspot.com)

The second byte contains the number of steps we want the motor to advance. To take a step, we send a 100 millisecond pulse to the current pin and move to the next pin. As simple as this.

The program in which in turn controls the Arduino board from the computer is also very simple, it has a text box to indicate the port to which the board is connected, another to indicate the number of steps we want to advance and a button for each address:

The application to control the engine
The application to control the engine

Since the number of steps can only be between 1 and 255, since it is a byte, I use a function to obtain the value or use the value 1 in case of writing an incorrect value:

private byte GetSteps ()
{
        int steps;
        if (int.TryParse (txtSteps.Text, out steps))
        {
                if (steps> 255)
                {
                        steps = 255;
                }
                if (steps <1)
                {
                        steps = 1;
                }
        }
        else
        {
                steps = 1;
        }
        txtSteps.Text = steps.ToString ();
        return (byte) steps;
}

In the Click event handler of the buttons, the data is written to the serial port to be passed to the Arduino board:

SerialPort p = new SerialPort (txtCOM.Text);
p.BaudRate = 9600;
p.Open ();
byte [] val = new byte [2] {1, GetSteps ()};
p.Write (val, 0, 2);
p.Close ();

Note that the BaudRate property of the port is set to 9600 in both the Arduino board program and the PC program.(w3circuit.blogspot.com)

If you want to see the complete assembly where I have used this circuit, you can follow this link where I explain how to build a robotic airsoft turret.