L298P motor shield wont work properly

Hi guys,

I'm trying to build a simple 2-wheel robot and trying to get my L298P motor driver to work properly.
However, the sketch only work partially, here's my setup and sketch.

int motorLE = 7;
int motorLI = 6;
int motorRE = 5;
int motorRI = 4;

void setup() {                
  pinMode(motorLI, OUTPUT);  
  pinMode(motorRI, OUTPUT);
}

void loop() {
  int value;
  for(value = 0 ; value <= 250; value+=5)
  {
    digitalWrite(motorRI,LOW); 
    analogWrite(motorRE, value);
    digitalWrite(motorLI,LOW);   
    analogWrite(motorLE, value);
    delay(100);
  }
}
  • Arduino Mega 2560, using a 12V power supply

  • L298P motor controller board, using a 7.4V Lipo battery, connected to the VSS and Ground

  • 2x tamiya motors

There is a Input and Enable for each motor, I connected them to my PWM ports on the 2560.

The motors wont move if I dont short the VMS=VSS pins together (can anyone tell me why?), so well then I connected them.

Now the motors are working, but only under certain conditions:

MotorA Input MotorB Input Result
LOW LOW Motors move in the same direction
HIGH HIGH Both Motors stopped moving, one of them shaking
LOW HIGH Motors move in the opposition direction
LOW LOW Both Motors stopped moving

And if I run the sketch above, only one motor will change its speed according to the loop, the other just keep spinning at the same speed.

I tried different wiring and codes but still cant crack this, any clue?

Do you have a link to your motor board?

HI here's the board spec, hope it helps..

Unless you are using a Mega, digital pin 7 does not have PWM. This is from the reference under analogWrite:

On most Arduino boards (those with the ATmega168 or ATmega328), this function works on pins 3, 5, 6, 9, 10, and 11. On the Arduino Mega, it works on pins 2 through 13.

You might want to try switching the pins around, like use 5 and 6 for the PWM.

I'm using a Mega,
nonetheless I also tried to use pin 3,5 for analogWrite, but not successful

MotorA Input MotorB Input Result
LOW LOW Motors move in the same direction
HIGH HIGH Both Motors stopped moving, one of them shaking
LOW HIGH Motors move in the opposition direction
LOW LOW Both Motors stopped moving

And if I run the sketch above, only one motor will change its speed according to the loop, the other just keep spinning at the same speed.

I tried different wiring and codes but still cant crack this, any clue?

That truth table is good, but missing two pins. Those are the inputs for direction? How are the two enable pins set for those results?

edit: I would not use the PWM until I got the motors working correctly strictly on-off and the correct direction. Use digitalWrite() as an on-off switch. How does that do?

That truth table is good, but missing two pins. Those are the inputs for direction? How are the two enable pins set for those results?

edit: I would not use the PWM until I got the motors working correctly strictly on-off and the correct direction. Use digitalWrite() as an on-off switch. How does that do?

Hi , first, yes the input pins are for direction, the enable pins are set to a constant speed like analogWrite(motorRE, 200).

And second, thanks for your suggestion on testing with digitalWrite() first!

The two motors now work correctly as I tested with the following sketch

int motorLE = 7;
int motorLI = 6;
int motorRE = 5;
int motorRI = 4;
int forward = 0;

void setup() {                
  pinMode(motorLI, OUTPUT);  
  pinMode(motorRI, OUTPUT);
  pinMode(motorLE, OUTPUT);
  pinMode(motorRE, OUTPUT);
}

void loop() {
  int value;
  
  if (forward ==0){
      digitalWrite(motorRI, HIGH);
      digitalWrite(motorLI, HIGH);
      forward = 1;
    }else{
      digitalWrite(motorRI, LOW);
      digitalWrite(motorLI, LOW);
      forward = 0;
    }
  digitalWrite(motorLE, HIGH);
  digitalWrite(motorRE, HIGH);

}

So the question now is if my motor board is supporting the PWM signals correctly?

So the question now is if my motor board is supporting the PWM signals correctly?

That is the next question. I would use the same testing procedure on the speed. Change the PWM cycle time on only one of the motors. Run it slowly up to full speed and back down to stopped with the PWM. Then try the other motor. Don't mess with the direction, just the speed. How does that do?

SurferTim:
That is the next question. I would use the same testing procedure on the speed. Change the PWM cycle time on only one of the motors. Run it slowly up to full speed and back down to stopped with the PWM. Then try the other motor. Don't mess with the direction, just the speed. How does that do?

Not sure if I fried my motor board today,
and looks like the left motor channel got some problem, it only spins one way, when the sketch switch the input and tells it to reverse, the motor power led is on but it isnt spinning in reverse.

My arduino was powered by USB before when I tested the motor board, and the motor board connects to the 5V and GND of the arduino, with external lipo on the other side

Today I tried to plug a 12V DC power supply on the arduino board, then one channel start to behave strangely.

Then I tried switching digital pins and motors, it seems that one motor channel is having trouble going backwards

Then I tried switching digital pins and motors, it seems that one motor channel is having trouble going backwards

Sounds like one of the L298P internal MOSFETs has failed.

SurferTim:

Then I tried switching digital pins and motors, it seems that one motor channel is having trouble going backwards

Sounds like one of the L298P internal MOSFETs has failed.

looks like so, is there anyway to verify it?

or is there any other better motor driver i should try?

got a new board, both motors working with PWM now
case solved!