Content on this site may not be resold or reproduced. Copying is strictly prohibited.
This post may contain affiliate links. See the Affiliate Disclosure for more information.
Use of information on this site is at your own risk. View the Terms of Service.

If you’re in the process of building a mobile robot or you need to use DC Motors, then you’re probably stuck on how to use the L298N motor driver. The L298N motor driver is an H-Bridge circuit and can be used with Arduino for DC motor control.

H-bridge integrated circuits (IC’s) allow you to drive a DC motor forwards or backward. This is especially helpful if you’re looking to add autonomy to a mobile robot. With a few steps, you can “command” your robot to move in any direction.

This tutorial assumes you already have your robot built and you’re using a 2-wheel drive (2WD) or 4-wheel drive (4WD) configuration. We’re also going to use an Arduino Uno for this tutorial; however, you can use any microcontroller you’d like as long as it has at least 2 digital output pins and 1 PWM pin for each side.

Note: This tutorial outlines the process of using any H-Bridge motor driver. If you’re using popular controllers like the L293D or 754410, then you can use this process as well. Just note that the wiring and logic might vary from the L298N.

Step 1: Find the L298N motor driver Datasheet

The first step is to look up the motor driver datasheet for your motor controller. The easiest way to do this is to search Google. I use the search query “L298N datasheet.” You can replace L298N with whatever IC you’re using.

Next, locate and open the PDF datasheet from the manufacturer.

Click here for the L298N datasheet PDF.

Scroll down to the section that says “Bidirectional DC Motor Control.”

dc motor control L298N how to code with Arduino
  • Facebook
  • Twitter
  • Pinterest

This section is called “Device Functional Modes” for the L293D.

L293D Truth Table for Digital Logic and Motor Control
  • Facebook
  • Twitter
  • Pinterest

Click here to open the L293D datasheet PDF.

Essentially, you’re looking for a Truth Table that provides the logic to drive your motors. A Truth Table is a special mathematical table that shows the output values based on a combination of inputs. Inputs and outputs use Boolean functions, which means the table is constructed based on a combination of ones and zeroes (HIGH or LOW).

Example 1: Translating the L298N Truth Table

For example, let’s look at the table in Figure 6 from the L298N datasheet. If you set the Enable pin equal to HIGH (Ven = H), pin 3 equal to HIGH (C=H), and pin 4 equal to LOW (D=L), then the motor will drive forward.

I recommend going through each row in the table and translate these functions in English.

Example 2: Translating the L293D Truth Table

Here’s another example using Table 1 from the L293D datasheet.

“The Enable pin must be set to HIGH to have any output.”

If you look at Table 1, you’ll notice that when Enable is LOW, that the output is Z, which is off. When the Enable is HIGH, you can modify the Input A to HIGH or LOW to receive a HIGH or LOW output, respectively.

Once you have these functions, you’ll be ready to make the connections between the L298N motor driver and the Arduino Uno.

Note: If your robot requires two sets of motors (left and right side), then most likely, you’ll “flip” the logic in the table to account for the mirrored rotation. This presumes you’ve wired both positive motor leads into the OUT1 and OUT4, and the negative leads into OUT2 and OUT3, respectively. Most likely your left motor will be “positive” and follow the chart. The right motor will be “negative” and have opposite logical values. (This is based on the right-hand rule.) You’ll have to run some tests to confirm this.

Step 2: Connect the L298N motor driver to Arduino

Now that you understand the logic behind your motor driver, it’s time to make the L298N motor driver connection with Arduino.

Remember, you can use any controller you’d like (Arduino, Raspberry Pi, etc) as long as it has at least two digital output pins and one PWM pin (per side).

Oftentimes when you’re using Arduino, there is a Fritzing diagram that shows the Arduino DC Motor circuit.

Here’s the motor controller connection diagram for the L298N and Arduino Uno.

L298N Motor Controller Wiring Diagram and Connections to Arduino Uno
  • Facebook
  • Twitter
  • Pinterest

The Fritzing diagram was translated from the L298N datasheet using the Pin Connections and Pin Functions Table. While the open-source community is usually correct, it’s always worth verifying the Fritzing diagram with the datasheet, just in case the wiring was incorrectly mapped.

Now let’s wire the components. First, connect your DC motor leads to the OUT pins on the L298N. Next, grab some jumper wires and connect the L298N logic pins to the Arduino.

Finally, you’ll need to power the L298N motor driver. I recommend using a separate power supply because the Arduino does not provide enough current to power both the Arduino pins and motors.

I’ve been a huge fan of 18650 rechargeable batteries. Use a battery case and barrel plug to connect the batteries to the Arduino, and splice a set of lines that feed into the L298N power pins.

Sale
ARDUINO UNO R3 [A000066]
Arduino Uno is the most used and documented board in the world
$29.95 −$9.45 $20.50

We go into this in more detail in our Build Arduino Robots course. If you’re confused about how the L298N motor driver connects and works with Arduino, feel free to enroll in the course. I’ll be available to help you as you’re working on your robot.

Step 3: Program the L298N

Lastly, it’s time to program the L298N motor driver. Find the translated Truth Table you worked on in Step 1. We’ll use this to build out the code.

