Shopping Cart

⚡️ We’re Helping 1 Million People Build Their First Robots

🇺🇸 Proudly Women-Owned Small Business
Arduino L298N motor code

How to Program the L298N with Arduino

We sometimes use affiliate links in our content. This won't cost you anything, but it helps us to offset the costs of paying our writing team. You can support us directly on BuyMeACoffee. Thank you!

If you’ve ever tried to build a mobile robot, then you’ll need to learn how to program motor controllers. In our Beginner Bots tutorials, we use the L298N motor controller with the Arduino Uno to drive four motors. You can pick up a Smart Robot Car and follow along with this article if you’d like!

For this tutorial, I’m going to show you how to translate motor controller specifications into the Arduino code.

First, we will take a look at the L298N Motor Controller Specifications. Typically the manufacturer will include a truth table outlining key functionality. I’ve moved a copy of the L298N truth table below, for your reference.

L298N Motor Controller Truth Table

Next, we will create global variables for each of the motor pins. There are three pins per motor, so we will map each pin to a global variable. In the setup() method, we’ll declare each pin as an OUTPUT using the built-in pinMode(...) method.

Arduino L298N motor code

Now, we’ll use the truth table to map out motor functionality for the robot. We want to create methods for forward, backward, left, right, and stop.

Here’s what the forward(...) method looks like.

We can use digitalWrite(...) to set the pin to a 1 or 0 (HIGH or LOW), respectively. To control the speed, we’ll use the analogWrite(...) command on the enable pin for each motor. Use a value of 0-255 because digital pins support 8-bit PWM control.

Also, the stop() method will set the Enable pin to 1 and Input 1 and 2 to 0. Then, you can repeat the translations for left, right, and backward moves using the remaining rows in the truth table.

When you’re finished, call the methods in loop() so that they execute on the robot. Be sure to test out your code on the floor and make modifications to the speed and/or direction, as necessary.

Tutorial Video

Are you more of a visual learner? Here’s a demonstration of how to use the L298N with Arduino.

If you have any questions about how to program your robot using a motor controller, be sure to comment below. You can also follow Learn Robotics on Facebook and Instagram for insider tips, robotics news, and updates!

Support Content Like This

You May Also Like

Have a question? Need Help?

Thanks for dropping by! Comments are no longer moderated daily.
Before posting your comment, please consider buying a coffee.
If you'd like to work with Liz, you can schedule a call here.

7 comments

  1. Awesome site and great video!

    What are your thoughts on using this to control motors over wifi?

    https://www.tinyosshop.com/index.php?route=product/product&product_id=667&filter_name=wifi%20motor

    1. Liz Miller

      Hi Graf, Thanks for dropping by. Should work, but it really depends on the application. Good luck! ~Liz from Learn Robotics

  2. qassim amh

    hi
    How can I use a L298N driver with an Arduino to make a CNC machine.
    Thanks for your help

    1. Liz Miller

      I would recommend using a Stepper Motor + Stepper Motor driver (not the L298N) if you’re going to create a CNC machine. Good luck!

  3. Carlos Bruni

    Dear Liz,

    Tenho trabalhado com o LM298 e não consigo variar a rpm via PWM nas porta ENA e ENB apenas nas IN1,2,3,4 . ENNA e ENB HIGH all the time.

    Carlos bruni

    1. Liz Miller

      Carlos, Thanks for the comment.

      First, make sure that the ENA pin is connected to a PWM-enabled digital pin. Any of the pins with a ~ next to them are PWM enabled. In this example ENA is on pin 10. Then, set the ENA pin to HIGH using digitalWrite(ENA, HIGH); Finally, to control speed, you’ll need to use the analogWrite(ENA, 127); command. This one sets the motor to roughly half speed.

      Give this a try, and let me know how it goes!

  4. Carlos Bruni

    Dear Liz,

    Tenho trabalhado com o LM298 e não consigo variar a rpm via PWM nas porta ENA e ENB apenas nas IN1,2,3,4 . ENNA e ENB HIGH all the time.

    Carlos bruni

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Learn Robotics Botly Favicon

Become a Learn Robotics Insider

 

Be the first to know when we launch new robotics content. Plus gain FREE robotics tips to your inbox.

Awesome! Check your inbox for a surprise!

Remote Robotics Courses for High School, Middle School, or Pre-College Training

Join the Powered by Learn Robotics Program

Teach robotics anywhere with our turnkey robotics program!

Download our free program brochure!

Success! Check your email for the download!