Introduction: L298n Arduino Library

Picture of L298n Arduino Library

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

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



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:


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.


awe070707 (author)2017-08-12

hi i think this module no need library..

jeanlouisquero made it! (author)2017-02-15


j'ai essayé avec votre programmation mais rien ne fonctionne

j'avais écrit dans un autre module :

I tried it with your programming but nothing works

I had written in another shetch:

//la pwm pour la vitesse
int ENA = 10;
int ENB = 5;
// les broches de signal pour le sens de rotation
//const int ENA = 10;
//const int ENB = 5;
const int in1 = 9;
const int in2 =8;
const int in3 = 7;
const int in4 =6;

void setup() {
pinMode(ENA , OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(ENB , OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
//on demarre moteur en avant et en roue libre

digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);

digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);

#include "l298n_lib.h"
void loop() {
// put your main code here, to run repeatedly:
int vitesse = analogRead;

if(vitesse > 512)
vitesse -= 512;
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);

vitesse = 512-vitesse;
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);

vitesse /= 2;
//analogWrite( ENA);

et cela ne fonctionne pas

j'ai brancher comme sur le schéma

I plugged the connectors as in the diagram but it does not work

I am all new in arduino

Yesterday I had 2 engines that worked when I added the 4 nothing works

Thank you for your help

hier j'avais seulement deux moteurs qui fonctionnaient

je n'arrive pas à faire fonctionner les 4 moteurs

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

Once you done that you can write

dmot('F',speed) // d stands for "desni" here in Croatia

You dont neet to have any digitalWrite(...) in your file.

One more problem that comes to my mind is,what kind of engines do you use?

digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);

and can you expalain me this part of code please?

Thanks :)

Ok thanks for your reply

The engines now work with this sketch

The radar on the computer works.

Not on the car.

Thank you for your help I will try again with your program

#include <Ultrasonic.h>

int trigPin = 13; //
int echoPin = 12; //
float v=331.5+0.6*20; // m/s //

//moteur droit
int L298N_IN1=2;
int L298N_IN2=4;
int L298N_ENA =3;
//moteur gauche
int L298N_IN3=7;
int L298N_IN4=8;
int L298N_ENB =5;

void setup() //
pinMode(L298N_IN1, OUTPUT);
pinMode(L298N_IN2, OUTPUT);
pinMode(L298N_IN3, OUTPUT);
pinMode(L298N_IN4, OUTPUT);
pinMode(L298N_ENA, OUTPUT);
pinMode(L298N_ENB, OUTPUT);

Serial.begin(9600); //
pinMode(trigPin, OUTPUT); //
pinMode(echoPin, INPUT); //
float distanceCm(){ //
// send sound pulse //
digitalWrite(trigPin, LOW); //
delayMicroseconds(3); //
digitalWrite(trigPin, HIGH); //
delayMicroseconds(5); //
digitalWrite(trigPin, LOW); //

// listen for echo //
float tUs = pulseIn(echoPin, HIGH); // microseconds //
float t = tUs / 1000.0 / 1000.0 / 2.0; // s //
float d = t*v;
return d*100; // cm //

void loop()
int d=distanceCm(); //
Serial.println(d, DEC);
delay(200); // ms //

digitalWrite(L298N_ENA, HIGH);
digitalWrite(L298N_ENB, HIGH);

void startCar()

digitalWrite(L298N_IN1, HIGH);
digitalWrite(L298N_IN2, LOW);
digitalWrite(L298N_IN3, HIGH);
digitalWrite(L298N_IN4, LOW);
void stopCar()
digitalWrite(L298N_IN1, LOW);
digitalWrite(L298N_IN2, LOW);
digitalWrite(L298N_IN3, LOW);
digitalWrite(L298N_IN4, LOW);

void leftTurn()
digitalWrite(L298N_IN1, LOW);
digitalWrite(L298N_IN2, HIGH);
digitalWrite(L298N_IN3, HIGH);
digitalWrite(L298N_IN4, LOW);

void rightTurn()

digitalWrite(L298N_IN1,HIGH );
digitalWrite(L298N_IN2, LOW);
digitalWrite(L298N_IN3,LOW );
digitalWrite(L298N_IN4, HIGH);

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

Malcom Blanc (author)2017-02-14

I am having trouble downloading the .h file, any help?

Ivica_Matic (author)Malcom Blanc2017-02-23

I will give you direct link

Download 7 zip :)

jeanlouisquero (author)2017-02-15

une précision

j'ai mis

A precision I put

analogWrite(ENA,500);au lieu de zero idem pour ENB

seul les moteurs ENA fonctionnent

Only ENA engines operate

About This Instructable




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 »
Add instructable to: