DC motor spins the wrong way: swap the wires or swap the code

Swap the two wires of that motor, or invert its direction in code. Either one fixes it.

What the message means

A brushed DC motor turns the other way when the polarity reverses. Left and right motors are mounted facing opposite ways, so one often looks backwards.

Fix it in this order

  1. Test each motor alone with a low-speed forward command.
  2. Swap the two motor wires at the driver output if it turns the wrong way.
  3. Or add a variable in code that flips the direction for that motor.
  4. Label the wires so that the change is not lost on the next rebuild.
  5. Check that the driver direction pins match the code.

Why it happens

A motor that turns the wrong way is simply wired the other way round. Swapping the two motor wires on the driver output reverses it, and changing the direction logic in code does the same.

On a robot with two motors, check each side separately, because mirrored mounting means one motor usually needs reversing.

If you see this, do this

You seeDo this
One side backwardsFlip that side.
Both backwardsSwap both or flip both in code.

robot drifts to one side, L298N not spinning.

Last reviewed: November 2026. These are the usual causes and the order that finds them fastest. Test on your own board; clones and cables differ.

Why this happens

A brushed DC motor turns because the current through its armature crosses a magnetic field. Reverse the current and the force reverses, so the motor turns the other way. That is why swapping the two motor wires, or driving the driver’s direction inputs the other way round, flips the direction. Nothing is faulty.

On a two-wheel robot the motors face opposite ways, so the same command spins one wheel forward and the other backward. The code usually assumes both are ‘forward’, and one side must be inverted somewhere. Choose one place for it, either the wires or the code, and keep a note of which.

How to confirm the cause

  • Lift the wheels off the table and run each motor alone at low speed.
  • Write down for each motor: command, and observed direction as seen from the wheel’s outer face.
  • Check the wiring of the driver inputs to the right pins of that channel. A swapped IN1 and IN2 reverses it.
  • If you use encoders, print their sign. They should count up when the wheel goes forward.

Worked example

One function with an invert flag per side. It suits an L298N-style driver with two direction pins and a PWM pin.

const bool LEFT_INVERT  = false;
const bool RIGHT_INVERT = true;

void setMotor(int in1, int in2, int pwm, int speed, bool invert) {
  if (invert) speed = -speed;
  digitalWrite(in1, speed > 0);
  digitalWrite(in2, speed < 0);
  analogWrite(pwm, min(abs(speed), 255));
}

Common mistakes

  • Swapping the wires and also inverting in code, which puts the motor back where it started.
  • Swapping the battery wires to the board, not the motor wires. Reverse supply polarity can destroy parts.
  • Passing a negative number to analogWrite.
  • Not marking the change, so the next rebuild repeats the problem.

If this did not fix it

  • If only one motor runs, check the enable pin and the driver channel.
  • If the robot drifts, see the speed difference between the motors.
  • If nothing moves, check the battery and the driver power.