Difference between revisions of "Stepper Motor + Arduino"

From ESE205 Wiki
Jump to navigation Jump to search
m (Ethanshry moved page Controlling Stepper Motors with Arduino to Stepper Motor + Arduino: Fall 2018 Wiki Reworks)
 
(7 intermediate revisions by 2 users not shown)
Line 6: Line 6:
  
 
The motor used in our project (Finding Feanor) was a NEMA 17 bipolar stepper. We used an ITEAD Dual Stepper Motor Driver Shield for Arduino that used two A3967 microstepping driver chips to control the steppers.
 
The motor used in our project (Finding Feanor) was a NEMA 17 bipolar stepper. We used an ITEAD Dual Stepper Motor Driver Shield for Arduino that used two A3967 microstepping driver chips to control the steppers.
 +
 +
[https://www.itead.cc/wiki/Arduino_Dual_Step_Motor_Driver_Shield This] wiki page details the motor shield and its use. The motor wires can be inserted into the drive sockets and tightened with a small screwdriver or connected to the board using the 4 male headers adjacent to the drive sockets.
  
 
== Arduino ==
 
== Arduino ==
Line 11: Line 13:
 
The Arduino has a convenient library called stepper.h that allows for the control of the motors with several pre-written functions. The complete documentation of the library can be found [https://www.arduino.cc/en/Reference/Stepper here].
 
The Arduino has a convenient library called stepper.h that allows for the control of the motors with several pre-written functions. The complete documentation of the library can be found [https://www.arduino.cc/en/Reference/Stepper here].
  
An important thing to note about the step() function is that it is a blocking function. This means that if step(50) is called, the code will not move to the next line until all 50 steps have been completed. Thus, for the illusion of smooth movement with two motors, it would be necessary to increment one step at a time in rapid succession.
+
An important thing to note about the [https://www.arduino.cc/en/Reference/StepperStep step()] function is that it is a blocking function. This means that if step(50) is called, the code will not move to the next line until all 50 steps have been completed. Thus, for the illusion of smooth movement with two motors, it would be necessary to increment one step at a time in rapid succession.
  
 
=== Example Code ===  
 
=== Example Code ===  
Line 20: Line 22:
 
#include <Stepper.h>
 
#include <Stepper.h>
 
void setup() {
 
void setup() {
    // put your setup code here, to run once:
 
 
     Serial.begin(9600);
 
     Serial.begin(9600);
 
     pinMode(2, OUTPUT);
 
     pinMode(2, OUTPUT);
 
     pinMode(3, OUTPUT);
 
     pinMode(3, OUTPUT);
    TBStepper.setSpeed(200);
+
     String readStr = "";                         // A string to store the bits entered into the serial monitor
    Stepper myStepper(stepsPerRevolution, 2, 3); // Create the stepper object
+
    myStepper.setSpeed(200);
     String readStr = ""; // A string to store the bits entered into the serial monitor
 
 
     const int stepsPerRevolution = 200;
 
     const int stepsPerRevolution = 200;
 
     int stepCount = 0;
 
     int stepCount = 0;
 +
    Stepper myStepper(stepsPerRevolution, 2, 3); // Create the stepper object
 
}
 
}
  
 
void loop() {
 
void loop() {
    // Manually controlling the stepper motor to find what step ranges work
+
                                                // Manually controlling the stepper motor to find what step ranges work
 
     while ( Serial.available() != 0) {
 
     while ( Serial.available() != 0) {
         char c = Serial.read();           //reads in the first available bit from serial monitor
+
         char c = Serial.read();                 //reads in the first available byte from serial monitor
 
         readStr += c;
 
         readStr += c;
 
     }
 
     }
  
     int n = readStr.toInt();           //converts readStr to an integer value n
+
     int n = readStr.toInt();                   //converts readStr to an integer value n
  
 
     if (n != 0) {
 
     if (n != 0) {
Line 45: Line 46:
 
         Serial.print("steps: ");
 
         Serial.print("steps: ");
 
         Serial.println(stepCount);
 
         Serial.println(stepCount);
         n = 0;                           //Resets values of n and readStr
+
         n = 0;                                 //Resets values of n and readStr
        readStr = "";
+
        readStr = "";
 
     }
 
     }
 
}
 
}
 
</source>
 
</source>
 +
 +
Here, the <tt>stepper</tt> object is defined to have 200 steps per rotation, and be controlled by pins 2 and 3 (which correspond to the wires on the X drive socket on the stepper motor shield). Next, the speed is defined by the <tt>setSpeed()</tt> method. The loops checks to see if anything has been entered into the serial monitor, and if it has, it reads it and converts it to an integer. Finally, the code uses the <tt>step(n)</tt> command to increment the stepper <tt>n</tt> steps and update the <tt>stepCount</tt> variable accordingly.
  
 
[[Category:HowTos]]
 
[[Category:HowTos]]
 +
[[Category:Arduino]]
 +
[[Category:Electronics]]

Latest revision as of 03:24, 17 August 2018

Hardware

There are two basic kinds of stepper motor: unipolar and bipolar. A unipolar stepper motor consists of one coil, or winding, per motor phase with a center tap through which current can flow. If one section of the coils is powered, the motor is in one phase. Power the other section and the motor will switch phase. In this way, the current will not need to switch polarity to operate the motor. This leads to a much simpler control circuit.

A bipolar motor will use one winding per phase. In order to switch motor phase, the polarity of the current going through the windings needs to be reversed. This leads to a more complicated method of controlling the motor.

The motor used in our project (Finding Feanor) was a NEMA 17 bipolar stepper. We used an ITEAD Dual Stepper Motor Driver Shield for Arduino that used two A3967 microstepping driver chips to control the steppers.

This wiki page details the motor shield and its use. The motor wires can be inserted into the drive sockets and tightened with a small screwdriver or connected to the board using the 4 male headers adjacent to the drive sockets.

Arduino

The Arduino has a convenient library called stepper.h that allows for the control of the motors with several pre-written functions. The complete documentation of the library can be found here.

An important thing to note about the step() function is that it is a blocking function. This means that if step(50) is called, the code will not move to the next line until all 50 steps have been completed. Thus, for the illusion of smooth movement with two motors, it would be necessary to increment one step at a time in rapid succession.

Example Code

This simple arduino sketch allows the user to input a number of steps in the serial monitor for the attached stepper motor to increment.

#include <Stepper.h>
void setup() {
    Serial.begin(9600);
    pinMode(2, OUTPUT);
    pinMode(3, OUTPUT);
    String readStr = "";                         // A string to store the bits entered into the serial monitor
    myStepper.setSpeed(200);
    const int stepsPerRevolution = 200;
    int stepCount = 0;
    Stepper myStepper(stepsPerRevolution, 2, 3); // Create the stepper object
}

void loop() {
                                                // Manually controlling the stepper motor to find what step ranges work
    while ( Serial.available() != 0) {
        char c = Serial.read();                 //reads in the first available byte from serial monitor
        readStr += c;
     }

     int n = readStr.toInt();                   //converts readStr to an integer value n

     if (n != 0) {
         myStepper.step(n);
         stepCount = stepCount + n;
         Serial.print("steps: ");
         Serial.println(stepCount);
         n = 0;                                 //Resets values of n and readStr
         readStr = "";
     }
}

Here, the stepper object is defined to have 200 steps per rotation, and be controlled by pins 2 and 3 (which correspond to the wires on the X drive socket on the stepper motor shield). Next, the speed is defined by the setSpeed() method. The loops checks to see if anything has been entered into the serial monitor, and if it has, it reads it and converts it to an integer. Finally, the code uses the step(n) command to increment the stepper n steps and update the stepCount variable accordingly.