Arduino Tutorial: Stepper Motors and Arduino (Work in Progress)

I recommend you read the excellent Adafruit Stepper Motor Tutorial either before or after reading this tutorial as they compliment each other.

Overview: What are stepper motors, and how do unipolar motors differ from bipolar motors?

What are stepper motors and how do they differ from servo motors and brushed or permanent magnet DC motors (so-called “regular” motors)?

Permanent magnet (PM) DC motors are easy to turn get spinning, only slightly more difficult to control speed and direction, can run at high speeds, and are relatively cheap. However it is impossible to know where a motor is in it’s rotation, or to stop at a particular place.

Stepper motors are more complicated to get moving and don’t turn as quickly as PMDC motors. However they can be very accurately positioned, often to within a degree of rotation. Stepper motors are invaluable for robotics and CNC projects.

Bipolar, Unipolar and Universal stepper motors

Unipolar motors require that the polarity of the voltage on their coils is always the same. Coils are either energized or not.

In contrast, the voltage on bipolar motors can be reversed. Coils are typically always energized, in one polarity or another. This means that bipolar motors use more power, but provide more torque.

Universal motors can be used either way.

Circuits and Software

Circuits and software for unipolar motors

For low current unipolar motors, the ULN2003 is a popular chip. It consists of 7 Darlington transistors and can provide up to 500 mA at up to 50 V. It can be used on a breadboard, or with a module such as this from Oddwires. Note this is not a shield, and you are responsible for wiring up the connections from this module to your Arduino.

Unipolar code:

int Pin0 = 10; 
int Pin1 = 11; 
int Pin2 = 12; 
int Pin3 = 13; 
 
void setup() 
{ 
  pinMode(Pin0, OUTPUT);  
  pinMode(Pin1, OUTPUT);  
  pinMode(Pin2, OUTPUT);  
  pinMode(Pin3, OUTPUT);  
} 
 
void loop() 
{ 
  allOff();
  digitalWrite(Pin0, HIGH);  
  delay (100);
 
  allOff();
  digitalWrite(Pin1, HIGH);  
  delay (100);
 
  allOff();
  digitalWrite(Pin2, HIGH);  
  delay (100);
 
  allOff();
  digitalWrite(Pin3, HIGH);  
  delay (100);
}
 
void allOff()
{ 
   digitalWrite(Pin0, LOW);  
   digitalWrite(Pin1, LOW); 
   digitalWrite(Pin2, LOW); 
   digitalWrite(Pin3, LOW); 
}

Circuits and software for bipolar stepper motors

If your stepper motor uses less than 40 mA, you can connect it directly to Arduino. For instance the 21-02485 from Symbol Technologies is a tiny motor, often available on eBay and surplus electronic sights, which seems to use less than 30mA. Plug the 4 wires directly into 4 pins on your Arduino, and simply run:

// very simple stepper motor test program
// for 21-02485 bipolar motor connected  
// directly to Arduino outputs
 
// Michael Shiloh 10/15/15
// This program is in the public domain 
 
// which color wire goes to which pin 
const int YELLOW = 9;
const int RED = 8;
const int WHITE = 7;
const int BLUE = 6;
 
void setup()  {
  pinMode(YELLOW, OUTPUT);
  pinMode(RED, OUTPUT);
  pinMode(WHITE, OUTPUT);
  pinMode(BLUE, OUTPUT);
}
 
void loop() {
    digitalWrite(YELLOW, LOW);
    digitalWrite(RED, HIGH);
    digitalWrite(WHITE, LOW);
    digitalWrite(BLUE, HIGH);
    delay(100);
    
    digitalWrite(YELLOW, LOW);
    digitalWrite(RED, HIGH);
    digitalWrite(WHITE, HIGH);
    digitalWrite(BLUE, LOW);
    delay(100);
    
    digitalWrite(YELLOW, HIGH);
    digitalWrite(RED, LOW);
    digitalWrite(WHITE, HIGH);
    digitalWrite(BLUE, LOW);
    delay(100);
    
    digitalWrite(YELLOW, HIGH);
    digitalWrite(RED, LOW);
    digitalWrite(WHITE, LOW);
    digitalWrite(BLUE, HIGH);
    delay(100);
}

For low current (about 600 mA) bipolar stepper motors, the L293D (or 754410*) is a handy dual H-bridge controller. (*Note that the 754410 doesn’t have the reverse voltage protection (flyback diodes) that the L293D has.) Adafruit has an execllent tutorial on how to use the L293D.

For slightly higher current (about 1.5A), the L298 is a good choice. However it will not fit in to a solderless breadboard due to its unusual footprint, and so it’s often easier to buy it pre-mounted as a module, such as this one from Oddwires. Note this is not a shield, and you are responsible for wiring up the connections from this module to your Arduino. Functionally, it is very similar to using the L293D and thus you can apply what you learned from the Adafruit L293D tutorial.

