Direction: the H-bridge
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.
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.
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.
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.
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.
Driving it from code
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.
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.
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.
Stepper motors
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.
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.
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.
What happens
;; step 1/11 · 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
The four switches are drawn as simple switches. Inside the DRV8833 they are N-channel MOSFETs, and its logic only ever selects the four states of its truth table, so it cannot be told to short one side. Its built-in dead time is 450 ns at 5 V. The drawing's rotor is a single two-pole magnet, so 4 steps make a turn. Real hybrid steppers have toothed rotors and stators; 200 steps (1.8°) is common. In the drawing the stepper sits beside the DC motor; really you would connect its coils to the two bridges in place of the motor.