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