Stepper library and Dual H-Bridge

I was playing with stepper motors and I noticed something odd in the library code (This is the Stepper library included in Arduino IDE).

      case 2:  //0101
        digitalWrite(motor_pin_1, LOW);
        digitalWrite(motor_pin_2, HIGH);
        digitalWrite(motor_pin_3, LOW);
        digitalWrite(motor_pin_4, HIGH);
      break;
      case 3:  //1001
        digitalWrite(motor_pin_1, HIGH);  // At this point pin_1 and pin_2 are both HIGH
        digitalWrite(motor_pin_2, LOW);
        digitalWrite(motor_pin_3, LOW);
        digitalWrite(motor_pin_4, HIGH);
      break;

Take a look at the first line in "case 3": motor_pin_2 is HIGH from previous step ( for clockwise rotation ) and now we are setting motor_pin_1 to HIGH too. Wouldn't it short the H-bridge for a split second? But everything seems to work fine. Is there some "magic" in digitalWrite I am not aware of? Or is it shorting it?

Thanks

aren't pins 1& 2 for one motor and pins 3&4 for a 2nd motor?

Yeah exactly :slight_smile: ... and 1&2 are both HIGH so motor should spin clockwise and counterclockwise at the same time (hence the short circuit).

1&2 are both HIGH

i see pins 2&4 high in case 2 and pins 1&4 HIGH in case3.

for the situation you're suggesting, there would be a short across the motor causing braking, not a short across any power supply

pins 1&4 HIGH in case3.

Yes it is true at the end of the case3. But between first and second line in case3 it is 1101 :slight_smile:

ok, now i understand. both outputs are momentary (time to execute digitalWrite) the same

but there's still no issue setting both h-bridge outputs to the same value. As I said, doing so creates a short between the motor terminals if both outputs are enabled (0 < PWM), allowing a current generated by the BEMF of the motor to flow thru.

the result of this momentary short is braking, which is harmless if trying to reverse the motor.

Ok I am programmer by hearth and just electronics hobbyist. But I was thinking that 1101 is "illegal" in a dual H-bridge circuit ("H H"), because you open all four transistors in the first H and create two direct paths from VCC to GND ( those vertical lines in the first H).

in an H-bridge there are 4 transistors (see below). each pin in your library is controlling the transistors connected to one motor terminal whether the motor is connected to Vcc or ground. they both can't be on at the same time.

with 2 pins, you either connect both motor terminal to Vcc or ground or one to Vcc and the other to ground.

a dual h-bridge is two separate circuits, 8 transistors controlling 2 motors

Oh I see the problem now :slight_smile: ... The H-bridge I was thinking about uses just two inputs, so dual H-bridge are those four bits. Your dual H-bridge would need 8inputs right?

My H-bridge looks like this:

The author of that post seems to agree that settings both pins high is not a good idea.

did you look at braking?

if you have an h-bridge like the schematic I posted with mosfets and diodes, setting both sides of the h-bridge to either low or high creates a short across the motor which creates a load on the motor creating a braking force.

i don't see a problems setting both sides of the h-bridge you showed should have a problem, although, without the diodes I don't see how it creates a short across the motor to do braking.

the TI SN754410 i've used has Darlingtons and diodes.

those diode protect the transistors when the motor voltage is cut off abruptly, BEMF can generate high voltage spikes without a current path.

gcjr:
i don't see a problems setting both sides of the h-bridge you showed should have a problem

I do. If both low side transistors are on, so are the high side transistors. Poof smoke!

sorry, your right with that circuit because the inputs control both the low side and the opposite high side transistors.

his library isn't intended to work with that design. looks like the transitions need to set both OFF first

if you have an h-bridge like the schematic

I am sorry man. I should have specified the schematic in the first place. But from this quote:

aren't pins 1& 2 for one motor and pins 3&4 for a 2nd motor?

I was thinking that we are on the same page. But somehow you started speaking about four pins per motor and braking. And I didn't catch that for few posts :slight_smile:

did you look at braking?

My H-bridge can't do any braking. And it's fine because it's controlling a stepper motor anyway.

gcjr's schematic shows a conventional H-Bridge circuit. PetrChudoba's schematic is a variant called a half-controlled H-Bridge.

another great learning experience

PetrChudoba's schematic is a variant called a half-controlled H-Bridge.

Thanks. I didn't know that.

It's the standard library included with the Arduino IDE. And I have seen tons of people using it (in youtube videos mostly) with this half-controlled H-Bridge without any issues. And mine is working flawlessly too.

So I am curious why ...

Most power supplies have current limiting. It's likely that shorting the bridge for a few milliseconds will actually kill the voltage on the power supply for that period of time - not long enough to cause serious damage or even be noticeable. In that case, the power dissipation in the transistors would be very low. Not that it's a good thing! But the problem can be easily fixed by changing the order of digitalWrite()'s.

Put a scope on the power supply.

:slight_smile: Sadly I don't own an oscilloscope.

It's simple reordering, I could try to submit PR, but the repo seems to be dead.

Do you happen to know if it's needed for 2-wires and 5-wires steppers too?

not sure that a scope is needed. electronics are damaged by heat and i'm not sure how much damage occurs if there a short for the period that digitalWrite() executes. but certainly should be avoided.

it seems clear that the stepper library may not be best for this type of h-bridge. why not write you own code? what features of the library do you need? (i could provide some code).