Let's Make Robots!

L293D H-Bridge problem

Ok heres the problem, no matter what I do I can't get the H-bridge to work properly.

I've triple checked my connections, here is the test code i'm using


  // h bridge test
 
  const int switchPin = 12;    // switch input
  const int motor1Pin = 7;    // H-bridge leg 1 (pin 2, 1A)
  const int motor2Pin = 6;    // H-bridge leg 2 (pin 7, 2A)
  const int ledPin = 13;      // LED
  const int enablePin1 =8;    // Enable motor pin
 
  void setup() {
    // set the switch as an input:
    pinMode(switchPin, INPUT);

    // set all the other pins you're using as outputs:
    pinMode(motor1Pin, OUTPUT);
    pinMode(motor2Pin, OUTPUT);
    pinMode(ledPin, OUTPUT);

    // set enablePin high so that motor can turn on:
   
    digitalWrite(enablePin1, HIGH);
 
    // blink the LED 3 times. This should happen only once.
    // if you see the LED blink three times, it means that the module
    // reset itself,. probably because the motor caused a brownout
    // or a short.
    blink(ledPin, 3, 100);
  }

  void loop() {
    // if the switch is high, motor will turn on one direction:
   
    if (digitalRead(switchPin) == HIGH) {
      digitalWrite(motor1Pin, LOW);   // set leg 1A of the H-bridge low
      digitalWrite(motor2Pin, HIGH);  // set leg 2A of the H-bridge high
     
    }
    // if the switch is low, motor will turn in the other direction:
   
    else {
      digitalWrite(motor1Pin, HIGH);  // set leg 1A of the H-bridge high
      digitalWrite(motor2Pin, LOW);   // set leg 2A of the H-bridge low
     
    }
  }

  /*
    blinks an LED
   */
  void blink(int whatPin, int howManyTimes, int milliSecs) {
    int i = 0;
    for ( i = 0; i < howManyTimes; i++) {
      digitalWrite(whatPin, HIGH);
      delay(milliSecs/2);
      digitalWrite(whatPin, LOW);
      delay(milliSecs/2);
    }
  }

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Great I could use a tutorial on PWM

Well I can't find the usb cable for my camera so here is something anyway!

 

 

How do I post pics?

See this help page for how to post pictures.

http://letsmakerobots.com/node/22529

how exactly it does not work properly? Are you getting any motion at all?

It on moves in one direction when the switch is pressed it stops.

Everything looks pretty much good.

I would try following:

I'm not sure about your switch circuit, but if you connect it strait between arduino pin and ground, you should add

digitalWrite(switchPin, HIGH); after pinMode(switchPin, INPUT); to enable internal pullup resistor.

Most probably you do not really need enable pin on L293D, so you could just connect it to battery +

Cannot think about anything else so far. btw what kind of motors are you using? And if you could take a photo of your setup it would help us a lot.

 

I'm not sure about your switch circuit, but if you connect it strait between arduino pin and ground, you should add

digitalWrite(switchPin, HIGH); after pinMode(switchPin, INPUT); to enable internal pullup resistor.

 

Thanks for the tip.

 

what kind of motors are you using?

I'm using motors from a cheap walmart rc truck, they ran on 2 AA batteries and have caps across they're + and -solder points.

What's the schematic? Code seems fine at first glimpse.

Since I've yet to figure out how to post pics I'll just describe it. 

 

Pin 1 of L293 goes to pin 8 on Arduino

Pin 2 "   "        "      to pin 7       "

Pin 3                      to Motor +

Pins 4 and 5 to ground

pin 6                       to Motor -

Pin 7                      to Pin 6

Pin 8                      to battery Pos 

Pin 9 thru 15 unused

Pin 16 to  +5vdc on Arduino

Arduino, L293, battery all on same ground plane

 

Hope thats clear

I would suggest to connect all 4 GND pins (4,5,12,13) . Don't know, if they are internally connected or not, nor if this can cause any problems.

What kind of battery and motor you're using? Switching a motor from full speed forward to full speed reverse isn't wise. That could result in a voltage drop, that could cause a controller reset. Better use PWM and ramp the motor down to zero, reverse direction, than ramp up to full speed.