L298N Motor Drive Board

Anything Arduino

L298N Motor Drive Board

Postby Tim Laren » Sun Nov 27, 2011 6:51 pm

The L298N Motor Drive Board is based on the L298N chip. It sports two full bridges capable of driving two motors up to 48 Volts and 2 Amps each maximum. This board has all the glue to support this chip along with LEDs that indicate the H-bridge outputs for easy debugging.

You can make a cable or simply use jumper wires to connect it to your Arduino or other Micro Board like a PIC or AVR. Each motor has it's own 2 pin screw terminal block. The logic and motor power are separate and brought in on a 3 screw terminal block. There is also a jumper block of 4 jumpers. The first two are ENA and ENB. If these jumpers are installed then you should not connect the ENA and ENB pins to your Arduino as these are overrides that force the H-bridges to always be enabled. If you want to control the speed of the motors using the Enables then you should remove these jumpers. The second two jumpers, are a little misleading in their labels, PWM1 and PWM2. They are actually Current Sense Shunts. If you want to use the Current Sense feature of the L298N then you have to install resistors on the board and remove these jumpers. If you remove these jumpers and do not install resistors your motors will not run. Please refer the N298N spec sheet if you want to utilize this option.

There are two identical circuits, A and B. Each has an Enable, that must be high for the bridge to drive your motor. You can also use the Enable to adjust the speed of the motors with a PWM output from your Arduino. There is also an IN1 and IN2 for each bridge. On my board the second bridge has IN3 and IN4. Each of these inputs controls the corresponding output as follows:
IN1 - HIGH OUT1.1 HIGH
IN1 - LOW OUT1.1 LOW
IN2 - HIGH OUT1.2 HIGH
IN2 - LOW OUT1.2 LOW
IN3 - HIGH OUT2.1 HIGH
IN3 - LOW OUT2.1 LOW
IN4 - HIGH OUT2.2 HIGH
IN4 - LOW OUT2.2 LOW
Remember that the motor requires one lead to be high and the other to be low to run. If you reverse these leads it will run in the other direction.

There are 5 LEDs, four Green and one Red. The Red LED is the power light and the four Green LEDs are indicators for the outputs of the H-bridge.

Here is a simple configuration two run two motors:
Jumpers:
ENA Removed
ENB Removed
PWM1 Installed
PWM2 Installed

On the header:
GND ==> GND on Arduino
5V ==> N/C (5V Logic power is available from on-board regulator)
ENB ==> Arduino 6
ENA ==> Arduino 3
IN1 ==> Arduino 2
IN2 ==> Arduino 4 (Corrected Jan 1, 2012, was 3, Thanks Julian)
IN3 ==> Arduino 7
IN4 ==> Arduino 8

Power Terminal Block:
5V ==> N/C
GND ==> Ground on Motor Power Supply
VCC ==> +12V (for 12 volt motor)

OUT1 Terminal Block:
Pin1 ==> Motor 1 +
Pin2 ==> Motor 1 -

OUT2 Terminal Block:
Pin1 ==> Motor 2 +
Pin2 ==> Motor 2 -

Here is some sample code:
Code: Select all
/*
  L298N Test Code
  hacker.instanet.net
  Tim Laren timl@instanet.com
  This example code is in the public domain.
 */

#define OUT1PIN1 2    // H-bridge OUT1.1
#define OUT1PIN2 4    // H-bridge OUT1.2
#define ENABLE1  3    // H-bridge Ena 1
#define OUT2PIN1 7    // H-bridge OUT2.1
#define OUT2PIN2 8    // H-bridge OUT2.2
#define ENABLE2  6    // H-bridge Ena 2


void setup() {
  // setup motor pins
  pinMode(OUT1PIN1, OUTPUT);
  pinMode(OUT1PIN2, OUTPUT);
  pinMode(ENABLE1, OUTPUT);

  digitalWrite(ENABLE1, HIGH);   // set the enables to high
}


