Sub topic 3
Reversing the motor by choosing which switches close
This chapter assumes you know Ohm's law, can read a simple wiring diagram, and understand a transistor as a switch. It also assumes you have seen the four-switch H-bridge layout from the previous chapter. Here you will learn which switch combinations drive the motor forward, drive it in reverse, and leave it stopped. You will also see how driver modules reduce those combinations to a few logic pins.
The four switches and their names
An H-bridge has four switches arranged around the motor. Call the two on the supply side the high-side switches and the two on the ground side the low-side switches. Label them like this:
- A_high connects the left motor terminal to the motor supply (VM).
- A_low connects the left motor terminal to ground.
- B_high connects the right motor terminal to VM.
- B_low connects the right motor terminal to ground.
The motor sits between the left terminal (call it OUT1) and the right terminal (call it OUT2). Current flows through the motor only when the two terminals are at different voltages. Direction depends on which terminal is higher.
Forward and reverse
Closing one diagonal pair of switches puts the full supply voltage across the motor in one direction. Closing the other diagonal pair reverses the voltage, so the motor spins the other way.
- Forward: close Ahigh and Blow. Current flows from VM through Ahigh, into OUT1, through the motor to OUT2, and down through Blow to ground.
- Reverse: close Bhigh and Alow. Current flows from VM through Bhigh, into OUT2, through the motor to OUT1, and down through Alow to ground.
"Forward" is only a label. Which physical direction the shaft turns depends on how you wired the motor to OUT1 and OUT2. If the motor turns the wrong way for your robot, swap the two motor wires or change the sign convention in your code.
The diagram deliberately routes every direction change through a stopped state. Chapter 12 covers why in detail. For now, note that going straight from Forward to Reverse means all four switches change state at nearly the same time, which is the situation that makes shoot-through possible.
All possible switch combinations
Four switches give 16 combinations. Most are useless or harmful. The table lists the ones that matter.
| A_high | A_low | B_high | B_low | Result |
|---|---|---|---|---|
| on | off | off | on | Forward |
| off | on | on | off | Reverse |
| off | off | off | off | Coast (motor terminals float) |
| off | on | off | on | Brake (both terminals to ground) |
| on | off | on | off | Brake (both terminals to VM) |
| on | on | any | any | Shoot-through on the left leg: avoid |
| any | any | on | on | Shoot-through on the right leg: avoid |
Each column pair (Ahigh with Alow, Bhigh with Blow) is a half-bridge, also called a leg. The rule is simple: never close both switches in the same leg. That would connect VM directly to ground. The next chapter covers this fault. The brake and coast rows are explained properly in a later chapter, but you should recognize them here because every driver exposes them through its input pins.
From four switches to two or three pins
Driving four switches independently would take four control signals and a lot of care. Most driver chips and modules reduce the problem so you cannot request the dangerous combinations.
Two direction inputs plus an enable
Chips such as the L298N and L293D give each bridge two direction inputs (IN1, IN2) and an enable input (EN). Each input controls one half-bridge. When IN1 is high, the left leg connects OUT1 to VM. When IN1 is low, the left leg connects OUT1 to ground. IN2 does the same for the right leg. Because each input selects either the high-side or the low-side switch of its leg, the two switches in one leg can never be on together.
| IN1 | IN2 | EN | Motor |
|---|---|---|---|
| HIGH | LOW | HIGH | Forward |
| LOW | HIGH | HIGH | Reverse |
| LOW | LOW | HIGH | Brake (both outputs to ground) |
| HIGH | HIGH | HIGH | Brake (both outputs to VM) |
| any | any | LOW | Coast (outputs disabled) |
The exact behavior differs between parts. On the L298N and L293D, pulling EN low turns all outputs off, so the motor coasts. Other chips treat LOW/LOW as coast and use a separate brake pin. Always check the truth table in the datasheet for your part before you rely on a combination.
Direction pin plus a PWM pin
Some modules use one DIR pin and one PWM pin. DIR picks forward or reverse. PWM sets how much of each cycle the selected pair is on. This takes two Arduino pins per motor, instead of three.
Two PWM pins
Other drivers, including many small DRV88xx-style parts, have two inputs, IN1 and IN2. You apply PWM to one input and hold the other low to choose direction. Swapping which input carries the PWM reverses the motor. Pin names and logic differ between families, so treat the pin names in this chapter as a pattern and not as a universal standard.
Setting direction from code
The following sketch uses the IN1, IN2 and EN scheme. A positive number selects forward, a negative number selects reverse, and zero stops the motor. The sign sets direction and the magnitude sets PWM duty.
const uint8_t IN1 = 7;
const uint8_t IN2 = 8;
const uint8_t EN = 9; // must be a PWM-capable pin
void setup() {
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(EN, OUTPUT);
}
// speed: -255 (full reverse) to 255 (full forward), 0 = stopped
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(200);
delay(2000);
setMotor(0);
delay(500);
setMotor(-200);
delay(2000);
setMotor(0);
delay(500);
}Look at how loop() is written. It stops for 500 ms between directions. That pause lets the motor slow down so the reversal does not start while the rotor is still spinning at speed. Skipping the pause works on a small motor, but it stresses the driver and the gearbox. A later chapter replaces the fixed pause with a speed ramp.
Inside setMotor(), the direction pins are set before analogWrite() changes the duty cycle. When you reverse from, say, 200 to -200 in a single call, the driver sees the direction pins flip while the enable duty is still high. Calling setMotor(0) between the two avoids that.
Common errors
- The motor spins the wrong way. Swap the motor wires, or swap the signs in your code. Do not change the driver wiring to fix a labeling issue.
- The motor only runs in one direction. Check that both direction pins are wired to separate Arduino pins and that neither is stuck. Test each pin by writing it HIGH and measuring the corresponding output.
- The motor hums but does not turn. The PWM value may be too low to overcome friction and the motor's starting torque. Try a higher value such as 150, then reduce it.
- Nothing happens at all. Confirm that EN is connected to a PWM-capable pin, that the motor supply is on, and that the Arduino and driver share a ground.
- The driver gets hot immediately. You may be holding a combination that shorts a leg or the motor is stalled. Remove power and check the wiring.
Summary of the idea
Forward and reverse come from closing opposite diagonal pairs of switches. The driver's input pins stand in for those four switches, and the chip's internal logic keeps both switches in one leg from closing together. You set direction through the pin pattern and speed through the PWM duty on the enable or input pin.
Exercise
A driver has inputs IN1, IN2 and EN. You want the motor to turn in the direction labeled "reverse" in the table above. Which pin states do you set, and which four switches in the bridge end up closed? What would you do if the motor turns the wrong physical way?
Show answer
Set IN1 LOW, IN2 HIGH, and EN HIGH (or a PWM duty). The switches Bhigh and Alow close, so current flows from VM through Bhigh, through the motor from OUT2 to OUT1, and to ground through Alow. If the shaft turns the wrong physical way, swap the two motor wires on OUT1 and OUT2, or swap the sign convention in your code. The driver wiring itself is fine.
Exercise
In the switch table, why is the combination Ahigh on and Alow on never allowed, and how does a two-input driver such as the L298N make it impossible to request?
Show answer
Closing both switches in one leg connects VM directly to ground through two nearly zero-resistance switches, so a very large current flows and can destroy the driver. In an IN1/IN2 driver, each input selects either the high-side or the low-side switch of its leg, so a single input can never turn both on at once.