L293D on Arduino (and Steppers)

So one of the more common chips that are used to control DC motors and Steppers is the L293D.  It’s a really simple chip, that’s not too bad to hook up.  Used with the Stepper.h library that ships with Arduino it’s pretty darned easy to use.

I ran into a guy over on Google+ that had some questions about it, and some code that he was looking at, and it just wasn’t working.  The big problem that I ran into when I tried to run it, is that it was essentially using the chip “naked.”  No libraries, just driving everything straight from a potentiometer and a couple of buttons.  The potentiometer controlled the speed, and the buttons made it go forward or reverse…in theory.  I think that the problem was in the way that the coils were being energized, but that’s really just kind of a shot in the dark.  I ran into a tutorial while trying to look up some issues (from which I get my shot-in-the-dark idea, from looking at the diagrams) over on Adafruit.  (Here’s a link to that tutorial, here.)

So I set up my own example, using an Uno instead of a Nano, but otherwise we’ve got the same hardware.  L293D controller, 28BYJ-48 5V DC Stepper.  Then just a couple of tact buttons, and instead of a single trim pot to control the speed of the stepper, I added two.  They control the speed and the number of steps, respectively.  I am using the library that comes with the Arduino IDE…I’m not usually a big fan of reinventing the wheel.  However, this setup does work as intended, so there shouldn’t be too much trouble if you want to take it back from here and work the motor with raw inputs straight from the Uno.  Below is the fritzing diagram, and the code.

GPlus Stepper Diagram_bb

(and yes, if anybody managed to see this before I updated the original Fritzing diagram above, I did indeed forget to connect the power and ground rails at first.  It’s fixed now!)

 

#include  // Included stepper library with the Arduino suite

// These are the 4 pins that go from the Arduino to the L293D
int pin1 = 12;
int pin2 = 11;
int pin3 = 10;
int pin4 = 9;

// The two trim-potentiometers that determine speed and step number
int speedPot = A0;
int stepPot = A1;

// The variables that hold the current speed and step numbers
int calcSpeed;
int calcStep;

// These are the pins for the positive and negative buttons
int btn1 = 7; // Positive
int btn2 = 6; // Negative

// These are the variables that hold the current state of the buttons
int b1State;
int b2State;

// Declare the stepper per the  library. 512 is steps per revolution.
Stepper motor(512,pin1,pin2,pin3,pin4);

void setup()
{
 pinMode(pin1, OUTPUT);
 pinMode(pin2, OUTPUT);
 pinMode(pin3, OUTPUT);
 pinMode(pin4, OUTPUT);

 // Using pullup resistors for the buttons to get clean results.
 pinMode(btn1, INPUT_PULLUP);
 pinMode(btn2, INPUT_PULLUP);

 // Start serial mode for debugging purposes. Not required.
 Serial.begin(9600);
}

void loop() 
{
 // Load the values of the various inputs into their respective state variables
 b1State = digitalRead(btn1);      // This is the positive button
 b2State = digitalRead(btn2);      // This is the negative button
 calcStep = analogRead(stepPot);   // Step amount
 calcSpeed = analogRead(speedPot); // Speed amount

 // Alternatively, you can declare calcStep as a const at the beginning of this
 // sketch, and leave out any reference to reading or mapping to that variable,
 // as long as you leave calcStep in the if statements to actually run the motor,
 // then this code will work seamlessly (with those specific alterations)

 // Map the raw integers from the potentiometers to values that
 // will work for this particular stepper. This is nowhere near
 // exact, just a really brief trial and error using the serial
 // outputs that are still in, below. Modify the final number in
 // each set to change the behaviour, if you wish.
 calcStep = map(calcStep,0,1023,1,512);
 calcSpeed = map(calcSpeed,0,1023,1,50);

 motor.setSpeed(calcSpeed); // Sets the speed of the motor on every iteration

 // If the positive button is pressed...
 if(b1State == LOW)
 {
 motor.step(calcStep); // Move the motor. Shocker.
 }

 // If the negative button is pressed...
 if(b2State == LOW)
 {
 motor.step(calcStep*-1); // Move the motor...wait for it...BACKWARDS
 }

 // For production, I would probably replace the IF statements above with attached interrupts
 // to give a little more flexibility and immediacy, but this works perfectly fine as-is for
 // a demonstration.

 // Basic debugging below here to show in the Serial Monitor what's going on at any given time
 Serial.print("+ Button: ");
 Serial.print(b1State);
 Serial.print(" / - Button: ");
 Serial.print(b2State);
 Serial.print(" / Speed: ");
 Serial.print(calcSpeed);
 Serial.print(" / Step: ");
 Serial.print(calcStep);
 Serial.println();
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s