To start, let’s define all of the logic pins on the L298N as global variables. Set those equal to the digital pins on the Arduino. For this example, we’ll use the following configuration:

//Left Motor
int ENA = 10;
int IN1 = 9;
int IN2 = 8;

//Right Motor
int ENB = 5;
int IN3 = 7;
int IN4 = 6;

In the setup method, set all of the L298N logic pins as OUTPUTS. Then, set the enable pins to HIGH. Remember, the enable pins have to be HIGH to drive the motors.

The left motor is connected to OUT1 and OUT2 and is controlled using ENA, IN1, and IN2. The right motor is connected to OUT3 and OUT4 and is controlled using ENB, IN3, and IN4.

Now let’s create a few methods to control the motors. These methods include: forward(), backward(), left(), right(), and stop().

Example 3: Write a Forward() method using L298N motor driver

Take your translated Truth Table and write the code to drive an Arduino DC motor forward.

/*
 * Command DC Motor forward using L298N motor driver
 */
void forward(){
//left motor
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
//right motor
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);

//...speed control goes here...
}

You can control the speed of an Arduino DC motor using PWM. Since the enable pin is connected to a PWM-enabled pin on the Arduino, you can send an 8-bit signal to the L298N motor driver to control speed.

//speed control
  analogWrite(enableA, 127); //0-255
  analogWrite(enableB, 127); //0-255

Use the analogWrite(...) method on the enable pin with an input value from 0-255 (2^8=256) to set the speed. In this case, 127 is half-speed.

Once you have the code written for the left side, you’ll need to write the code to control the right side. The process is the same; however, the logic will be flipped.

Example 4: Call your motor control methods in loop()

Repeat this step by writing the remaining methods to reverse the motors, turn left and right, and stop. To test these methods, you can call them in loop() and upload them to your board.

void loop(){
	forward();
}

Tip: If you want to run the motors and then stop, you’ll need to initialize another loop. Here’s an example:

void loop(){
	forward(); //move forward
	delay(1000); //wait 1 second
	while(1){
	    stopMotors(); //stop forever
 }
}

There are many ways you can write this code, so feel free to play around with it, and find solutions that work for your application.

Optional Step 4: Create a DC Motor Library for Arduino

You can choose to stop at Step 3 and use the methods as-is. That is perfectly fine, and in most applications, you’re ready to go!

However, if you plan on using the L298N or motor driver a lot, then you may want to develop your own DC Motor Library for Arduino.

Create a DC Motor control library for Arduino using motor drivers
  • Facebook
  • Twitter
  • Pinterest

The nice thing about having a library is that once it’s written, it’s written. You can just include that library and you have autonomous motor functions available. That means you don’t have to worry about rewriting or translating Truth Tables if you use the same motor driver for different mobile robots.

The first step to converting your motor controller methods into a library is downloading the test library folder from the Arduino website.

Next, create a header (Test.h) file using the template. Then migrate your functions into the C++ Test.cpp file. There’s a very good walkthrough on how to create a library using the blink sketch, here.

I recommend reading through this resource if you’re looking to set up a library for your L298N motor driver.

Differences between the L298N vs. L293D motor drivers

If you’re not using the L298N motor driver, then you might be using the L293D motor driver.

comparison L298N vs. L293D motor drivers for autonomous robots and dc motor control
  • Facebook
  • Twitter
  • Pinterest

Both are dual H-bridge IC’s that are popular choices when working with DC Motors and Arduino. You can use either the L298N or the L293D for bi-directional motor control. That means you can control two DC motors with a single L293D or L298N IC.

Here’s a comparison of the specifications between the L298N and L293D motor drivers:

which motor driver to use L298N vs L293D table
  • Facebook
  • Twitter
  • Pinterest

Photo Credit: Waihung.net

If the DC motors in your project require a peak output greater than 1.2A and a continuous output current greater than 0.6A, then you’ll want to use the L298N motor driver. You can also feed the L298N motor driver up to 40V, which is slightly greater than the max supply voltage of 36V on the L293D.

On the other hand, if you’re looking for a motor driver shield for the Arduino Uno, the L293D motor drive expansion board is a popular option.

Both of the L298N and L293D motor driver IC expansion boards cost around $5-$8.

Qunqi 2Packs L298N Motor Drive Controller Board...
Large capacity filter capacitance,afterflow protection diode, more stable and reliable.
$8.89

However, you can buy the IC without the expansion board (DIP pin) for less than $1 each.

If you’re not building a PCB, then I recommend spending more and choosing the expansion board option. It will make your life a million times easier!

Download our Motor Controller PDF Guide

Did you enjoy this article? Download our printable motor controller PDF guide.

Keep this handy tip sheet in your workbench for the next time you need to wire up a motor driver.

Still, Confused about Motor Controllers?

If you’re still confused about motor controllers and how to add them to your project, I highly recommend enrolling in Build Arduino Robots.

This course will teach you how to read datasheets, wire controllers, and write the logic for mobile robots. Plus, you’ll gain on-demand access to the lessons and can ask me questions directly via live chat. Click here to enroll.

Related Articles:

Sharing is caring! Find value in this content? Share it on your favorite Social Media platform.

Pin It on Pinterest