DC Motor only works sometimes and is slower. PWM/Timer problem?

Hey! I’m working on a robot that is able to follow a moving target. I am almost done but one of my motors, enB, seems to be failing very often. It is not starting and sometimes moves very slow in comparison to the other DC motor. I thought maybe there is problem with PWM/Timer pins or something? The motor is working fine when it i hook it up to a battery by itself.

Please help me! See my code below. PS. The PID code is not yet implented to analogWrite since I’m trying to get it to work.

#include <PID_v1.h>
#include <Servo.h>
#include <NewPing.h>

#define SONAR_NUM     2 // Number of sensors.
#define MAX_DISTANCE 25 // Maximum distance (in cm) to ping.
#define PING_INTERVAL 33 // Milliseconds between sensor pings (29ms is about the min to avoid cross-sensor echo).

Servo myservo;  // create servo object to control a servo

unsigned long pingTimer[SONAR_NUM]; // Holds the times when the next ping should happen for each sensor.
unsigned int cm[SONAR_NUM];         // Where the ping distances are stored.
uint8_t currentSensor = 0;          // Keeps track of which sensor is active.

int distanceLeft;
int distanceRight;

int currentAngle;

int pos=80;    // variable to store the servo position

double Setpoint = 80;
double Input;
double Output;

int enA = 6;
int in1 = 7;
int in2 = 2;

int enB = 5;
int in3 = 8;
int in4 = 13;
PID myPID(&Input, &Output, &Setpoint,5,0,0, DIRECT);

NewPing sonar[SONAR_NUM] = {     // Sensor object array.
  NewPing(12, 11, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping.
  NewPing(4, 3, MAX_DISTANCE),

void setup() {
  pingTimer[0] = millis() + 75;           // First ping starts at 75ms, gives time for the Arduino to chill before starting.
  for (uint8_t i = 1; i < SONAR_NUM; i++) // Set the starting time for each sensor.
    pingTimer[i] = pingTimer[i - 1] + PING_INTERVAL;
    myservo.attach(10);  // attaches the servo on pin 10 to the servo object
    pinMode(enA, OUTPUT);
    pinMode(enB, OUTPUT);
    pinMode(in1, OUTPUT);
    pinMode(in2, OUTPUT);
    pinMode(in3, OUTPUT);
    pinMode(in4, OUTPUT);
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);

void loop() {
  for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through all the sensors.
    if (millis() >= pingTimer[i]) {         // Is it this sensor's time to ping?
      pingTimer[i] += PING_INTERVAL * SONAR_NUM;  // Set next time this sensor will be pinged.
      if (i == 0 && currentSensor == SONAR_NUM - 1) oneSensorCycle(); // Sensor ping cycle complete, do something with the results.
      sonar[currentSensor].timer_stop();          // Make sure previous timer is canceled before starting a new ping (insurance).
      currentSensor = i;                          // Sensor being accessed.
      cm[currentSensor] = MAX_DISTANCE + 10;      // Always set to higher than MAX_DISTANCE
      sonar[currentSensor].ping_timer(echoCheck); // Do the ping (processing continues, interrupt will call echoCheck to look for echo).
  // Other code that *DOESN'T* analyze ping results can go here.

void echoCheck() { // If ping received, set the sensor distance to array.
  if (sonar[currentSensor].check_timer())
    cm[currentSensor] = sonar[currentSensor].ping_result / US_ROUNDTRIP_CM;

void oneSensorCycle() { // Sensor ping cycle complete, do something with the results.
  // The following code would be replaced with your code that does something with the ping results.

  for (uint8_t i = 0; i < SONAR_NUM; i++) {                 // Go through the all the sensors.
    if(i==0) {                                               
    distanceLeft=cm[i];                                     // Assign distance to variables.
    if (i==1) {
      distanceLeft=cm[i-1];                                // Assign distance to variables.

void servoFollow() {
  if (distanceLeft <= MAX_DISTANCE || distanceRight <= MAX_DISTANCE)
    if (distanceLeft + 5 < distanceRight) 
      pos = pos + 2;
    if (distanceRight + 5 < distanceLeft)
      pos = pos - 2;
  if (pos > 160)
    pos = 160;
  if (pos < 0)
    pos = 0;
  currentAngle = myservo.read();

void pid() {
  Input = currentAngle;
  analogWrite(enA, 150);
  analogWrite(enB, 150);


Basic troubleshooting: Swap the 2 motors. Where does the problem go?

terryking228: Hi,

Basic troubleshooting: Swap the 2 motors. Where does the problem go?

Thank you for your response! I have no clue what is happening I did as you said and switched around everything and tried everything and now it seems to work all the time but I did not change anything... I like that it's working but I do not like that I don't know why since an error can strike at any time.