Anda di halaman 1dari 7

technology

workshop

living

food

play

outside

Arduino Modules - L298N Dual H-Bridge Motor Controller


by Reichenstein7 on August 27, 2014

Table of Contents
Arduino Modules - L298N Dual H-Bridge Motor Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Intro: Arduino Modules - L298N Dual H-Bridge Motor Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Step 1: Getting to know your L298N Dual H-Bridge Motor Controller module: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Step 2: Wiring to an Arduino: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Step 3: Arduino Sketch Considerations: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Step 4: Arduino Sketch Example: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

File Downloads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Related Instructables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Advertisements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

http://www.instructables.com/id/Arduino-Modules-L298N-Dual-H-Bridge-Motor-Controll/

Intro: Arduino Modules - L298N Dual H-Bridge Motor Controller


Quick and simple start guide for using and exploring an L298N Dual H-Bridge Motor Controller module with an Arduino.
The model in the example I am using is from Ebay.
Materials needed:
L298N Dual H-Bridge Motor Controller module (various models will work)
Male to Female jumper wires
An Arduino, any flavor.
A DC power supply, 7-35v
A motor that is the correct voltage for your power supply used.

Step 1: Getting to know your L298N Dual H-Bridge Motor Controller module:
Usage:
H-Bridge's are typically used in controlling motors speed and direction, but can be used for other projects such as driving the brightness of certain lighting projects such
as high powered LED arrays.
How it works:
An H-Bridge is a circuit that can drive a current in either polarity and be controlled by *Pulse Width Modulation (PWM).
* Pulse Width Modulation is a means in controlling the duration of an electronic pulse. In motors try to imagine the brush as a water wheel and electrons as a the flowing
droplets of water. The voltage would be the water flowing over the wheel at a constant rate, the more water flowing the higher the voltage. Motors are rated at certain
voltages and can be damaged if the voltage is applied to heavily or if it is dropped quickly to slow the motor down. Thus PWM. Take the water wheel analogy and think of
the water hitting it in pulses but at a constant flow. The longer the pulses the faster the wheel will turn, the shorter the pulses, the slower the water wheel will turn. Motors
will last much longer and be more reliable if controlled through PWM.
Pins:
Out 1: Motor A lead out
Out 2: Motor A lead out
Out 3: Motor B lead out
Out 4: Mo (Can actually be from 5v-35v, just marked as 12v)
GND: Ground
5v: 5v input (unnecessary if your power source is 7v-35v, if the power source is 7v-35v then it can act as a 5v out)
EnA: Enables PWM signal for Motor A (Please see the "Arduino Sketch Considerations" section)
In1: Enable Motor A
In2: Enable Motor A
In3: Enable Motor B
In4: Enable Motor B
EnB: Enables PWM signal for Motor B (Please see the "Arduino Sketch Considerations" section)
Specifications:
Double H bridge Drive Chip: L298N
Logical voltage: 5V Drive voltage: 5V-35V
Logical current: 0-36mA Drive current: 2A (MAX single bridge)
Max power: 25W
Dimensions: 43 x 43 x 26mm
Weight: 26g
*Built-in 5v power supply, when the driving voltage is 7v-35v

http://www.instructables.com/id/Arduino-Modules-L298N-Dual-H-Bridge-Motor-Controll/

Step 2: Wiring to an Arduino:


There are several different models of these L298N Dual H-Bridge Motor Controllers. The generic wiring schematic above should do the trick for most.
Two things to mention;
Make sure you have all of your grounds tied together; Arduino, Power source, and the Motor controller.
The PWM Pins are unnecessary if you do not want to control PWM features.

Step 3: Arduino Sketch Considerations:


The Arduino code sketch is pretty straight forward. Since there isnt a library for the L298N Dual H-Bridge Motor Controller you just have to declare which pins the
controller is hooked to.
The int dir(number)Pin(letter) pins can be connected to any available digital pin you have available, as long as you declare the correct pin in your sketch. This
makes the L298N Dual H-Bridge Motor Controller very versatile if your project is using a lot of Arduino pins.
The int speedPin(letter) pins need to be connected to a PWM pin on the Arduino if you want to enable speed control through PWM.
As a quick cheat I have included a list of PWM pins for the main two types of Arduino's I use:
AT MEGA PWM: 2 to 13 and 44 to 46. Provide 8-bit PWM output with the analogWrite() function.
UNO PWM: 3, 5, 6, 9, 10, and 11. Provide 8-bit PWM output with the analogWrite() function.

Step 4: Arduino Sketch Example:


This code example I wrote to allow a serial monitor program such as Putty to control the L298N Dual H-Bridge Motor Controller via a keyboard with key presses.
Controls:
Key .......... Motor
1 ............... Motor 1 Forward
2 ............... Motor 1 Stop
3 ............... Motor 1 Reverse
4 ............... Motor 2 Forward
5 ............... Motor 2 Stop
6 ............... Motor 2 Reverse
Code:
Fixed the formatting issues (12/29/2014):

http://www.instructables.com/id/Arduino-Modules-L298N-Dual-H-Bridge-Motor-Controll/

