H-bridge short circuit prevention code?

Hi! Am I right that the code below would short an H-bridge?

when this code loops motor2A and motor2B will be HIGH at the same time.
I see most autonomous cars code is something like this, Is it because turning on both arduino pins to hbridge at the same time is too quick to matter?
what can I add to the code to prevent this?

this is for autonomous car
void loop() {

//Forward
//motor1 is Left side
//motor2 is Right side

digitalWrite(motor1A, HIGH);
digitalWrite(motor1B, LOW);

digitalWrite(motor2A, HIGH;
digitalWrite(motor2B, LOW);
delay(2000);

//Reverse
digitalWrite(motor1A, LOW);
digitalWrite(motor1B, HIGH);

digitalWrite(motor2A, LOW);
digitalWrite(motor2B, HIGH);
delay(2000);
}

http://itp.nyu.edu/physcomp/Labs/DCMotorControl
Correction:
I have added the above link. Read it. You need to use more control / hardware . Do not run your software as is!

I do not know about h- bridge but your software is running the motors one direction and than immediately in a reverse direction.
There must be something you can send to your h-bridge to actually pause the motion and than reverse it.

The two haves of an H-bridge should never be on at the same time, exactly how are these pins connected?


Rob

Thanks for the replies! I'm using arduino and H-bridge. I think all my wirings are correct. I'm asking about the code.
look at the last part of the code, after motor2B, HIGH, motor2A will be HIGH also if the code continues to loop.

I can fix this by changing the code sequence but it it'll only work for forward and reverse, adding left and right will again cause a short. Sorry if It's hard to understand what im saying, my english is poor.
void loop() {

//Forward
//motor1 is Left side
//motor2 is Right side
// Motor1A and Motor1B is One Motor.
//Motor2A and Motor2B is One Motor.
digitalWrite(motor1A, HIGH); // arduino pin to Hbridge
digitalWrite(motor1B, LOW); // arduino pin to Hbridge

digitalWrite(motor2A, HIGH; // motor2B is still HIGH = Short
digitalWrite(motor2B, LOW);// Now motor2B is LOW.
delay(2000);

//Reverse
digitalWrite(motor1A, LOW);
digitalWrite(motor1B, HIGH);

digitalWrite(motor2A, LOW);
digitalWrite(motor2B, HIGH); // this is HIGH and still be HIGH even after the delay.
delay(2000);
}

How about you write some functions like this

void turnLeft() {
  motor1rev();
  motor2fwd();
}

void turnRight() {
  motor1fwd();
  motor2rev();
}

void stop () {
  motor1stp();
  motor2stp()
  }

void   motor1stp() {
    digitalWrite(motor1B, LOW);
    digitalWrite(motor1A, LOW);
  }

void motor2stp() {
  digitalWrite(motor1B, LOW);
  digitalWrite(motor1A, LOW);
}

void motor1fwd() {
  digitalWrite(motor1B, LOW);
  digitalWrite(motor1A, HIGH);
}

void motor2fwd() {
  digitalWrite(motor2B, LOW);
  digitalWrite(motor2A, HIGH);
}

void motor1rev() {
  digitalWrite(motor1A, LOW);
  digitalWrite(motor1B, HIGH);
}

void motor2rev() {
  digitalWrite(motor2A, LOW);
  digitalWrite(motor2B, HIGH);
}

so you can see what happens when, and the main loop only has code that says WHAT to do not HOW to do it.

Note that all these funcs turn a pin off before they turn another one on (assuming that LOW == off).

Also, as vaclav said, don't slam the thing directly from forward to reverse, it's a physical device that has inertia. Always have a short delay between actions that have opposite effects on the motors.


Rob

Graynomad:
How about you write some functions like this

void turnLeft() {

motor1rev();
  motor2fwd();
}

void turnRight() {
  motor1fwd();
  motor2rev();
}

void stop () {
  motor1stp();
  motor2stp()
  }

void   motor1stp() {
    digitalWrite(motor1B, LOW);
    digitalWrite(motor1A, LOW);
  }

void motor2stp() {
  digitalWrite(motor1B, LOW);
  digitalWrite(motor1A, LOW);
}

void motor1fwd() {
  digitalWrite(motor1B, LOW);
  digitalWrite(motor1A, HIGH);
}

void motor2fwd() {
  digitalWrite(motor2B, LOW);
  digitalWrite(motor2A, HIGH);
}

void motor1rev() {
  digitalWrite(motor1A, LOW);
  digitalWrite(motor1B, HIGH);
}

void motor2rev() {
  digitalWrite(motor2A, LOW);
  digitalWrite(motor2B, HIGH);
}




so you can see what happens when, and the main loop only has code that says WHAT to do not HOW to do it.

Note that all these funcs turn a pin off before they turn another one on (assuming that LOW == off).

Also, as vaclav said, don't slam the thing directly from forward to reverse, it's a physical device that has inertia. Always have a short delay between actions that have opposite effects on the motors. 

______
Rob

Thanks for the code. I'm going to try to add "stop" everytime it need to change direction.
About the don't slam directly from reverse to forward. Is adding "stop" enough or do I need to add some "delay after the"stop"?

do I need to add some "delay after the"stop"?

Yep, don't know how much, that depends on the size of things I suppose. Try 500 and see how it goes.


Rob

Tamulmol:
Hi! Am I right that the code below would short an H-bridge?

No.

In order to short out the h-bridge the high and low drivers for a given output would need to be enabled at the same time. The hardware of the h-bridge will prevent that if it is designed properly. What you're talking about is driving two outputs both high or both low and connecting a motor between them. This will not damage anything. Typically, with both outputs low the motor is left isolated (coasting) and with both outputs high they will be connected together to produce generative braking.

PeterH:

Tamulmol:
Hi! Am I right that the code below would short an H-bridge?

No.

In order to short out the h-bridge the high and low drivers for a given output would need to be enabled at the same time. The hardware of the h-bridge will prevent that if it is designed properly. What you're talking about is driving two outputs both high or both low and connecting a motor between them. This will not damage anything. Typically, with both outputs low the motor is left isolated (coasting) and with both outputs high they will be connected together to produce generative braking.

And that is what I was talking about in short. I guess I better spell it out - the layout I looked at included "EN" signal / pin whatever you want to call it. The A /B signals did not include OFF / disconnect combination. They only had STOP slow / fast or something to that nature.
Again - to the OP I would advise to read the link. And if you need help understanding the mechanics, not the electronics I am sure this group will not mind discussing it.
Cheers
Vaclav

If you use PWM you can wind down the drive before switching to reverse and then wind it
up again - much smoother, no massive current spikes as the motor is driven at double
stall current.

PeterH:

Tamulmol:
Hi! Am I right that the code below would short an H-bridge?

No.

In order to short out the h-bridge the high and low drivers for a given output would need to be enabled at the same time. The hardware of the h-bridge will prevent that if it is designed properly. What you're talking about is driving two outputs both high or both low and connecting a motor between them. This will not damage anything. Typically, with both outputs low the motor is left isolated (coasting) and with both outputs high they will be connected together to produce generative braking.

Do you mean it's ok to turn both pins HIGH at the same time? I thought it's a thing to avoid in an Hbridge, theres a lot of tutorials saying do not close both sides at the same time. I really need to read more before building my own lol. Thanks

I thought it's a thing to avoid in an Hbridge, theres a lot of tutorials saying do not close both sides at the same time.

That's true, but if this is a proper "controller" you are driving it should be protected against such screw ups. If it's a DIY bridge with a few FETs then you have to be a lot more careful. I was assuming the latter actually.


Rob