We use own and third party cookies to improve our services and show related advertising to your preferences by analyzing your browsing habits. If you continue browsing, we consider that you accept their use. Can change the settings or get more information here

Ver sitio en español Go to homepage Contact me
martes, 01 de noviembre de 2016

Stepper motor controller with Arduino

A stepper motor is a kind of motor that, instead of rotating continuously in one direction, allows rotate in very small steps and stop and change direction very quickly, without problems of inertia. This makes these engines very suitable for mounting them in devices which perform movements that required high precision. In this article I will show how to build a simple controller to handle one of these motors through the Arduino board, along with a sample program written in CSharp that allows you to operate the motor from the computer where the plate is connected.

In this link you can download the source code and executable of the StepByStepMotorArduino project, written with Visual Studio 2013.

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

Arduino mega board
Arduino mega board

There are many types of stepper motors. That which I will use in this article is a unipolar one, which is the easiest to control, since it is only necessary to activate in an order determined the different coils for the motor to move in the desired direction.

The motor I have used is recycled. I got it from an old hard drive, on a PC that had nothing less than the processor 8086, but it is similar to others that can be found in the market without problems.

Stepper motor
Stepper motor

The problem in this case is to find out which cables polarize the coils and what the common wire is. This is pretty simple; you only need a multimeter to measure the resistance between each pair of wires. Resistance will be the same between any two coil terminals, but it will be around one half between one of these terminals and the common one.

In this case, I have a common terminal and four cables that connect to each coil. To find out the order in which we polarize these wires, simply connect the common terminal to ground and keep trying to passing a current pulse through the other wires until you get four movements in the same direction. In the case of this motor, I used 12V, which is standard in this type of engine.

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

Motor controller board
Motor controller board

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

Board circuit scheme
Board circuit scheme

In the board you can see five transistors. In this example I only need 4, so I only show the scheme for this amount, the board also allows you to move motors with 5 terminals; all you have to do is to add another switch to the scheme.

The inputs S1 to S4 are connected to 4 output pins of the Arduino board. I used the pins 22, 24, 26 and 28, but you can use those you want instead.

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 is to indicate the direction of the movement, with bit 0 to 1 to move in one direction and bit 1 to 1 to move in the opposite. The two bits can be passed to 1 at time, in which case the engine will make some steps in one direction and then return to the starting point.

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

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

The stepper motor conntrol application
The motor control application

As the number of steps can only be between 1 and 255, since it is a byte, I use a function to get the value or use a default value of 1 if an incorrect value is written:

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 pass them on 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();

Notice that the port BaudRate property is set to 9600 in both the program in the Arduino board and that in the PC program.

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

Share this article: Share in Twitter Share in Facebook Share in Google Plus Share in LinkedIn
Comments (0):
* (Your comment will be published after revision)

E-Mail


Name


Web


Message


CAPTCHA
Change the CAPTCHA codeSpeak the CAPTCHA code