L298 different output voltages

Hi, Im currently working on a project that involves the use of two 6V dc motors controlled by a L298 driver. The problem im facing is that my motors are running at different speeds which is apparently caused by different output voltages for both motors which i measured. I atached the circuit im using.
I also measured the in1, in2, in3, in4, enA, inB voltages and they seem to be fine.

Any help would be really apreciated.

l298.bmp (217 KB)

post your code. Don't ask why.

raschemmel:
post your code. Don't ask why.

If you insist, the motor control is on the bottom, btw, i already measured the pwm outputs and they are ok.

#include<Wire.h>
const int GyroA=0x68;
const byte pin_motor1_dir = 2;
const byte pin_motor1_en = 3;
const byte pin_motor2_dir = 4;
const byte pin_motor2_en = 5;
const byte pin_buzzer = 6;
const byte pin_echo = 7;
const byte pin_trigger = 8;
const byte pin_led1 = 9;
const byte pin_led2 = 10;
const byte pin_btn = 12;
const byte pin_reflex_1 = A0;
const byte pin_reflex_2 = A1;
const byte pin_reflex_3 = A2;
const byte pin_reflex_4 = A3;
byte motor1_speed = 0;
int data_ultrasonido;
int data_reflex[4];

int buttonState;
int lastButtonState = LOW;
long debounceDelay = 50;
long lastDebounceTime = 0;

float data_imu[7];
byte z = 0;
void setup()
{
pinMode(pin_motor1_dir,OUTPUT);
pinMode(pin_motor1_en,OUTPUT);
pinMode(pin_motor2_dir,OUTPUT);
pinMode(pin_motor2_en,OUTPUT);
pinMode(pin_buzzer,OUTPUT);
pinMode(pin_trigger,OUTPUT);
pinMode(pin_echo,INPUT);
pinMode(pin_led1,OUTPUT);
pinMode(pin_led2,OUTPUT);
pinMode(pin_btn,INPUT);
pinMode(pin_reflex_1,INPUT);
pinMode(pin_reflex_2,INPUT);
pinMode(pin_reflex_3,INPUT);
pinMode(pin_reflex_4,INPUT);
Wire.begin();
Wire.beginTransmission(GyroA);
Wire.write(0x6B);
Wire.write(0); //Wake up
Wire.endTransmission(true);
//digitalWrite(pin_buzzer,HIGH);
//digitalWrite(pin_led1,HIGH);
//digitalWrite(pin_led2,HIGH);
Serial.begin(9600);
}
void loop()
{
//delay(350);
//debug_imu();
//debug_reflex();
//debug_ultrasonido();
debounce();

}
void debug_reflex()
{
sensar_reflex();
for(int i = 0;i<4;i++)
{
Serial.print("Reflex ");
Serial.print(i);
Serial.print(": ");
Serial.println(data_reflex*);*

  • }*
  • Serial.println();*
    }
    void debug_ultrasonido()
    {
  • sensar_ultrasonido();*
  • Serial.print("Distancia: ");*
  • Serial.println(data_ultrasonido);*
  • Serial.println();*
    }
    void debug_imu()
    {
  • sensar_imu();*
  • Serial.print("AcX: ");*
  • Serial.println(data_imu[2]);*
  • Serial.print("AcY: ");*
  • Serial.println(data_imu[1]);*
  • Serial.print("AcZ: ");*
  • Serial.println(data_imu[0]);*
  • Serial.print("GyX: ");*
  • Serial.println(data_imu[3]);*
  • Serial.print("GyY: ");*
  • Serial.println(data_imu[4]);*
  • Serial.print("GyZ: ");*
  • Serial.println(data_imu[5]);*
  • Serial.print("Temp: ");*
  • Serial.println(data_imu[6]);*
  • Serial.println();*
    }
    void sensar_ultrasonido()
    {
  • long duration;*
  • digitalWrite(pin_trigger, LOW);*
  • delayMicroseconds(2);*
  • digitalWrite(pin_trigger, HIGH);*
  • delayMicroseconds(10);*
  • digitalWrite(pin_trigger, LOW);*
  • duration = pulseIn(pin_echo, HIGH,11765);*
  • if(duration!=0)*
  • {*
  • data_ultrasonido = duration/59;*
  • }*
  • else*
  • {*
  • data_ultrasonido = -1;*
  • }*
    }
    void sensar_reflex()
    {
  • data_reflex[0] = analogRead(pin_reflex_1);*
  • data_reflex[1] = analogRead(pin_reflex_2);*
  • data_reflex[2] = analogRead(pin_reflex_3);*
  • data_reflex[3] = analogRead(pin_reflex_4);*
    }
    void sensar_imu()
    {
  • Wire.beginTransmission(GyroA);*
  • Wire.write(0x3B);*
  • Wire.endTransmission(false);*
  • Wire.requestFrom(GyroA,14,true);*
    data_imu[0] = Wire.read()*256+Wire.read();
    data_imu[1]=Wire.read()*256+Wire.read();
    data_imu[2]=Wire.read()*256+Wire.read();
    data_imu[6]=(Wire.read()*256+Wire.read())/340.00+36.53;
    data_imu[3]=Wire.read()*256+Wire.read();
    data_imu[4]=Wire.read()*256+Wire.read();
    data_imu[5]=Wire.read()*256+Wire.read();
  • data_imu[3]/=131;*
  • data_imu[4]/=131;*
  • data_imu[5]/=131;*
  • data_imu[0]/=16384;*
  • data_imu[1]/=16384;*
  • data_imu[2]/=16384;*
    }
    void debounce()
    {
  • int state = digitalRead(pin_btn);*
  • if(state!=lastButtonState)*
  • {*
  • lastDebounceTime = millis();*
  • }*
  • if ((millis() - lastDebounceTime) > debounceDelay)*
  • {*
  • if (state != buttonState)*
  • {*
  • buttonState = state;*
  • if (buttonState == HIGH)*
  • {*
  • //tone(pin_buzzer,1760,50);*
  • if(motor1_speed < 255)*
  • {*
  • motor1_speed+=51;*
  • }*
  • else*
  • {*
  • motor1_speed = 0;*
  • }*
  • analogWrite(pin_motor1_en,motor1_speed);*
  • analogWrite(pin_motor2_en,motor1_speed);*
  • }*
  • }*
  • }*
  • lastButtonState = state;*
    }
    [/quote]

