loading

The L298n is a simple dual channel motor controler for Arduino.Featuring functions like direction change and PWM
speed control makes it easy and fun to use.And it is cheap,realy cheap.
You can get it on ebay for as low as 2$

But it is hard to find a good library for it,so i made one.
Download link https://drive.google.com/open?id=0Bxra0Xe0UREkZWdSZ2ExUGVEN3M

All you need to do is download the .h file and include it in your arduino sketch using:

#include "l298n_lib.h"

In setup part you need to initialize pins used to connect module and arduino:

set_pins (IN1,IN2,IN3,IN4,enableA,enableB);

enable pins must be pwm pins

example;

set_pins(2,3,4,5,9,11);

To controll motors in loop part of code you can write following commands:

dmot('F',127)....dmot means right motor,F is for Forward and 127 is pwm speed,can range from 0 to 255
lmot('B',127)...lmot means left motor,B is for Backward and 127 is pwm speed,can range from 0 to 255

And you can stop motors by:

dmot_stop();
lmot_stop():

Using this commands whith some serial communication and case statements can yield a very nice RC wireless car,the exaple will be included in file.

If you have read all to this point,thank you.

Follow me on twitter @ivica3730k

Have fun and have a nice day.

<p>bonjour</p><p>j'ai essay&eacute; avec votre programmation mais rien ne fonctionne </p><p>j'avais &eacute;crit dans un autre module :</p><p>Hello<br>I tried it with your programming but nothing works<br><br>I had written in another shetch:</p><p>//la pwm pour la vitesse<br> int ENA = 10;<br> int ENB = 5;<br>// les broches de signal pour le sens de rotation<br>//const int ENA = 10;<br>//const int ENB = 5;<br>const int in1 = 9;<br>const int in2 =8;<br>const int in3 = 7;<br>const int in4 =6;<br><br>void setup() {<br> pinMode(ENA , OUTPUT);<br> pinMode(in1, OUTPUT);<br> pinMode(in2, OUTPUT);<br> pinMode(ENB , OUTPUT);<br> pinMode(in3, OUTPUT);<br> pinMode(in4, OUTPUT);<br>Serial.begin(15200);<br>//on demarre moteur en avant et en roue libre<br> analogWrite(ENA,0);<br> <br> digitalWrite(in1, LOW);<br> digitalWrite(in2, HIGH);<br><br> digitalWrite(in3, LOW);<br> digitalWrite(in4, HIGH);<br> analogWrite(ENB,0); <br><br>}<br>#include &quot;l298n_lib.h&quot;<br>void loop() {<br> // put your main code here, to run repeatedly:<br>int vitesse = analogRead;<br><br>if(vitesse &gt; 512)<br>{<br> vitesse -= 512;<br> digitalWrite(in1, LOW);<br> digitalWrite(in2, HIGH);<br> digitalWrite(in3, LOW);<br> digitalWrite(in4, HIGH);<br> <br> <br> Serial.print(&quot;+&quot;);<br>} <br> else<br> {<br> vitesse = 512-vitesse;<br> digitalWrite(in1, LOW);<br> digitalWrite(in2, HIGH); <br> digitalWrite(in3, LOW);<br> digitalWrite(in4, HIGH);<br> <br> Serial.print(&quot;-&quot;);<br>}<br>vitesse /= 2;<br>//analogWrite( ENA);<br>Serial.println(vitesse);<br>delay(50);<br>}</p><p>et cela ne fonctionne pas </p><p>j'ai brancher comme sur le sch&eacute;ma</p><p>I plugged the connectors as in the diagram but it does not work<br><br>I am all new in arduino</p><p>Yesterday I had 2 engines that worked when I added the 4 nothing works<br><br>Thank you for your help</p><p>hier j'avais seulement deux moteurs qui fonctionnaient</p><p>je n'arrive pas &agrave; faire fonctionner les 4 moteurs</p>
<p>Sorry for slow response time.I dont know why my library is not working.When you define set_pins you set it like (IN1,IN2,IN3,IN4,enableApin,enableBpin).</p><p>Once you done that you can write </p><p>dmot('F',speed) // d stands for &quot;desni&quot; here in Croatia</p><p>You dont neet to have any digitalWrite(...) in your file.<br></p><p>One more problem that comes to my mind is,what kind of engines do you use?</p><p>digitalWrite(in1, LOW);<br>digitalWrite(in2, HIGH);<br>digitalWrite(in3, LOW);<br>digitalWrite(in4, HIGH);</p><p>and can you expalain me this part of code please?</p><p>Thanks :)</p>
<br><p>Ok thanks for your reply<br><br>The engines now work with this sketch<br><br>The radar on the computer works.<br><br>Not on the car.<br><br>Thank you for your help I will try again with your program</p><p>#include &lt;Ultrasonic.h&gt;<br><br><br>int trigPin = 13; // <br>int echoPin = 12; //<br>float v=331.5+0.6*20; // m/s // <br><br>//moteur droit<br>int L298N_IN1=2;<br>int L298N_IN2=4;<br>int L298N_ENA =3;<br>//moteur gauche<br>int L298N_IN3=7;<br>int L298N_IN4=8;<br>int L298N_ENB =5;<br><br><br><br><br>void setup() //<br>{ <br>pinMode(L298N_IN1, OUTPUT);<br>pinMode(L298N_IN2, OUTPUT);<br>pinMode(L298N_IN3, OUTPUT);<br>pinMode(L298N_IN4, OUTPUT);<br> pinMode(L298N_ENA, OUTPUT);<br> pinMode(L298N_ENB, OUTPUT);<br><br> <br> Serial.begin(9600); // <br> pinMode(trigPin, OUTPUT); // <br> pinMode(echoPin, INPUT); // <br> }<br>float distanceCm(){ // <br> // send sound pulse // <br> digitalWrite(trigPin, LOW); // <br> delayMicroseconds(3); // <br> digitalWrite(trigPin, HIGH); // <br> delayMicroseconds(5); // <br> digitalWrite(trigPin, LOW); // <br><br> // listen for echo // <br> float tUs = pulseIn(echoPin, HIGH); // microseconds //<br> float t = tUs / 1000.0 / 1000.0 / 2.0; // s // <br> float d = t*v;<br> return d*100; // cm // <br> }<br><br><br>void loop() <br>{ <br>int d=distanceCm(); // <br>Serial.println(d, DEC); <br>delay(200); // ms //<br><br> digitalWrite(L298N_ENA, HIGH);<br> digitalWrite(L298N_ENB, HIGH);<br> <br><br> <br>startCar();<br>delay(1000);<br>stopCar();<br>delay(1000);<br>leftTurn();<br>delay(1000);<br>rightTurn();<br>delay(1000);<br>}<br>void startCar()<br>{<br> <br> digitalWrite(L298N_IN1, HIGH);<br> digitalWrite(L298N_IN2, LOW);<br> digitalWrite(L298N_IN3, HIGH);<br> digitalWrite(L298N_IN4, LOW);<br>}<br>void stopCar()<br>{<br>digitalWrite(L298N_IN1, LOW);<br> digitalWrite(L298N_IN2, LOW);<br> digitalWrite(L298N_IN3, LOW);<br> digitalWrite(L298N_IN4, LOW);<br><br>}<br>void leftTurn()<br>{<br>digitalWrite(L298N_IN1, LOW);<br> digitalWrite(L298N_IN2, HIGH);<br> digitalWrite(L298N_IN3, HIGH);<br> digitalWrite(L298N_IN4, LOW);<br><br> }<br> void rightTurn()<br>{<br> <br>digitalWrite(L298N_IN1,HIGH );<br> digitalWrite(L298N_IN2, LOW);<br> digitalWrite(L298N_IN3,LOW );<br> digitalWrite(L298N_IN4, HIGH);<br> }</p>
<p>EDIT: I see where problem is,you dont have set_pins(); in your setup part,that is needed for proper function of library.And #include... shoud be on top</p>
<p>I am having trouble downloading the .h file, any help? </p>
<p>I will give you direct link </p><p><a href="http://ivica-matic.tk/downloads/L298N_library_final.7z" rel="nofollow">http://ivica-matic.tk/downloads/L298N_library_fina...</a></p><p>Download 7 zip :)</p>
<p>une pr&eacute;cision </p><p>j'ai mis </p><p>A precision I put</p><p>analogWrite(ENA,500);au lieu de zero idem pour ENB</p><p>seul les moteurs ENA fonctionnent</p><p>Only ENA engines operate</p>

About This Instructable

2,135views

10favorites

License:

Bio: Short way,Everything Arduino,AVR... C++,C# and Basic. Full home automatization is my dream I love playing whith stuff that can kill you or ... More »
More by Ivica_Matic:L298n Arduino Library How to Test Arduino Cirtcuit and Code Online How to hook up voltage divider 
Add instructable to: