DIY L298n motor controller - need a little help

Hi all,
So today I built myself a l298 motor controller for the purpose of controlling a few 24v dc motors. I used an instructables paint by number type of tutorial thinking that it we be clear on how to set it up with arduino at the end..but it isnt so clear to me.
Here is the link for the board that I made

What I would like help with is how to use it, what pins I should use and which grounds to connect together and also if Im going to have an issue using 0,43 ohm 1 watt resistors instead of the 0,047 it says to use.

Ill be setting it up for pwm control of 2 motors. I measured the amps of both motors and it seems I am under the 2amp limit of this chip.
my power supply is not quite 24v(19.4) taken from an old laptop that died but it has about 4amps output.
Its really just the hook up that im looking to complete and if those cap and resistor values are within spec..

You have to know the maximum current of the motors, the "stall current". The driver should be able to supply that kind of current. So you might need another driver after all.
Sparkfun has a nice selection, https://www.sparkfun.com/categories/179

The power supply from the laptop is okay. If it is a good brand, a 4A model could supply 6A or 8A, but it will get too hot if you draw more current than the maximum.

Do you want the DC motor to rotate in both directions ?
If so, you use a normal H-bridge.

The GND (X2-1) and +12V (X1-3) are not used.

The connected on the left has GND and +5V. Those should be connected to the Arduino.
So the Arduino provides the +5V, which is also used by the L298 for the logic signals.

For the sense resistors, see the datasheet of the L298.
The current to the motors via the H-bridge is also going through the sense resistors.
2A and 0.43 ohm = 0.86 Volt. That is okay if they are 2W and 2A is the stall current.

Erdin the motors are not that big and as I said I measured them and stall was less than 1amp each so I should be fine. Id rather try and get this working as I have no real money to go off and buy anything..Im not kidding:(

I made a little picture to make it a little clearer of where I want to make sure I'm hooking things up properly. The image should server as a nice example of how some newbie like me can hook this all up

Normally with a h bridge Id expect that I would have 1 PWM and 1 digital pin to control direction but its seems from this setup that its using 2 PWM outs from the arduino. I am not sure of what advantage or disadvantage this has (apart that Im using 2 extra PWM on my uno).

Im trying to make a fast Gimbal head that uses a potentiometer as the initial controller for example 0 - 512 :: 512 - 1024 = PWM left and right for each motor 512 being middle or 0 PWM

Anyway just trying to sort out this pin stuff and in the meantime looking fro some nice code to run the PWM..

Normally with a h bridge Id expect that I would have 1 PWM and 1 digital pin to control direction but its seems from this setup that its using 2 PWM outs from the arduino.

Probably easiest to have the inputs not pwm'd, ie just h/l or l/h for direction, and pwm the enable pin (the one you're wondering about putting to 5v or digital pin). That way you only need one pwm pin per motor.

And yes you need to ground the driver board to the Arduino, else the signals which the Arduino "knows" as 5v have no 0v reference at the other end of the wire on the driver board.

ok so your saying to use a non pwm pin for example input 1 and 2 (lets say pins 1 and 2 from the Arduino for the direction change) say 0 1 for left and 1 0 for right and then use the enable pin with PWM for the speed?

I mean It doesnt bother me too much to leave it as originally intended but if its going to make code easier then that's a plus

Yes, I'm also used to connect the PWM to the Enable.
The Arduino uses a PWM of about 490Hz, which the L298 can easily follow that with the Enable.

As far as I know, there is only one exception. There is a driver that can't have a high frequency at the enable, but I forgot which one. Almost everything else uses the PWM at the enable.

You use Arduino pin 1, but pin 0 and 1 are the RX and TX for the communication to the PC (via the USB). Please don't use pin 0 and 1.

I wrote something that was wrong. I wrote: "The GND (X2-1) and +12V (X1-3) are not used", but I ment that those are not used for the motors. They are used for the power supply, but you figured that out already.

Yep that's exactly how I did it with the 298-equipped MotoMama shield.... except don't use pin 1 since pins 0 and 1 work together for serial i/o.

ok great so Ill be off to give it a spin soon..hopefully:)

ok so the motors are now moving which is nice so thanks for all the help:) Another thing i have to deal with now is the audible noise of the PWM frequency. Ive read up a little already about the time registers but now I'm confused more. What I am hearing is within the speech frequency range (using pin 10 as the PWM) I tried using the fast PWM on pin 6 but this just pushes the sound slightly higher in pitch. From what I understand the default value of the PWM for the slow and fast is 32000 and 64000(approx). This should for me be clearly out of the audible range but it clearly inst. Any points or perhaps what to look at..is 64000 approx the upper limit of the PWM frequency or could I go higher(not considering the external electronics) or should I go lower?.

The default frequency is 490 Hz.

Many motor libraries are able to select higher frequencies.
But you could also increase the PWM frequency.

There must be a better way specific for motors somewhere, I think it is this one, http://arduino.cc/forum/index.php/topic,16612.0.html

The L298 can't do very high frequencies, select the lowest frequency with little motor noise.

thanks Erdin, its late here so its some bed time reading for me. One question though may be how did you derive the switching rate/freq of the l298/ i looked at the data sheet but could not determine which of the values were important to figure it out.

