Feedback!

L293 Stepper Motor Breakout

Views: 16344 Difficulty: 2 Status: Complete
Stepper_breaker

This L293D Stepper motor controller lets you precisely control a stepper motor with just two signal lines.

Stepper motors are powerful and precise. Good tutorial with circuits and library documentation available from the Arduino site. Steppers can be annoying to control and take up four control wires if you don't use transistors. This breakout board, available at our store, lets you control a bipolar stepper motor, like this one available from Sparkfun with just two control lines. It has two transistors on board so the extraneous control lines, which are always the opposite of the other two, can be eliminated. It also has capacitors to help ensure your stepper runs smooth and doesn't disrupt the rest of your circuit.

PCB Backside

Screen_shot_2013-01-17_at_10.07.29_pm
The back of the PCB hooked up to the stepper

Video of the L293 Breakout Board Conrolling a Stepper Motor

In this video we show the speed and direction control mode where spinning the potentiometer changes the speed and direction of the motor is spinning. If the pot is all the way to the right then the motor is going at max speed clockwise, if the pot is all the way to the left the pot goes max speed counter-clockwise. If the pot is precisely in the middle then the motor does not move. We also demonstrate the angle control mode where the motor moves around a circle according to where the potentiometer is pointing.

Step-by-Step Instructions

Step 0 Get the Breakout Board

First get the L293D breakout board at the LucidTronix store. You will also need a bi-polar stepper motor. You can get one at spark fun, or salvage one from an old printer or scanner. You will also need an Arduino or other micro-controller. Lastly, you need a 12V power supply for the stepper motor. We use a drill battery in the video above, you can also use 8 AA batteries (8*1.5V = 12V), or 12V wall wort.

Step 1 Wires or Headers?

Decide how to hook up to the breakout board. The board comes with male headers that you can solder in making it really easy to use in a solder-less breadboard. This is a good choice if you want to experiment a lot with your setup. On the other hand if you know exactly how you want to use your stepper motor, you can solder hook up wires straight to the breakout board. At minimum you will need to connect the 4 wires to the stepper motor, two wires to the micro-controller, 5V and ground from the microcontroller and 12V and ground from the motor's power supply.

Step 2 Get the Wires Straight

The bi-polar stepper motor comes with four wires. Each wire is connected to one other through a motor coil. You can test which one is which by using the connectivity sensor on your multimeter or using the just seeing which pairs have zero resistance between them. Four the sparkfun motor, the blue and yellow wires are one pair, and the red and green wires are another pair. Connect these four wires to breakout board, so that the blue wire goes to slot 1, the yellow wire is in slot 2, the green wires is in slot 3, and the red wire is in slot 4. Use the image as a guide.

Step 3 Connect to an Arduino

Now that the motor is hooked up to the breakout board, you are ready to connect to an Arduino. You can use any two digital pins, in the picture and in the code below, we are using pins 2 and 3.

Step 4 Connect to Power! 12V and 5V

The L293D chip needs to connect to both the 5V micro-controller power supply as well as 12V stepper motor power source. Our breakout board has three pins for 12V power to come in. You just need to connect 1 to the power source the others are there if you want to power more motors (or other 12V accessories). There are also two pins where you can connect the 5V power. you can connect the 5V pin on the breakout board straight to the 5V pin on the Arduino. Lastly, you need to connect to ground. The L293 board, the 12V power supply and the micro-controller must share a common ground. We have pulled out four ground pins on our breakout board so you have plenty to spare. Just make sure the 12V ground pin AND the arduino ground (GND) pins are both connected to the L293.

Arduino Code for Stepper Motor Control