Download the attached L298N_Dual_H_Bridge_Keyboard_Control_Reichenstein7.ino


Alternate Download: http://www.mediafire.com/download/z654e8at1b21d7e/L298N_Dual_H_Bridge_Keyboard_Control_Reichenstein7.ino

File Downloads

L298N_Dual_H_Bridge_Keyboard_Control_Reichenstein7.ino (2 KB)
[NOTE: When saving, if you see .tmp as the file ext, rename it to 'L298N_Dual_H_Bridge_Keyboard_Control_Reichenstein7.ino']

Related Instructables

Dual H-Bridge L298 Breakout


Board Homemade by
BIGDOG1971

Control DC and
stepper motors
with L298N Dual
Motor Controller
Modules and
Arduino by
tronixlabs

Toy to Obstacle
Avoider by
eberhard1

Dual Motor
Driver with
Arduino using a
SN754410NE
Quad Half HBridge by
ArduinoFun

How to Control
any R/C car with
an airplane
transmitter! by
vkarpuram

Getting Started
With The
MC33932 Dual
Motor Shield by
JayconSystems

Advertisements

Comments
32 comments Add Comment

Reichenstein7 says:

Dec 29, 2014. 2:02 PM REPLY

I have added a link to the .ino file via Mediafire for those who are still seeing formatting errors.

vjoshi14 says:

Apr 7, 2015. 9:22 AM REPLY


Please help, i am not able to run the DC motor with this h bridge. it has in1, in2, in3, in4, e-a, e-b, gnd, 5v, 12v on one side and 4 screw ports on the other.
please tell me do I need to add a power supply on the ports too and also on which port should I add the motor to, and the most important thing this program is
not working for me.

LeoZ1 says:

Nov 2, 2014. 2:29 PM REPLY


I just got my controller working! It's exactly the same model as the one in this tutorial, which was very helpful! But there's a trick to this model, just something
I noticed. Instead of declaring a PWM speed control pin, I just left the jumpers on ENA and ENB on, because they automatically enable the Motor A and B.
Then I just PWM'ed either IN pins on the controller. I found that I could still vary the speed of the motor this way, even with one less connection. Is this the
correct way to go? I want to make sure that even if the motor seems to work, I'm not damaging anything or wasting a lot of energy in the process. Because I
have noticed that the heat sink gets extremely hot. (I'm inputting around 14-16V).

MadhusudanK says:

Apr 5, 2015. 4:36 AM REPLY


Even I want to know if this is the correct way or not, because I have only three PWM pins left and I need to control 3 DC geared motors with those.
Please reply @ Reichenstein7

http://www.instructables.com/id/Arduino-Modules-L298N-Dual-H-Bridge-Motor-Controll/

nimz says:

Mar 16, 2015. 3:03 AM REPLY

Can i use it run 4 dc motors?

killercatfish says:

Mar 12, 2015. 5:44 AM REPLY


Any recommendation for a power supply? My students want to use this to run two dc motors on a car chasis. So smaller the better to fit on there.
Thanks!

Reichenstein7 says:

Mar 14, 2015. 7:26 PM REPLY


I have been using heavy 12v SLA batteries and wall-warts myself, but any DC source that ranges 7-35v should do the trick.

tintinmovie says:

Jan 18, 2015. 3:09 PM REPLY


I made it and it works except the stop. I added a delay of 2 seconds but that did nothing. When I type 2(Stop) it will go forward or backwards depending on
which one it is on. So if it is going forward it will go backwards and if it's going backward it will go forward. Can you help me with this problem?

Reichenstein7 says:

Mar 10, 2015. 8:35 PM REPLY


This is probably a wiring problem, I have had similar occur here in a botched install. Check the wires against the above diagram.

AlexanderR2 says:

Mar 2, 2015. 7:32 AM REPLY


I have a quick question. When you mention "make sure the power source, arduino and motor driver grounds are tied together" Did you mean all of them
soldered into just one cable or..? I'm new to electronics, and i'm quite confused since I only see one ground connection in your diagram.
Thanks in advance.

AlexanderR2 says:

Mar 2, 2015. 7:35 AM REPLY

If its not what you meant, where would I connect the power source ground cable?
Thanks, and sorry if its a stupid question. Im CS student, this is for a robotics investigation Im doing with arduino :)

Reichenstein7 says:

Mar 10, 2015. 8:32 PM REPLY

Your question is not stupid. =)


In most of these small projects you want to make sure all of the grounds are connected together otherwise anomalies may occur (motors stuttering,
intermittent brownouts, etc). There are instances where you would want grounds to be isolated, but not here.
What I mean by the above is make sure the ground for the battery or power source, the motor controller and Arduino are all connected together to
ensure the desired output works reliably.

inese.eglava says:

Mar 4, 2015. 10:28 AM REPLY


My Arduino didn't change speed through PWM with direct connection. I put 2k resistor between ENA (ENB) and Arduino Output pins 9 (10).

8401288342 says:

Feb 16, 2015. 11:40 PM REPLY

What is the maximum delivered voltage and current at the Motor end.

