Arduino Motor Shield R3 Issue: Channel B not working

I have Arduino Motor Shield R3. I've got both channels plugged into motors, and 12V power supply. I've posted my code below. Channel A works fine, but Channel B won't work at all. I know the Channel's not fried because when I put both + terminals into same motor and both - terminals into the other motor, the lights for Channel B light up. But in that case, none of the motors spin. In proper wiring, Channel A works and its lights turn on, but Channel B lights don't turn on at all. Why isn't it working I think I'm doing everything correct?

void setup() {
  
  //Setup Channel A
  pinMode(12, OUTPUT); //Initiates Motor Channel A pin
  pinMode(9, OUTPUT); //Initiates Brake Channel A pin
  pinMode(3,OUTPUT);

  //Setup Channel B
  pinMode(13, OUTPUT); //Initiates Motor Channel A pin
  pinMode(8, OUTPUT);  //Initiates Brake Channel A pin
  pinMode(11,OUTPUT);
  
}

void loop(){  
  //Motor A forward @ full speed
  digitalWrite(12, HIGH); //Establishes forward direction of Channel A
  digitalWrite(9, LOW);   //Disengage the Brake for Channel A
  analogWrite(3, 255);   //Spins the motor on Channel A at full speed

  //Motor B backward @ half speed
  digitalWrite(13, HIGH);  //Establishes backward direction of Channel B
  digitalWrite(8, LOW);   //Disengage the Brake for Channel B
  analogWrite(11, 255);    //Spins the motor on Channel B at half speed
  
  delay(3000);
  
  digitalWrite(9, HIGH);  //Engage the Brake for Channel A
  digitalWrite(8, HIGH);  //Engage the Brake for Channel B  
}

Also, when I spin the motor connected to channel B fast enough, the Channel B lights turn on. But Channel B won't work by itself given the code above and proper wiring.

Show a wiring diagram.

You must understand - if it is not working, you have done something wrong. I can tell by this small part of your code that you are not observant.

You must remove all your wiring. Then make your sketch clear and correct, including comments, according to your wiring diagram. Then, connect each wire, verifying both ends are connected according to the schematic and the sketch.

Here is a slightly modified version of your sketch. See if you understand the changes.

void setup() {
  //Setup Channel A
  pinMode(12, OUTPUT); // Enable
  pinMode(9, OUTPUT);  // Brake
  pinMode(3, OUTPUT);  // PWM

  //Setup Channel B
  pinMode(13, OUTPUT); // Enable
  pinMode(8, OUTPUT);  // Brake
  pinMode(11, OUTPUT); // PWM
}

void loop() {
  // release brakes
  digitalWrite(9, LOW);
  digitalWrite(8, LOW);

  // set speed
  analogWrite(3, 255); // range is 0 to 255 minimum around 50
  analogWrite(11, 255); // range is 0 to 255 minimum around 50

  // enable motors
  digitalWrite(12, HIGH);
  digitalWrite(13, HIGH);

  // let motors run for three seconds
  delay(3000);

  // disable motors
  digitalWrite(12, LOW);
  digitalWrite(13, LOW);

  // wait a short time before applying brakes
  delay(250);

  // engage brakes
  digitalWrite(9, HIGH);
  digitalWrite(8, HIGH);

  // wait for one second before repeating
  delay(1000);
}

Once you get that working, here is your code with a few more changes for driving your robot...

#define ENABLE_A 12
#define BRAKE_A   9
#define PWM_A     3

#define ENABLE_B 13
#define BRAKE_B   8
#define PWM_B    11

byte pin[] = {3, 8, 9, 11, 12, 13};

void setup() {
  for (int i = 0; i < 6; i++)
    pinMode(pin[i], OUTPUT); // configure all pins as OUTPUT
}

void loop() {
  // release brakes
  digitalWrite(BRAKE_A, LOW);
  digitalWrite(BRAKE_B, LOW);

  // set speed
  analogWrite(PWM_A, 255); // range is 0 to 255 minimum is around 50
  analogWrite(PWM_B, 255); // range is 0 to 255 minimum is around 50

  // enable motors
  digitalWrite(ENABLE_A, HIGH);
  digitalWrite(ENABLE_B, HIGH);

  // let motors run for three seconds
  delay(2000);

  // disable motors
  digitalWrite(ENABLE_A, LOW);
  digitalWrite(ENABLE_B, LOW);

  // wait a short time before applying brakes
  delay(250);

  // engage brakes
  digitalWrite(BRAKE_A, HIGH);
  digitalWrite(BRAKE_B, HIGH);

  // wait for one second before continuing
  delay(1000);

  // enable motors
  digitalWrite(ENABLE_A, HIGH);
  digitalWrite(ENABLE_B, HIGH);

  turnLeft(); // call the left-turn function
  delay(2000); // turn for one second

  turnRight(); // call the left-turn function
  delay(2000); // turn for one second

  // disable motors
  digitalWrite(ENABLE_A, LOW);
  digitalWrite(ENABLE_B, LOW);

  // wait a short time before applying brakes
  delay(250);

  // engage brakes
  digitalWrite(BRAKE_A, HIGH);
  digitalWrite(BRAKE_B, HIGH);
}

void turnRight() {
  analogWrite(PWM_A, 127);
  analogWrite(PWM_B, 0);
}

void turnLeft() {
  analogWrite(PWM_A, 0);
  analogWrite(PWM_B, 127);
}

Hi,

Thank you for your elaborate response. Turns out, my motor shield had a short in it. I tried a different motor shield and the problem went away. However, I do appreciate your well organized code. I think I will use aspects of it. Thank you for posting it.

My question for you is: Why did you wait 250 ms before applying brake to motors?

Ouch... I hope you discard it or fix it so it does not damage anything else.

I read here (?) that releasing the forward driving (briefly) before applying the brakes is better for the motor. I picked 250 ms (1/4 second) because that is what I use with my arduino car. Imagine a real car, pressing the accelerator and the brake at the same time... not a good situation... so translate that into driving your robot motors.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.