Sub topic 3
Reversing the Motor by Choosing Which Switches Close
This chapter assumes that you understand pinMode, digitalWrite, and analogWrite, and that you can read a simple wiring diagram. You should also know that a motor draws current through a load and that a transistor can act as an electronic switch.
The H-bridge switch pairs
An H-bridge places the motor between two vertical legs. Each leg has a high-side switch connected to the positive motor supply and a low-side switch connected to ground.
Name the switches:
Q1: left high-side switchQ2: left low-side switchQ3: right high-side switchQ4: right low-side switch
To turn the motor in one direction, close Q1 and Q4. Current flows from the supply, through Q1, across the motor from left to right, and through Q4 to ground.
To reverse the motor, open that pair and close Q3 and Q2. Current now flows through the motor from right to left.
The motor terminals are the two points where the H-bridge connects to the motor. Swapping the polarity across these terminals swaps the motor's direction.
| Left high side | Left low side | Right high side | Right low side | Result |
|---|---|---|---|---|
| On | Off | Off | On | Forward |
| Off | On | On | Off | Reverse |
| Off | Off | Off | Off | Coast or high-impedance stop |
| Off | On | Off | On | Dynamic brake on many drivers |
The exact brake behavior depends on the driver. Read its data sheet before relying on a particular state.
Controlling a driver from Arduino
Most motor-driver modules do not expose four raw switch gates. Instead, they provide two direction inputs and one enable or PWM input. The driver converts those logic signals into safe switch control.
For example, IN1 = HIGH and IN2 = LOW can select forward rotation. Reversing those inputs selects reverse rotation. The enable input controls whether the selected path is active and can receive a PWM signal for speed control.
const uint8_t IN1 = 7;
const uint8_t IN2 = 8;
const uint8_t EN = 9; // PWM-capable pin
void setup() {
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(EN, OUTPUT);
}
void setMotor(int speed) {
speed = constrain(speed, -255, 255);
if (speed > 0) {
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
} else if (speed < 0) {
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
} else {
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
}
analogWrite(EN, abs(speed));
}
void loop() {
setMotor(180);
delay(2000);
setMotor(0);
delay(500);
setMotor(-180);
delay(2000);
setMotor(0);
delay(500);
}Here, the sign of speed selects the direction. Its magnitude selects the PWM duty cycle. A value of 255 requests full duty cycle, while 128 requests about 50 percent.
Changing direction while the motor is spinning can produce a large current spike. Stop first, or ramp the command through zero. The motor's stored mechanical energy must be removed before it accelerates in the opposite direction.
Exercise
Which switch pair produces reverse rotation in the named H-bridge?
Show answer
Close Q3 and Q2, while keeping Q1 and Q4 open.