Go Down

Topic: Arduino Uno with H-bridge (Read 2 times) previous topic - next topic

LocalgHost

#10
Sep 02, 2011, 11:30 am Last Edit: Sep 02, 2011, 11:32 am by LocalgHost Reason: 1
I use some L298N Ic to drive DC motors ( They are dual H bridge drivers ie, can control 2 DC motors each )

Here is a snippet of code that works for them :, note that I have defined onboard_led_pin as pin 13, and the in_1,in_2 and enable pins as whatever they were connected to ( in_1 and in_2 are digital outputs, and enable is a pin with PWM should I choose to do speed control.

If you want the motor to do something, you call " motor_drive(DIR1,255) "  or " motor_drive(1,255) "  which will drive tho motor at full power n direction 1

Do you have a schematic of your setup so its easier to debug?

Quote

#define DIR1  1                              // motor direction 1
#define DIR2  2                              // motor direction 2
#define OFF  3                              // motor off



Quote

void motor_drive (int dir, int power)  // Direct hardware interface
{
  switch(dir)                                                         // Determine motor driving according to passed values
 {
    case DIR1:                                                      // Direction 1
            digitalWrite(in_1,HIGH);                                // Motor turns in Direction 1 at a set power (PWM on enable line)
            digitalWrite(in_2,LOW);            
            analogWrite(enable,power);
            digitalWrite(onboard_LED_pin,HIGH);
            break;
           
    case DIR2:                                                     // Direction 2
            digitalWrite(in_1,LOW);                                // Motor turns in Direction 2 at a set power (PWM on enable line)
            digitalWrite(in_2,HIGH);
            analogWrite(enable,power);
            digitalWrite(onboard_LED_pin,HIGH);
            break;
    case OFF:
            digitalWrite(in_1,LOW);                                // Motor lines are pulled low, and enable set low
            digitalWrite(in_2,LOW);
            digitalWrite(enable,LOW);
            digitalWrite(onboard_LED_pin,HIGH);
            break;
    default:
            digitalWrite(in_1,LOW);                                //  Motor lines are pulled low, and enable set low
            digitalWrite(in_2,LOW);
            digitalWrite(enable,LOW);
            digitalWrite(onboard_LED_pin,HIGH);
            break;
 }
 digitalWrite(onboard_LED_pin,LOW);
}

.
  .
...

MrRubberBand

Thanks for the advice,

It was indeed the analogwrite(pin,speed) that was the fix. Well it worked for a few seconds only. I think it is because I am connecting the ground of the arduino to the ground of the battery incorrectly. It seems to be working only if it touches slightly or something. Anyone know a solution? When I push the wires in the breadboard completely, it doesn't work anymore.

MrRubberBand

Nevermind guys, the problem is with the bent h-bridge. Everything works now, Thanks for all your help.

Go Up