This code gives precise control of a stepper motor. It uses the arduino stepper motor library. It expects one button connected to pin 8 and a potentiometer connected to analog pin 0. Pressing the button switches between phase and speed control. When the speed control function is in control then twisting the potentiometer changes the speed of the motor. When phase control is in the driver's seat of the MCU twisting the pot rotates the motor to different angles around a circle. The speed control function goes like this
void speed_control(){
  int val = analogRead(pot_pin);
  val -= 512;
  val /= 5;
  stepper.setSpeed(abs(val));
  int step_amount = 0;
  if ( val > 0) step_amount = 1;
  else if ( val < 0) step_amount = -1;
  stepper.step(step_amount); 
}
First it reads the potentiometer value which is going to be a number between 0 and 1024 and put it in the variable val. Then we subtract 512 so that range will be -512 to +512. We divide this value by 5 (so the motor wont spin too fast) and now the range is about -102 to +102. Now we set the speed of the stepper motor to the absolute value of val which is a number between 0 and 102. So if the potentiometer is exactly in the middle the motors speed will be zero, and as the pot spins towards either extreme the motor will spin faster. Now we set the step amount to either -1 or +1 depending on the sign of val. This will control the direction the motor is spinning. The stepper.step() function is a blocking function meaning nothing can happen after it is called until the motor completes the step. So it is good to step small amount to keep your code more responsive.
Next up we have the phase control function. This function controls the angle of stepper not its speed. It goes like this:
void phase_control(){
  int val = analogRead(pot_pin);
  int degree = map(val,0,1023,0,200);
  int step_amount = 0;
  if (cur_pos < degree  && cur_pos < 200) step_amount = 1;
  else if(cur_pos > degree && cur_pos > 0) step_amount = -1;
  stepper.step(step_amount); 
  cur_pos += step_amount; 
}
Again we start by reading the potentiometer value in, but now we map this value to a number between 0 and 200. We use 200 because that is the number of steps of our particular motor. The smallest step is 1.8 degrees (1.8*200 = 360). Now we check if the cur_pos variable, which stores the current angle of the stepper. If cur_pos is less than the degree than we set the step amount to be positive to turn towards the degree heading. if cur_pos is bigger than degree then we set the step_amount to -1 to turn back towards degree. If cur_pos is equal to degree then we leave step_amount equal to 0 and don't turn at all (we are already pointing where the potentiometer directed). Lastly, we update the cur_pos variable to reflect the new position.
The main loop of this sketch is super simple. Just check which mode we are in and control the motors speed or angle accordingly. Then check the button to see if we need to change the mode. Grab the code below!
/* LucidTronix Control a stepper motor.
 * For instructions details and schematic, See:
 * http://www.lucidtronix.com/tutorials/24
 * Control rgb color by specifying
 * Hue saturation and brightness.
 * This code shows a rainbow
 */
#include <Stepper.h>
// change this to the number of steps on your motor
#define STEPS 200

// create an instance of the stepper class, specifying
// the number of steps of the motor and the pins it's
// attached to:
Stepper stepper(STEPS, 2,3);

int btn1 = 2;
int pot_pin = 2;
int mode = 1;
int num_modes = 2;
int cur_pos = 100;
unsigned long last_press = 0;

void setup()
{
    // set the speed of the motor to 30 RPMs
    stepper.setSpeed(30);
    pinMode(btn1, INPUT);
}

void loop()
{
  if (mode == 0 ) speed_control();
  else if (mode == 1) phase_control();
  check_buttons();
}

void check_buttons(){
  if (digitalRead(btn1) == HIGH  && millis() - last_press  > 500){
    mode = ++mode % num_modes; 
    last_press = millis();
    stepper.setSpeed(30);
  }
}

void speed_control(){
  int val = analogRead(pot_pin);
  val -= 512;
  val /= 5;
  stepper.setSpeed(abs(val));
  int step_amount = 0;
  if ( val > 0) step_amount = 1;
  else if ( val < 0) step_amount = -1;
  stepper.step(step_amount); 
}

void phase_control(){
  int val = analogRead(pot_pin);
  int degree = map(val,0,1023,0,200);
  int step_amount = 0;
  if (cur_pos < degree  && cur_pos < 200) step_amount = 1;
  else if(cur_pos > degree && cur_pos > 0) step_amount = -1;
  stepper.step(step_amount); 
  cur_pos += step_amount; 
}

Download EAGLE files and code

Download arduino and EAGLE files for this project!
Click Here to Download: Download EAGLE files and code

Parts

Title Description # Cost Link Picture
PCB L293 Breakout Stepper motor control with just two control lines. 1 $4.0 Link L293_boards
L293 IC DRVR P/P 4CH W/DIODES 20-SOIC L293D(D) 4.5 V ~ 36 V 1.2A PMIC - MOSFET, Bridge Drivers - Internal Switch Value: 4.5 V ~ 36 V 1 $3.63 Link 20-soic_0.295
Transistor Darlington BJT NPN 30V SOT23-3 Value: NPN 2 $0.24 Link Sot-23-3_pkg
Capacitor CAP ALUM 100UF 25V 20% SMD Value: 100µF 1 $0.21 Link Ave_series__338__6_3x7_7__x
Resistor Chip Resistor - Surface Mount RES 10K OHM 1/8W 5% 0805 SMD Value: 10k 2 $0.1 Link Screen_shot_2012-12-28_at_7.29.44_pm
Permalink: http://lucidtronix.com/tutorials/24
A breakout board for an electret condenser microphone shaped like an ear....
Wall-ter is a wall following robot....
Light sensors guids this robot towards the light....
Two proximity sensors help this bot avoid cliffs....
Display numbers in big bright red LEDs use only 3 micro-controller pins and the 74HC595 shift registe...
32 buttons for a fully-functioning hand-held USB keyboard....
Display 4 digits using only 3 MCU wires with the 4 x 7 Segment LED and shift register breakout board....
The MCP79410 breakout board gives easy access to this cute little timekeeper....