void loop() {
  digitalWrite(OUT1PIN1, LOW);   // set leg 1 of the H-bridge low
  digitalWrite(OUT1PIN2, HIGH);  // set leg 2 of the H-bridge high

  delay(5000);

  digitalWrite(OUT1PIN1, HIGH);  // set leg 1 of the H-bridge high
  digitalWrite(OUT1PIN2, LOW);   // set leg 2 of the H-bridge low

  delay(5000);
}



This will run motor 1 forward for 5 seconds then reverse it. Rinse and repeat.

Here are files http://hacker.instanet.net/arduino/files/L298N/
This is the ZIP http://hacker.instanet.net/arduino/files/L298N/L298N_Sample.zip

Speed control a little later.

Tim
Image
Tim Laren
Site Admin
 
Posts: 134
Joined: Thu Mar 04, 2010 8:25 pm

Re: L298N Motor Drive Board

Postby Tim Laren » Sun Nov 27, 2011 11:02 pm

Did another little program with speed control.

All it does is start at nothing and increase to full speed, then slow to a stop and reverse, increase to full and then slow to a stop; Rinse and repeat.

Here is the code:

Code: Select all
/*
  L298N Test Code w/ Speed Control
  hacker.instanet.net
  Tim Laren timl@instanet.com
  This example code is in the public domain.
 */

#define OUT1PIN1 2    // H-bridge OUT1.1
#define OUT1PIN2 4    // H-bridge OUT1.2
#define ENABLE1  3    // H-bridge Ena 1
#define OUT2PIN1 7    // H-bridge OUT2.1
#define OUT2PIN2 8    // H-bridge OUT2.2
#define ENABLE2  6    // H-bridge Ena 2

void setup() {
  // setup Motor 1 pins
  pinMode(OUT1PIN1, OUTPUT);
  pinMode(OUT1PIN2, OUTPUT);
 
  // setup Motor 2 pins
  pinMode(OUT2PIN1, OUTPUT);
  pinMode(OUT2PIN2, OUTPUT);
}

void loop() {
  digitalWrite(OUT1PIN1, LOW);   // set leg 1 of the H-bridge low
  digitalWrite(OUT1PIN2, HIGH);  // set leg 2 of the H-bridge high
  Ramp(ENABLE1,0,255,1,10);
  Ramp(ENABLE1,255,0,-1,10);
  digitalWrite(OUT1PIN1, HIGH);  // set leg 1 of the H-bridge high
  digitalWrite(OUT1PIN2, LOW);   // set leg 2 of the H-bridge low
  Ramp(ENABLE1,0,255,1,10);
  Ramp(ENABLE1,255,0,-1,10);
}

void Ramp(int Motor, int Start, int End, int Inc, int Del) {
  /* Ramps from PWM of Start to End
     adding Inc each time
     delay of Del MS each time thru the loop
  */
  for(int i=Start;i<End;i = i + Inc) {
    analogWrite(Motor, i);       // Motor 1 Speed
    delay(Del);
  }
}



Ramp takes 4 parameters:
Start ==> Starting speed
End ==> Ending speed
Inc ==> count to add to current speed
Del ==> Delay time between each step

I'm not sure this routine will be useful in the real world but it shows a lot about how to control the motor with the Motor Controller Board. I hope you find it informative.

A little note about one thing you may wonder about in my code: I use the define rather then defining an 'int' with a specific value.
#define OUT1PIN1 2
rather than
int OUT1PIN1 = 2;
Reason being is I never modify those so they are constant, therefor I used constants. It takes a little less Flash and for this example that does not matter, but in other projects I have run of code space and those bytes mattered. I feel it's a good practice.

One more in this set will be using the Motor Controller Board for a stepper motor. Stepper motors are a lot of bit flipping and there are libraries for the Arduino so I will not start from scratch for that one.

Please feel free to post messages with questions or experiences you have with your Arduino here to share with everyone.

Tim
Image
Tim Laren
Site Admin
 
Posts: 134
Joined: Thu Mar 04, 2010 8:25 pm

Re: L298N Motor Drive Board

Postby hseidema » Mon Feb 04, 2013 10:41 pm