click the MODIFY button in the upper right of the post window.
Highlight all you code.
click the "#" CODE TAGS button on the toolbar above just to the left of the QUOTE button.
click SAVE (at the bottom).
When you post code on the forum, please make a habit of using the code tags "#" button.

http://arduino.cc/en/Reference/analogWrite
WHAT DOES THIS SAY ABOUT YOUR MOTOR-2 PWM PIN ?
The frequency of the PWM signal on most pins is approximately 490 Hz. On the Uno and similar boards, pins 5 and 6 have a frequency of approximately 980 Hz.

Now do you see why I asked you to post your code ?

raschemmel:

click the MODIFY button in the upper right of the post window.
Highlight all you code.
click the "#" CODE TAGS button on the toolbar above just to the left of the QUOTE button.
click SAVE (at the bottom).
When you post code on the forum, please make a habit of using the code tags "#" button.

analogWrite() - Arduino Reference
WHAT DOES THIS SAY ABOUT YOUR MOTOR-2 PWM PIN ?
The frequency of the PWM signal on most pins is approximately 490 Hz. On the Uno and similar boards, pins 5 and 6 have a frequency of approximately 980 Hz.

Now do you see why I asked you to post your code ?

Thanks for the help, ill check if that solves it.

You know what you need to do to correct it I assume ?

raschemmel:
You know what you need to do to correct it I assume ?

I guess changing the pins so both pwm outputs operate at the same frequency?

right

raschemmel:
right

It worked, thank you for your help

That was easy wasn't it ?
Aren't you glad it wasn't a hardware problem ?
Now you see why I asked you to post your code ?

raschemmel:
That was easy wasn't it ?
Aren't you glad it wasn't a hardware problem ?
Now you see why I asked you to post your code ?

Haha yea, i honestly thought the l298 was broken, but as usual it was just one of those little details you completely ignore

Can you post the link of your particular L298 module ? I assume it is a premade module on a pcb and not a breadboarded chip , correct ?

raschemmel:
Can you post the link of your particular L298 module ? I assume it is a premade module on a pcb and not a breadboarded chip , correct ?

Yeap, here's the sparkfun link and datasheet:

You don't have a heatsink or PCB ?

raschemmel:
You don't have a heatsink or PCB ?

Im currently testing it in a breadboard, but i do have a heatsink attached to it

That's really surprising. Most people use a variation of these (one of about 10 variations with different layouts):

Oh and BTW,
This is what your code would look like if you had followed my instructions: (in Reply#3)

   #include<Wire.h>
const int GyroA=0x68; 
const byte pin_motor1_dir = 2;
const byte pin_motor1_en = 3;
const byte pin_motor2_dir = 4;
const byte pin_motor2_en = 5;
const byte pin_buzzer = 6;
const byte pin_echo = 7;
const byte pin_trigger = 8;
const byte pin_led1 = 9;
const byte pin_led2 = 10;
const byte pin_btn = 12;
const byte pin_reflex_1 = A0;
const byte pin_reflex_2 = A1;
const byte pin_reflex_3 = A2;
const byte pin_reflex_4 = A3;
byte motor1_speed = 0;
int data_ultrasonido;
int data_reflex[4];

int buttonState;
int lastButtonState = LOW;
long debounceDelay = 50;
long lastDebounceTime = 0;

float data_imu[7];
byte z = 0;
void setup()
{
  pinMode(pin_motor1_dir,OUTPUT);
  pinMode(pin_motor1_en,OUTPUT);
  pinMode(pin_motor2_dir,OUTPUT);
  pinMode(pin_motor2_en,OUTPUT);
  pinMode(pin_buzzer,OUTPUT);
  pinMode(pin_trigger,OUTPUT);
  pinMode(pin_echo,INPUT);
  pinMode(pin_led1,OUTPUT);
  pinMode(pin_led2,OUTPUT);
  pinMode(pin_btn,INPUT);
  pinMode(pin_reflex_1,INPUT);
  pinMode(pin_reflex_2,INPUT);
  pinMode(pin_reflex_3,INPUT);
  pinMode(pin_reflex_4,INPUT);
  Wire.begin();
  Wire.beginTransmission(GyroA);
  Wire.write(0x6B);
  Wire.write(0); //Wake up
  Wire.endTransmission(true);
  //digitalWrite(pin_buzzer,HIGH);
  //digitalWrite(pin_led1,HIGH);
  //digitalWrite(pin_led2,HIGH);
  Serial.begin(9600);
}
void loop()
{
  //delay(350);
  //debug_imu();
  //debug_reflex();
  //debug_ultrasonido();
  debounce();
  
}
void debug_reflex()
{
  sensar_reflex();
  for(int i = 0;i<4;i++)
  {
    Serial.print("Reflex ");
    Serial.print(i);
    Serial.print(": ");
    Serial.println(data_reflex);
  }
  Serial.println();
}
void debug_ultrasonido()
{
  sensar_ultrasonido();
  Serial.print("Distancia: ");
  Serial.println(data_ultrasonido);
  Serial.println();
}
void debug_imu()
{
  sensar_imu();
  Serial.print("AcX: ");
  Serial.println(data_imu[2]);
  Serial.print("AcY: ");
  Serial.println(data_imu[1]);
  Serial.print("AcZ: ");
  Serial.println(data_imu[0]);
  Serial.print("GyX: ");
  Serial.println(data_imu[3]);
  Serial.print("GyY: ");
  Serial.println(data_imu[4]);
  Serial.print("GyZ: ");
  Serial.println(data_imu[5]);
  Serial.print("Temp: ");
  Serial.println(data_imu[6]);
  Serial.println();
}
void sensar_ultrasonido()
{
  long duration;
  digitalWrite(pin_trigger, LOW); 
  delayMicroseconds(2);
  digitalWrite(pin_trigger, HIGH);
  delayMicroseconds(10); 
  digitalWrite(pin_trigger, LOW);
  duration = pulseIn(pin_echo, HIGH,11765);
  if(duration!=0)
  {
    data_ultrasonido = duration/59;
  }
  else
  {
    data_ultrasonido = -1;
  }
}
void sensar_reflex()
{
  data_reflex[0] = analogRead(pin_reflex_1);
  data_reflex[1] = analogRead(pin_reflex_2);
  data_reflex[2] = analogRead(pin_reflex_3);
  data_reflex[3] = analogRead(pin_reflex_4);
}
void sensar_imu()
{
  Wire.beginTransmission(GyroA);
  Wire.write(0x3B);
  Wire.endTransmission(false);
  Wire.requestFrom(GyroA,14,true); 
  data_imu[0] = Wire.read()*256+Wire.read(); 
  data_imu[1]=Wire.read()*256+Wire.read(); 
  data_imu[2]=Wire.read()*256+Wire.read();
  data_imu[6]=(Wire.read()*256+Wire.read())/340.00+36.53; 
  data_imu[3]=Wire.read()*256+Wire.read();
  data_imu[4]=Wire.read()*256+Wire.read();  
  data_imu[5]=Wire.read()*256+Wire.read();  
  data_imu[3]/=131;
  data_imu[4]/=131;
  data_imu[5]/=131;
  data_imu[0]/=16384;
  data_imu[1]/=16384;
  data_imu[2]/=16384;
}
void debounce()
{
  int state = digitalRead(pin_btn);
  if(state!=lastButtonState)
  {
    lastDebounceTime = millis();
  }
  if ((millis() - lastDebounceTime) > debounceDelay) 
  {
    if (state != buttonState) 
    {
      buttonState = state;
      if (buttonState == HIGH) 
      {
        //tone(pin_buzzer,1760,50);
        if(motor1_speed < 255)
        {
          motor1_speed+=51;
        }
        else
        {
          motor1_speed = 0;
        }
        analogWrite(pin_motor1_en,motor1_speed);
        analogWrite(pin_motor2_en,motor1_speed);
      }
    }
  }
  lastButtonState = state;
}
Report to moderator     Logged

raschemmel:
That's really surprising. Most people use a variation of these (one of about 10 variations with different layouts):
L298 H-Bridge Dual Motor Driver for sale | eBay

Yea I also have one of those but for this project (micro sumo) i needed to save as much space as possible and so im going to etch my own pcb with all the circuit

Ambitious , eh ?

raschemmel:
Ambitious , eh ?

I dont have much of a choice lol