Arduino DC Motor Won't Move Backward

I'm attempting to code two DC motors alongside an ultrasonic sensor for a robot, and my code seems to work... however, the motors only want to go one direction. They move perfectly fine going forward, but when I code it to go backward, both motors do not move. Reversing the polarity, however, causes the same issue in the opposite direction; they move perfectly fine backward, but do not want to go move forward.

I am using an Elegoo UNO R3, with a TB6612FNG motor driver and an HC-SR04 ultrasonic sensor. Tinkercad did not have the motor driver I'm using... so in its place is a pink square of wires in the breadboard.

Extensive googling has not helped... hopefully someone knows what's going on?

Here is my code:

//the right motor will be controlled by the motor A pins on the motor driver
const int AIN1 = 13;           //control pin 1 on the motor driver for the right motor
const int AIN2 = 12;            //control pin 2 on the motor driver for the right motor
const int PWMA = 11;            //speed control pin on the motor driver for the right motor

//the left motor will be controlled by the motor B pins on the motor driver
const int PWMB = 10;           //speed control pin on the motor driver for the left motor
const int BIN2 = 9;           //control pin 2 on the motor driver for the left motor
const int BIN1 = 8;           //control pin 1 on the motor driver for the left motor

int switchPin = 7;             //switch to turn the robot on and off

const int PIN_ECHO = 6;
const int PIN_TRIG = 5;
float duration, distance;

// Distance limits (Centimeters)
const float DIST_MIN = 8.0f;
const float DIST_MAX = 12.0f;
const int SPEED = 200;


void setup() {
  pinMode(LED_BUILTIN, OUTPUT);

  pinMode(switchPin, INPUT_PULLUP);   //set this as a pullup to sense whether the switch is flipped

  //set the motor control pins as outputs
  pinMode(AIN1, OUTPUT);
  pinMode(AIN2, OUTPUT);
  pinMode(PWMA, OUTPUT);

  pinMode(BIN1, OUTPUT);
  pinMode(BIN2, OUTPUT);
  pinMode(PWMB, OUTPUT);

	pinMode(PIN_ECHO, INPUT);
  pinMode(PIN_TRIG, OUTPUT);

	Serial.begin(9600);
}

void loop() {
  if (digitalRead(switchPin) == LOW) {
    digitalWrite(PIN_TRIG, LOW);
	  delayMicroseconds(2);
	  digitalWrite(PIN_TRIG, HIGH);
	  delayMicroseconds(10);
	  digitalWrite(PIN_TRIG, LOW);

    duration = pulseIn(PIN_ECHO, HIGH);
    distance = (duration*.0343)/2;

    Serial.print("Distance: ");
    Serial.print(distance);

    // I am expecting this to be really choppy--we might need to have it slow down instead of stopping dead when it's done
    if (distance < DIST_MIN) { // too close!
      Serial.println(" (Too close!)");
      digitalWrite(LED_BUILTIN, LOW);
      rightMotor(-SPEED);                                //drive the right wheel forward
      leftMotor(-SPEED);                                 //drive the left wheel forward
    } else if (DIST_MAX < distance) { // too far!
      Serial.println(" (Too far!)");
      digitalWrite(LED_BUILTIN, LOW);
      rightMotor(SPEED);                                //drive the right wheel forward
      leftMotor(SPEED);                                 //drive the left wheel forward
    } else { // just right!
      Serial.println(" (Just right!)");
      digitalWrite(LED_BUILTIN, HIGH);
      rightMotor(0);                                //drive the right wheel forward
      leftMotor(0);                                 //drive the left wheel forward
    }
  } else {
    digitalWrite(LED_BUILTIN, LOW);
    rightMotor(0);
    leftMotor(0);
  }
}

/********************************************************************************/
void rightMotor(int motorSpeed)                       //function for driving the right motor
{
  if (motorSpeed > 0)                                 //if the motor should drive forward (positive speed)
  {
    digitalWrite(AIN1, HIGH);                         //set pin 1 to high
    digitalWrite(AIN2, LOW);                          //set pin 2 to low
  }
  else if (motorSpeed < 0)                            //if the motor should drive backward (negative speed)
  {
    digitalWrite(AIN1, LOW);                          //set pin 1 to low
    digitalWrite(AIN2, HIGH);                         //set pin 2 to high
  }
  else                                                //if the motor should stop
  {
    digitalWrite(AIN1, LOW);                          //set pin 1 to low
    digitalWrite(AIN2, LOW);                          //set pin 2 to low
  }
  analogWrite(PWMA, abs(motorSpeed));                 //now that the motor direction is set, drive it at the entered speed
}