TahirK says:

Jan 9, 2015. 11:50 PM REPLY


Can i use this controller with servo motor voltage regulator? means if voltage down that 12v then motor rounding on clock vise and if voltage up than 12v
then motor rounding anti clock vise.

AbdulM5 says:

Dec 30, 2014. 6:10 AM REPLY

if i am using pwm, so do i have to remove jumper from enable pins. Also which pin should be connected to arduino then.

Reichenstein7 says:

Dec 30, 2014. 10:01 AM REPLY


If you want to control the motor speed via PWM, you will have to pull the jumpers off of the 2 ENA pins. Each of these should be connected to a PWM
capable pin on the Arduino. The 3rd picture in this Instructable shows a wiring diagram.
----As a quick cheat I have included a list of PWM pins for the main two types of Arduino's I use:
AT MEGA PWM: 2 to 13 and 44 to 46. Provide 8-bit PWM output with the analogWrite() function.
UNO PWM: 3, 5, 6, 9, 10, and 11. Provide 8-bit PWM output with the analogWrite() function.

http://www.instructables.com/id/Arduino-Modules-L298N-Dual-H-Bridge-Motor-Controll/

Vova.Kuzmenkov0 says:

Nov 21, 2014. 6:49 AM REPLY

Hey Reichenstein7!
There is an error in your sketch: you've missed initialisation of serial port in void setup() {..} section
Please add the following:
void setup() {
...*your initialisations here*
Serial.begin(9600);
Serial.println("Enter Number 1 to 6 to control the motor");
}
Then the code will work! You'll be able to control motors via serial monitor.
All in all your guide was a great help to me! thanks!
Regards,
Vova

Reichenstein7 says:

Nov 21, 2014. 8:03 AM REPLY


I will take a look at the code this afternoon, sometime things get missed in the copy + paste of things, but I know the code I have added to my repo
works.

Reichenstein7 says:

Nov 26, 2014. 10:53 AM REPLY


Okay, so a little late checking up on this. Apparently this site is really bad for putting this code on. If you click the link and open the code in a
webpage, characters are lost or incorrect.
If you click the link and save it, The files name and all formatting is lost. There also seem to be a few characters that cannot be saved and show up
as boxes.
I used to post code at the end of the Instructable, but had similar bad results.
Anybody have a better way of sharing code on this site or should I start linking it to somewhere like Mediafire?

quadro123 says:

Nov 13, 2014. 10:21 AM REPLY


When i wanna to control 24V dc module, jumper near left capacitor need to be pocked in? Or take it out? Also when i Connect 24V power supply, do i really
need to connect 5V from arduino uno?

vsharma23 says:

Oct 10, 2014. 1:13 AM REPLY


I m new in Arduino and i mtrying to make robot . I m using this motor controler can you plz help me how to connect it with arduino and dc motor.??
plz reply..

Reichenstein7 says:

Oct 11, 2014. 6:30 AM REPLY


Sure, I use this model in a few of my other projects. I will try to publish a writeup in the next couple of days. Feel free to follow me, I am in the middle of
publishing instructions for a variety of Arduino compatible sensors and controllers.

http://www.instructables.com/id/Arduino-Modules-L298N-Dual-H-Bridge-Motor-Controll/

vsharma23 says:

Oct 11, 2014. 8:53 PM REPLY

ok thanks.

Reichenstein7 says:

Nov 5, 2014. 6:19 AM REPLY

I realized a few days ago that I have used this controller in another instructables. Take a look here:
http://www.instructables.com/id/Project-Launch-Platform-Robot-Part-1-The-Head/

yakeen4u says:

Sep 16, 2014. 6:31 PM REPLY

this project include joystick?

Reichenstein7 says:

Sep 17, 2014. 5:36 AM REPLY


This is a really quick sketch that shows how to call the features of this module. My sketch only includes keyboard control, but can easily adapted to a
joystick.
* ( Depending upon the type of joystick you use, you could remap the joystick with the keys from my sketch and that would work. However I would add to
it to at least take advantage of the PWM [Variable speed]
To change speed simply change the variable in " (speedPinA, 255) " and add it as another case. )

rafael.duarte.752 says:

Sep 6, 2014. 7:36 PM REPLY

Can you share again the arduino sketch please?


Thanks in advance

Reichenstein7 says:

Sep 6, 2014. 9:03 PM REPLY

The sketch is attached. The formatting gets all messed up if I paste it.

carlos66ba says:

Aug 28, 2014. 12:51 PM REPLY


Nice instructable. I have a question: do you know what is the maximum switching frequency that the L298N supports? Thanks!

omnibot says:

Aug 28, 2014. 10:57 PM REPLY


40kHz commutation frequency according to the datasheet at http://www.sparkfun.com/datasheets/Components/General/L298N.pdf

Arman5592 says:
Great ! I wanted to buy one of these and I was confused with all the pins and how to connect to an arduino .

http://www.instructables.com/id/Arduino-Modules-L298N-Dual-H-Bridge-Motor-Controll/

Aug 28, 2014. 11:04 AM REPLY

Anda mungkin juga menyukai