L298n not working properly.

I am experiencing an issue with the l298n motor driver board.I have a arduino controlling the l298n motor driver board.Even I set the same instruction to both the motors,they spin at different rpm :o .I am not able to find where the problem lies(arduino or the motor driver).I have a 10v power supply powering the board.Any help will be appreciatedThe code i am using is:

/*  L298N_Dual_H_Bridge

This program controls 2 separate DC motors using the L298N 
H Bridge Chip. 


          *THE SUBROUTINES WORK AS FOLLOWS*

motorA(mode, speed)
% replace A with B to control motor B %

mode is a number 0 -> 3 that determines what the motor 
will do.
0 = coast/disable the H bridge
1 = turn motor clockwise
2 = turn motor counter clockwise
3 = brake motor

speed is a number 0 -> 100 that represents percentage of
motor speed.
0 = off
50 = 50% of full motor speed
100 = 100% of full motor speed

EXAMPLE
Say you need to have motor A turn clockwise at 33% of its
full speed.  The subroutine call would be the following...

motorA(1, 33);

Created by 
Iain Portalupi http://www.youtube.com/iainportalupi
1/2/2014

This code is in the public domain.
*/

#define ENA 5  //enable A on pin 5 (needs to be a pwm pin)
#define ENB 3  //enable B on pin 3 (needs to be a pwm pin)
#define IN1 2  //IN1 on pin 2 conrtols one side of bridge A
#define IN2 4  //IN2 on pin 4 controls other side of A
#define IN3 6  //IN3 on pin 6 conrtols one side of bridge B
#define IN4 7  //IN4 on pin 7 controls other side of B
int sensor = 0;
void setup()
{
  //set all of the outputs
  pinMode(ENA, OUTPUT);
  pinMode(ENB, OUTPUT);
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);
}

void loop()
{
  motorA(1,30);
  motorB(1,30);
  delay(5000);
  motorA(1,70);
  motorB(1,70);
  delay(5000);
}

//******************   Motor A control   *******************
void motorA(int mode, int percent)
{
  
  //change the percentage range of 0 -> 100 into the PWM
  //range of 0 -> 255 using the map function
  int duty = map(percent, 0, 100, 0, 255);
  switch(mode)
  {
    case 0:  //disable/coast
      digitalWrite(ENA, LOW);  //set enable low to disable A
      break;
      
    case 1:  //turn clockwise
      //setting IN1 high connects motor lead 1 to +voltage
      digitalWrite(IN1, HIGH);   
      
      //setting IN2 low connects motor lead 2 to ground
      digitalWrite(IN2, LOW);  
      
      //use pwm to control motor speed through enable pin
      analogWrite(ENA, duty);  
      
      break;
      
    case 2:  //turn counter-clockwise
      //setting IN1 low connects motor lead 1 to ground
      digitalWrite(IN1, LOW);   
      
      //setting IN2 high connects motor lead 2 to +voltage
      digitalWrite(IN2, HIGH);  
      
      //use pwm to control motor speed through enable pin
      analogWrite(ENA, duty);  
      
      break;
      
    case 3:  //brake motor
      //setting IN1 low connects motor lead 1 to ground
      digitalWrite(IN1, LOW);   
      
      //setting IN2 high connects motor lead 2 to ground
      digitalWrite(IN2, LOW);  
      
      //use pwm to control motor braking power 
      //through enable pin
      analogWrite(ENA, duty);  
      
      break;
  }
}
//**********************************************************


//******************   Motor B control   *******************
  void motorB(int mode, int percent)
{
  
  //change the percentage range of 0 -> 100 into the PWM
  //range of 0 -> 255 using the map function
  int duty = map(percent, 0, 100, 0, 255);
  
  switch(mode)
  {
    case 0:  //disable/coast
      digitalWrite(ENB, LOW);  //set enable low to disable B
      break;
      
    case 1:  //turn clockwise
      //setting IN3 high connects motor lead 1 to +voltage
      digitalWrite(IN3, HIGH);   
      
      //setting IN4 low connects motor lead 2 to ground
      digitalWrite(IN4, LOW);  
      
      //use pwm to control motor speed through enable pin
      analogWrite(ENB, duty);  
      
      break;
      
    case 2:  //turn counter-clockwise
      //setting IN3 low connects motor lead 1 to ground
      digitalWrite(IN3, LOW);   
      
      //setting IN4 high connects motor lead 2 to +voltage
      digitalWrite(IN4, HIGH);  
      
      //use pwm to control motor speed through enable pin
      analogWrite(ENB, duty);  
      
      break;
      
    case 3:  //brake motor
      //setting IN3 low connects motor lead 1 to ground
      digitalWrite(IN3, LOW);   
      
      //setting IN4 high connects motor lead 2 to ground
      digitalWrite(IN4, LOW);  
      
      //use pwm to control motor braking power 
      //through enable pin
      analogWrite(ENB, duty);  
      
      break;
  }
}
//**********************************************************

Motors spin at different RPMs, even if they are of the same type. You need shaft encoders and PID code to fully control the speed of rotation.

I read voltages at the output of board and it has a difference of 2v.I think at least the voltages must be same at the outputs,shouldn't they?

It is certainly possible that your motors are overloading the L298N and it is in partial thermal shutdown.

Post a link to the product page or the data sheet for the motors.

The motor I have used is in attachments.The seller says they are 100rpm and draw 100mA at 9v no load.

41AYiCw9eTL.SY400.jpg

Those should work fine with the L298N, but in general, you cannot expect toy motors like that to run at the same speeds, even if supplied with the same voltage.

If you want a small robot run in a straight line, adjust the PWM values until it does. For turns, the motors run at different rates anyway.

Hi,
Have you swapped the motors over, to see if the problem goes with the motor?

Tom.... :slight_smile:

I have measured voltages at the output leads of the two motors.There is a difference of about 1v even arduino is giving the same instruction to the motors.So isn't the problem with the motor driver board or is it normal for it to behave like this?

Hi,
You are using pins 3 and 5 as your PWM for the motors

  • pin 5 and 6, PWM frequency = 62500Hz
  • pin 9 ,10, 3 and 11 PWM frequency = 32150Hz

From this info your PWM frequencies are different for each motor.
If the loads were LEDs or lamps you would not have a big difference in performance.
However because you are using motors, and they have inductance, they respond differently to different PWM frequencies.
I would say that this could be you problem.
So I would suggest you try putting the motor PWM on 5and6 or 9and10 or 11and13.

Tom.... :slight_smile: