Arduino Forum

Topics => Robotics => Topic started by: mateoar on Sep 11, 2014, 02:02 am

Title: L298 different output voltages
Post by: mateoar on Sep 11, 2014, 02:02 am
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.

Title: Re: L298 different output voltages
Post by: raschemmel on Sep 11, 2014, 02:21 am
post your code. Don't ask why.
Title: Re: L298 different output voltages
Post by: mateoar on Sep 11, 2014, 02:24 am

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.

Quote
#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;
}
Title: Re: L298 different output voltages
Post by: raschemmel on Sep 11, 2014, 02:39 am

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. 
Quote




http://arduino.cc/en/Reference/analogWrite  (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 ?
Title: Re: L298 different output voltages
Post by: mateoar on Sep 11, 2014, 02:44 am


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. 
Quote




http://arduino.cc/en/Reference/analogWrite  (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 ?


Thanks for the help, ill check if that solves it.
Title: Re: L298 different output voltages
Post by: raschemmel on Sep 11, 2014, 02:48 am
You know what you need to do to correct it I assume ?
Title: Re: L298 different output voltages
Post by: mateoar on Sep 11, 2014, 03:02 am

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?
Title: Re: L298 different output voltages
Post by: raschemmel on Sep 11, 2014, 03:13 am
right
Title: Re: L298 different output voltages
Post by: mateoar on Sep 11, 2014, 03:15 am

right


It worked, thank you for your help
Title: Re: L298 different output voltages
Post by: raschemmel on Sep 11, 2014, 03:20 am
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 ?
Title: Re: L298 different output voltages
Post by: mateoar on Sep 11, 2014, 03:51 am

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
Title: Re: L298 different output voltages
Post by: raschemmel on Sep 11, 2014, 04:27 am
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 ?
Title: Re: L298 different output voltages
Post by: mateoar on Sep 11, 2014, 05:40 am

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:
https://www.sparkfun.com/products/9479 (https://www.sparkfun.com/products/9479)
https://www.sparkfun.com/datasheets/Robotics/L298_H_Bridge.pdf (https://www.sparkfun.com/datasheets/Robotics/L298_H_Bridge.pdf)
Title: Re: L298 different output voltages
Post by: raschemmel on Sep 11, 2014, 07:04 am
You don't have a heatsink or PCB ?
Title: Re: L298 different output voltages
Post by: mateoar on Sep 11, 2014, 07:29 am

You don't have a heatsink or PCB ?


Im currently testing it in a breadboard, but i do have a heatsink attached to it
Title: Re: L298 different output voltages
Post by: raschemmel on Sep 11, 2014, 07:57 am
That's really surprising. Most people use a variation of these (one of about 10 variations with different layouts):
   http://www.ebay.com/sch/i.html?_kw=L298+H-Bridge+Dual+Motor+Driver       (http://www.ebay.com/sch/i.html?_kw=L298+H-Bridge+Dual+Motor+Driver)
Title: Re: L298 different output voltages
Post by: raschemmel on Sep 11, 2014, 07:59 am
Oh and BTW,
This is what your code would look like if you had followed my instructions: (in Reply#3)

Code: [Select]
  #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
       
Title: Re: L298 different output voltages
Post by: mateoar on Sep 11, 2014, 09:10 am

That's really surprising. Most people use a variation of these (one of about 10 variations with different layouts):
   http://www.ebay.com/sch/i.html?_kw=L298+H-Bridge+Dual+Motor+Driver       (http://www.ebay.com/sch/i.html?_kw=L298+H-Bridge+Dual+Motor+Driver)


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
Title: Re: L298 different output voltages
Post by: raschemmel on Sep 11, 2014, 09:13 am
Ambitious , eh ?
Title: Re: L298 different output voltages
Post by: mateoar on Sep 11, 2014, 09:17 am

Ambitious , eh ?


I dont have much of a choice lol
Title: Re: L298 different output voltages
Post by: raschemmel on Sep 11, 2014, 03:14 pm
Sometimes we do our best work when we have a gun to our head. :smiley-mr-green:

That's why there is always a Boss. Someone has to hold the gun....
Title: Re: L298 different output voltages
Post by: narzan on Oct 05, 2014, 07:24 pm
hahaha @raschemmel
ok put the gun on my head  ;) and help me in this  :smiley-roll-sweat:
this is my post
http://forum.arduino.cc/index.php?topic=270583.15