ENع
←H-bridges and steppers
Lesson S4 · Special topics

H-bridges, motor direction and stepper motors

14 min11 steps5 questionsIntermediate
About this lesson

A DC motor turns the way the current through it flows. An H-bridge, four switches around the motor, chooses that direction, and must never close both switches on one side. Follow an Arduino Uno R3 driving a TI DRV8833: two input pins pick forward, reverse, coast or brake, and analogWrite() on one input sets the speed. Then drive a two-coil stepper one coil at a time and watch it move in exact steps. The lesson “Driving a servo and a one-way DC motor with a MOSFET” switched a motor one way; “PWM: dimming an LED” explains duty cycle; for a speed that holds under load, see the Control lesson “DC motor speed control”.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig S4 · Interactive
01 / 11

Current sets the direction

A DC motor turns one way when current enters its left lead, and the other way when current enters from the right. One transistor, as in the servo and MOSFET lesson, only switches current on and off: one direction.

DC motor   current left -> right : turns forward
           current right -> left : turns in reverse
one transistor: on or off, always the same way round
to reverse, the current must cross the motor the other way
Loading figure…
or ← →
Read the lesson step by step
  1. Current sets the direction

    A DC motor turns one way when current enters its left lead, and the other way when current enters from the right. One transistor, as in the servo and MOSFET lesson, only switches current on and off: one direction.

    DC motor   current left -> right : turns forward
               current right -> left : turns in reverse
    one transistor: on or off, always the same way round
    to reverse, the current must cross the motor the other way
  2. Four switches in an H

    An H-bridge puts the motor in the crossbar of an H. Each side has a high-side switch to the supply (S1, S3) and a low-side switch to ground (S2, S4). Closing two of them picks the path.

          supply (+)
       S1 ─┬──────┬─ S3     high side
           └─ M ──┘          the motor is the crossbar
       S2 ─┴──────┴─ S4     low side
          ground (-)
  3. Forward: close S1 and S4

    Close S1 and S4, one high and one low on opposite sides. Current leaves the 6 V battery, crosses the motor from left to right and returns through S4 to ground. The motor turns forward.

    closed  S1 + S4      open  S2, S3
    path    battery + -> S1 -> motor (left to right) -> S4 -> battery -
    motor   forward
  4. Reverse: close S3 and S2

    Now close the other diagonal, S3 and S2. Current crosses the motor from right to left, so it turns the other way. Same battery, same motor: only the switches changed.

    closed  S3 + S2      open  S1, S4
    path    battery + -> S3 -> motor (right to left) -> S2 -> battery -
    motor   reverse
  5. Never both on one side

    Close S1 and S2 together and the battery is shorted through two switches, missing the motor. That is shoot-through: a huge current that can burn the switches. Drivers leave a short dead time, all off, between turning one off and the other on.

    closed  S1 + S2  (or S3 + S4)
    path    battery + -> S1 -> S2 -> battery -   no motor in the way
    shoot-through: only the switches and wires limit the current
    DRV8833 dead time 450 ns (VM = 5 V), built in
  6. A driver chip does it

    The DRV8833 holds two H-bridges made of transistors. The Uno only sets two pins: AIN1 = 1 and AIN2 = 0 means forward, so the chip closes S1 and S4. The motor current comes from the battery, not the Uno.

    AIN1 AIN2   OUT1 OUT2   switches   motor
      0    0       Z    Z     none       coast
      0    1       L    H     S2+S3      reverse
      1    0       H    L     S1+S4      forward
      1    1       L    L     S2+S4      brake
    
    VM 2.7 to 10.8 V · 1.5 A RMS, 2 A peak per bridge
    
    // Arduino Uno R3 + TI DRV8833 (nSLEEP tied high, VM = 6 V)
    const int AIN1 = 9, AIN2 = 10;  // bridge A: the DC motor
    
    void drive(int in1, int in2) {
      digitalWrite(AIN1, in1);
      digitalWrite(AIN2, in2);
    }
    
    void setup() {
      pinMode(AIN1, OUTPUT);
      pinMode(AIN2, OUTPUT);
    }
    
    void loop() {
      drive(HIGH, LOW);  delay(2000);  // forward
      drive(LOW, LOW);   delay(1000);  // coast
      drive(LOW, HIGH);  delay(2000);  // reverse
      drive(HIGH, HIGH); delay(1000);  // brake
      analogWrite(AIN1, 128);            // forward, about half
      digitalWrite(AIN2, LOW);
      delay(2000);
    }
  7. Stop: coast or brake

    Inputs 0 0 open all four switches: the motor coasts slowly to a stop. Inputs 1 1 close both low-side switches, shorting the motor. Its own spin now drives a current that fights the turning, so it brakes fast.

    0 0   coast   all open, the motor spins down on its own
    1 1   brake   S2 + S4 short the motor's leads
          the spinning motor acts as a generator;
          its current brakes it
  8. Speed: PWM on one input

    analogWrite(AIN1, 128) switches AIN1 on and off 490 times a second, on for 128 of every 255 parts: about half. Off means 0 0, coast. The motor is too heavy to follow each pulse, so it runs at roughly half speed.

    line 3   analogWrite(AIN1, 128);   AIN2 stays LOW
    PWM      490 Hz on Uno pin 9: one cycle every 2.04 ms
    duty     128 / 255 = 50.2 %
    on   1 0  forward       off  0 0  coast (fast decay)
    the drawing slows the switching right down
  9. A stepper motor

    A stepper has two coils, A and B, around a magnet rotor. Current through a coil makes a magnetic field; the rotor turns to line up with it and holds. Each coil needs its own H-bridge: the DRV8833 has two.

    stepper   2 coils (A, B) + a magnet rotor
    coil A    on bridge A (AIN1, AIN2)
    coil B    on bridge B (BIN1, BIN2)
    current in a coil -> a field -> the rotor lines up and holds
  10. One coil at a time

    Energise coil A forward, then B forward, then A reversed, then B reversed. Each change turns the field a quarter turn and the rotor follows: one step. Repeat to keep turning; run the order backwards to turn the other way.

    step  AIN1 AIN2 BIN1 BIN2   coil   rotor
     1     1    0    0    0      A+     0°
     2     0    0    1    0      B+     90°
     3     0    1    0    0      A-     180°
     4     0    0    0    1      B-     270°
    
    // wave drive: one coil at a time. Bridge A = coil A, bridge B = coil B
    const int PIN[4] = {9, 10, 7, 8};  // AIN1 AIN2 BIN1 BIN2
    const int SEQ[4][4] = {
      {1, 0, 0, 0},   // A+
      {0, 0, 1, 0},   // B+
      {0, 1, 0, 0},   // A-
      {0, 0, 0, 1}    // B-
    };
    void stepTo(int n) {
      for (int i = 0; i < 4; i++) digitalWrite(PIN[i], SEQ[n % 4][i]);
    }
    void setup() { for (int i = 0; i < 4; i++) pinMode(PIN[i], OUTPUT); }
    void loop() {
      for (int n = 0; n < 200; n++) { stepTo(n); delay(10); }  // 1 turn
    }
  11. Count steps, know the angle

    Our drawing needs 4 steps per turn. A common real stepper has a toothed rotor and needs 200: 1.8 degrees each, so 50 steps is exactly 90 degrees, with no sensor. Step it too fast and it misses steps, and the count is wrong.

    drawing    4 steps per turn, 90° each
    real       200 steps per turn, 1.8° each
    50 steps   50 x 1.8° = 90°
    open loop: no sensor checks the angle
    too fast -> missed steps -> the count is wrong

Quiz Unlocks after the last step.