For anyone who wants to use this for 4 pin stepper motors, and is frustrated by the availability of info on the internet for this board, it's very simple:

Leave all breakers pins connected (ENA and ENB automatically tied to 5V).
Connect 5V and Ground from your Arduino to the 5V and GND pins on the main header row.
Connect the IN1 IN2 IN3 and IN4 pins on the main header row to any 4 digital pins on the arduino board.
Supply your supply voltage to to the VCC screw header (or 5V if you're using a 5V power supply or supplying from the arduino), and probably a good idea to connect the supply ground to the arduino ground as well.
Connect your 4 pins of the stepper to the A1, A2, B1, B2 pins on the board, and make sure that your motor leads are connected to the matching headers on the board.

Now that you're all hooked up, you can use the built in Stepper library on the arduino. Instructions for that can be found here: http://arduino.cc/en/Reference/StepperConstructor

Like I said, pretty simple, but if, like me, you still would like to have had a really quick and easy how-to: there it is.

Cheers
hseidema
 
Posts: 1
Joined: Mon Feb 04, 2013 10:31 pm

Re: L298N Motor Drive Board

Postby llb@knightscope.com » Wed Jul 23, 2014 4:36 pm

Hi, and thanks for the code above! It's awesome

So I am using the L298N to drive two linear HAD1 linear actuators (12V each and a no-load drive current of ~950mA each)
http://www.alibaba.com/showroom/mini-li ... -had1.html

I am also using a current sensor per motor to get feedback of what the motor is doing (only sensors I have available, but I can detect of the motors are moving or stopped). I am using two ACS714 current sensors. The supply voltage for each is 4.5V to 5.5V and Supply Current is 10mA to 13ma:
http://www.pololu.com/product/1187/specs

And Here is the circuit diagram that I made for my actual setup (an Arduino UNO, two current sensors, to linear actuators, and one motor drive):
https://www.dropbox.com/s/ouxfrcutw5lcj6a/Actuator.jpeg


Will this setup work? Will I have enough current/power coming out of the 5V of the arduino to power both the L298N logic and the two ACS714 sensors?
llb@knightscope.com
 
Posts: 1
Joined: Wed Jul 23, 2014 4:04 pm

Re: L298N Motor Drive Board

Postby Tim Laren » Wed Jul 23, 2014 11:20 pm

The L298N Modules I have have an integrated 5V Regulator that will supply about an Amp. You can run the Arduino and all your sensors off that.
If you are not using that board and using the Arduino to supply the current then you are limited to less than 500 ma including the UNO itself.
Either way I think you are safe.
Image
Tim Laren
Site Admin
 
Posts: 134
Joined: Thu Mar 04, 2010 8:25 pm

Re: L298N Motor Drive Board

Postby jerryleblanc56 » Tue Jun 23, 2015 1:15 pm

I am building a project with the Tamiya dual-drive geared motors and they cannot handle much power.
You're only supposed to drive them with 3V
You suggested speed control via the Arduino PWM but it's not clear if I can set a lower limit on the voltage.
Should I look for a different motor controller to avoid burning my puny motors?
jerryleblanc56
 
Posts: 1
Joined: Tue Jun 23, 2015 1:09 pm

Re: L298N Motor Drive Board

Postby Tim Laren » Tue Jun 23, 2015 4:44 pm

What PWM does is turn on and off the voltage very fast. The ratio of that on to off period changes the voltage 'seen' by the load (motor). If you have the PWM set at 10% then the motor will 'see' .5 Volts. If you set in software the max PWM value at 153 then the motor should never see more than 3 Volts.

The LandShark kit I have was originally designed to use 4 AA batteries or 6 Volts. I have tested it with two 3.7 V LiPo batteries and just limit the top motor speed/power and have run the two test cars for several hours with no sigh of motor damage. The motors on the LandShark are rated at 3-6V.

Tim
Image
Tim Laren
Site Admin
 
Posts: 134
Joined: Thu Mar 04, 2010 8:25 pm


Return to Arduino

Who is online

Users browsing this forum: Bing [Bot] and 1 guest

cron