/********************************************************************************/
void leftMotor(int motorSpeed)                        //function for driving the left motor
{
  if (motorSpeed > 0)                                 //if the motor should drive forward (positive speed)
  {
    digitalWrite(BIN1, HIGH);                         //set pin 1 to high
    digitalWrite(BIN2, LOW);                          //set pin 2 to low
  }
  else if (motorSpeed < 0)                            //if the motor should drive backward (negative speed)
  {
    digitalWrite(BIN1, LOW);                          //set pin 1 to low
    digitalWrite(BIN2, HIGH);                         //set pin 2 to high
  }
  else                                                //if the motor should stop
  {
    digitalWrite(BIN1, LOW);                          //set pin 1 to low
    digitalWrite(BIN2, LOW);                          //set pin 2 to low
  }
  analogWrite(PWMB, abs(motorSpeed));                 //now that the motor direction is set, drive it at the entered speed
}

Can You grab pen and paper and make schematics? Those Fritzing pictures are mysteries that many helpers don't want to decipher.

You will damage the Arduino by attempting to power motors from the 5V output.

Use a separate power supply for motors and servos, and don't forget to connect the grounds.

Are you using an external power supply or powering the motors from the Arduino 5v?

Take a look at this post from @Robin2

I was using a 9V battery, but the day after using it the motors started slowing down significantly. Powering the arduino from the laptop caused it to start working again; after some googling, I discovered that 9V batteries are not the best batteries to use to power motors.

@anon94801460 Really?? Huh... how do I do that? My arduino kit came with a power module that I can hook batteries up to... I can see what I can do with that.

Servo example. The approach is similar for motors.

How does the ground have a two-way split?? Please bear with me; this is my second time ever using an arduino.

You can twist bare wires tightly together to make a splice or split wiring, and there is more than one ground pin on an Arduino Uno.

If you have a breadboard, there are power and ground tracks. Post an actual picture of your project.

Uh, the project is a little everywhere... but I can post a picture of it later today.

I am indeed using a breadboard. All I'd need to do is plug the power, servo, and arduino into the same ground track?

That is indeed the purpose of the ground track.

Update: I've got the robot powered via two power sources. A 9V battery powering the arduino (and the ultrasonic sensor), and 4 AA batteries powering the motors. It runs perfectly fine now, unpowered from the laptop (thank you for helping fix that!!!!)... but still won't move backward.

Here are some photos for.... what little clarity they may provide. (The motors are below the plate, as is the 9V battery.)



Breadboards are designed for temporary experiments with low power logic circuits, not motors, and are not very reliable.

The tracks can't handle motor currents and tend to burn, leading to unreliable connections or no connection at all.

If you want this robot to work reliably, now would be a good time to learn to solder. Adafruit and Sparkfun have great soldering tutorials.

But... if motors work unreliably, then why would an arduino starter kit come with motors and a guide on how to code motors with an arduino? That, and nothing in the troubleshooting section saying "this might be unreliable, if it is, go learn to solder"?

Not saying you're wrong, I'm just frustrated.

Update: Pins were coded for the wrong numbers. This was template code I copypasted, didn't even think the pin numbers could have been different from the guide I was given.

Old code:

For my project, this should instead be:

const int PWMA = 13;            //speed control pin on the motor driver for the right motor
const int AIN2 = 12;            //control pin 2 on the motor driver for the right motor
const int AIN1 = 11;           //control pin 1 on the motor driver for the right motor

//the left motor will be controlled by the motor B pins on the motor driver
const int BIN1 = 10;           //control pin 1 on the motor driver for the left motor
const int BIN2 = 9;           //control pin 2 on the motor driver for the left motor
const int PWMB = 8;           //speed control pin on the motor driver for the left motor

My guess is that most of those kits, often given as gifts, are put together once or twice, worked for a few minutes, set aside and people are happy.

It appears that you are putting together a mishmash of components. The images you posted show parts from Sparkfun, an Uno clone, a separate motor driver, a range sensor, but not an Arduino Starter kit.

You are very correct; the parts my group is using come from two different "introduction to arduino" kits, since both kits didn't have enough pieces and parts to do what we wanted to on their own. We were trying to save money for our prototype, but are likely going to go out of our way to buy better parts pretty soon.

If you want help debugging "motor won't move backward", forum members will need to see a complete schematic diagram of the project, with all pins, parts and connections clearly labeled. Hand drawn is fine.

Be sure to include in the post links to the product pages for all the modules.

Finally, you need a multimeter to verify connectivity of suspected bad connections, and to verify proper operating voltages at various points in the circuit.

A multimeter sounds really useful, and links sound really helpful, but do you know of a good wiring diagram program that's free? I'm going to do some googling on my own, but I'm wondering if you have anything you'd suggest.

Pencil and paper works fine and is much faster than learning how to use a program.