Higher current bipolar motors require higher current circuits. For instance the TB6560 can handle 2.5 or 3.5 amps, depending on the chip package. Instead of working with the chip by itself, it might be easier to purchase a controller based on this chip, e.g. this one from SainSMART. Here is an Instructable for using what looks like exactly the same controller (but I’m not sure) with Arduino.

Here is my code for using a controller based on the TB6560, which includes a potentiometer for controlling the speed:

// Code to drive the "Microstep Driver TB6560"
// 
// 10/14/15 - Michael Shiloh: Initial entry
 
 
const int dirPin = 2;
const int enablePin = 3; 
const int pulsePin = 4;
const int potPin = A0;
 
const int minDelay = 400;
const int maxDelay = 10000;
const int margin = 10;
 
void setup() {
  pinMode(dirPin, OUTPUT);
  pinMode(enablePin, OUTPUT);
  pinMode(pulsePin, OUTPUT);
 
  // disable controller
  // Note that HIGH is disabled!!
  digitalWrite(enablePin, HIGH);
 
  // Later this might move to the loop
  // and expect control from outside
  // e.g. grasshopper or switches or
  // something like that:
  
  // enable motor
  digitalWrite(enablePin, LOW);
  
  // arbitrarily pick a direction
  digitalWrite(dirPin, HIGH);
 
  Serial.begin(9600);
}
 
void loop() {
  int delayMicros = map(analogRead(potPin), 0, 1023, maxDelay, minDelay);
  //Serial.println(delayMicros);
 
  if (delayMicros < (maxDelay-margin)) {
    // move one step
    digitalWrite(pulsePin, HIGH);
    delayMicroseconds(delayMicros);
    digitalWrite(pulsePin, LOW);
    delayMicroseconds(delayMicros);
  }
}

Often these more elaborate drivers automatically control the current to the motor, so that the motor and the driver are protected from overheating and other damage.

Random information that might be useful in working with stepper motor

  1. It is normal for stepper motors to get quite hot during operation.
  2. The words driver and controller are sometimes used to reference both the electronic circuit that is delivering the current to the motor, as well as the software that is telling the circuit what to do.
  3. The greater the current, the hotter the motor and the electronic circuit
  4. The greater the load on the motor, the greater the current
  5. Increasing the supply voltage to a motor will cause it to consume more current (and therefore increase the heat)
  6. The more continuously a motor is used, the hotter the motor and the circuitry gets. Intermittent use allows things to cool down. The ratio of busy time to rest time is called the duty cycle. If you sleep 8 hours a day and work the rest of the time you have a 67% duty cycle.

Can your driver/controller handle the motor you have chosen?

If your stepper motor uses more current than your stepper motor circuit can handle, the circuit will overheat, not the motor. If you try to move something heavier than the motor can handle, the motor will overheat, not the circuit.

The way to determine if your circuit is overheating is simply to place your fingers on the  ICs or transistors. They will feel warm. If you can comfortably leave your fingers in place, they are not overheating. If you have to jerk your fingers away from the heat, they are too hot.

If your ICs or transistors are overheating, you can try to cool them down by helping them get rid of heat. You can simply aim a fan at them, but it will be much more efficient if you can give them more space to radiate heat from by attaching a heat sink. Try Googling “adding a heat sink to a motor controller”. Basically any piece of aluminum making firm contact with the ICs will help draw the heat out. For best results use some heat sink compound in between the aluminum and the ICs to assure good heat conduction.

Note that this will only help up to a point. If you can not keep your circuitry cool enough then you will have to change your circuit to one capable of handling more current.

Considerations when using a motor controller shield with Arduino

If you are using an external power supply be aware that some shields deliver this voltage to the Arduino, and Arduino can only handle a limited voltage. Most shields provide a way to isolate the two in case your motor voltage is much higher than the Arduino voltage. For instance, on the Arduino motor controller shield there is a small trace you can cut to isolate the two. The Adafruit motor controller shield has a jumper you can remove.

Some common stepper motors are designed for a very low voltage, e.g. 1 or 2 volts. Many common stepper motor controllers that are designed to work with Arduino will not work with voltages lower than about 4.5 volts. You’ll have to study the datasheet for the ICs used on your motor controller to determine if this is the case for you.

You can operate a motor at a higher voltage, but it will run hotter. Steps to mitigate this problem include using the motor with a light load; cooling the motor with a fan and/or heat sink; running the motor intermittently i.e. allowing it to cool before running it again. The ratio of running time to cooling time is called the duty cycle.

Resources

  • A good tutorial on the stepper motor controller designed and sold by Arduino.cc is this Instructable
  • For a more theoretical discussion of stepper motors see this PDF on the Solarbotics website

 

Print Friendly
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Leave a Reply

Your email address will not be published. Required fields are marked *