I searched for "khz" in the datasheet.
The commutation frequency is 25kHz. So I'm guessing that is a rough indication for the whole chip, also for the enable.
The slowest is turn-off sink and is 3us, that would result into a higher frequency than 25kHz, so I stick with the value 25kHz as maximum frequency.

Ok this is what I cam up with so far, no Board to test it on until latter today when I get back to the workshop. One question would be is if I can use another value other than the values in the below chart - for the prescalar factor to bring it into any frequency range I want...for example 1.3 or something

/*
Changing the PWM frequency on Pins 11 and 3
Setting	Divisor	Frequency
0x01	1	31250  //should not be able to hear this though the frequency is a little high for the l298
0x02	8	3906.25
0x03	32	976.5625
0x04	64	488.28125 // default
0x05	128	244.140625
0x06	256	122.0703125
0x07	1024	30.517578125 //here is also good..in theroy but may be a little to low for the dc motor 
*/

int ENA = 11; //PWM 
int IN1 = 12;
int IN2 = 13;
int value = 4; //4 being the default giving  488.28125hz

void setup ()
{
  pinMode (ENA, OUTPUT);
  pinMode (IN1, OUTPUT);
  pinMode (IN2, OUTPUT);
TCCR2B = (TCCR2B & 0xF8) | value;// change the value of the (Timer 2 pins 11, 3?) register to a value ranging between 1 - 7
}

void test_motor_1 ()
{
  
  for (int i = 0; i < 256; i++)
  {
    digitalWrite (IN1, HIGH);
    digitalWrite (IN2, LOW);
    analogWrite (ENA, i);
    delay (50);
  }

  delay (3000);
  digitalWrite (IN1, LOW);
  
  for (int i = 0; i < 256; i++)
  {
    digitalWrite (IN1, LOW);
    digitalWrite (IN2, HIGH);
    analogWrite (ENA, i);
    delay (50);
    Serial.println(i);
  }

  delay (3000);
  digitalWrite (IN2, LOW);
}

void loop()
{
  test_motor_1();
}

I think you can set the TCCR2B after the pinMode() call in setup.

You can set the prescaler, but the prescaler is different than the prescaler of TIMER0 and TIMER1.
It is easier to use only the TCCR2B with the values in the table.
You can read all about the timers in the datasheet.

Could you clean up your code for the timer that is used ?
Decide if you use Timer1 or Timer2.
If you use Timer2, use output pin 11 or 3 for the PWM output and change the comment that with the text "timer0".

This is a library, http://code.google.com/p/arduino-pwm-frequency-library/

Yep sorry about the pins..changed now to pins 11,3. And nice Ill have a look at the library too

cleaned up a little - cant wait to go test it:)

#include <PWM.h>

int ENA = 11; //PWM 
int IN1 = 12;
int IN2 = 13;
int32_t frequency = 25000; //frequency (in Hz)

void setup ()
{
   InitTimersSafe(); 

  //sets the frequency for the specified pin
  bool success = SetPinFrequencySafe(ENA, frequency);
  if(success)
  {
  pinMode (ENA, OUTPUT);
  pinMode (IN1, OUTPUT);
  pinMode (IN2, OUTPUT);
  InitTimersSafe(); //initialize all timers except for 0, to save time keeping functions
  }

}

void test_motor_1 ()
{
  
  for (int i = 0; i < 256; i++)
  {
    digitalWrite (IN1, HIGH);
    digitalWrite (IN2, LOW);
    analogWrite (ENA, i);
    delay (50);
  }

  delay (3000);
  digitalWrite (IN1, LOW);
  
  for (int i = 0; i < 256; i++)
  {
    digitalWrite (IN1, LOW);
    digitalWrite (IN2, HIGH);
    analogWrite (ENA, i);
    delay (50);
    Serial.println(i);
  }

  delay (3000);
  digitalWrite (IN2, LOW);
}

void loop()
{
  test_motor_1();
}

that completely resolves the PWM noise issue it runs at 25k no problem and the l298 seems to handle that well. I have no heating issues and a fair amount of torque.
Here is a little test sketch I made that runs PWM on pin 9 forwards and backwards

// simple move forward and backward one motor on the l298n using pins 12 13 9/ 9 being PWM
#include <PWM.h>
int ENA = 9;
int IN1 = 12;
int IN2 = 13;
int32_t frequency = 20000; //frequency (in Hz)

void setup ()
{
  Serial.begin(9600);
  InitTimersSafe(); 
  bool success = SetPinFrequencySafe(ENA, frequency);
  pinMode (ENA, OUTPUT);
  pinMode (IN1, OUTPUT);
  pinMode (IN2, OUTPUT);
}

void test_motor_1 ()
{
  for (int i = 256; i < 512; i++)
  {
    digitalWrite (IN1, HIGH);
    digitalWrite (IN2, LOW);
    analogWrite (ENA, i);
    delay (30);
    Serial.println(i);
  }

  delay (100);
  digitalWrite (IN1, LOW);
  
  for (int i = 256; i < 512; i++)
  {
    digitalWrite (IN1, LOW);
    digitalWrite (IN2, HIGH);
    analogWrite (ENA, i);
    delay (30);
   Serial.println(i);
  }

  delay (100);
  digitalWrite (IN2, LOW);
}

void loop()
{
  test_motor_1();
}