How does an H Bridge work?
Inside the red chip is a circuit called an H-Bridge because it makes the shape of an H. It's designed to provide adjustable voltage to a source. Notice in the above picture, all 4 switches are open. In this case the motor will receive no voltage.
In the pictures below 2 of the 4 switches are strategically opened and closed to drive the motor in a particular direction.
Our code can control which switch is opened and closed and how much power is delivered to the motor.
digitalWrite(9, HIGH); //this closes two of the switches for motor A
digitalWrite(8, LOW); //this opens two of the switches for motor A
analogWrite(3, 255); //this gives all (255) of the power