Skip to content

Software

Alonso Lara edited this page Mar 29, 2026 · 7 revisions

Software


Installation

Download the ZIP file containing the library. The link is located in the Current Version table at the top of the wiki's main page.

  1. In the Arduino IDE, navigate to Sketch > Include Library > Add .ZIP Library.
  2. You will be prompted to select the library you would like to add. Navigate to the downloaded .zip file's location and open it.
  3. Return to the Sketch > Include Library menu. You should now see the L298N library at the bottom of the drop-down menu.

It is now ready to be used in your sketch. The zip file is automatically expanded into the libraries folder in your Arduino sketches directory. Note: Examples for the library will not be visible in File > Examples until after the Arduino IDE has been restarted.

Source: Arduino Guides


Constructor

L298N driver(ena, in1, in2, in3, in4, enb [, invert][, minspeed]);

  • ena - Enables/Disables and regulates MOTOR_A speed (must be a PWM-capable pin).
  • in1 / in2 - Direction control pins for MOTOR_A.
  • in3 / in4 - Direction control pins for MOTOR_B.
  • enb - Enables/Disables and regulates MOTOR_B speed (must be a PWM-capable pin).
  • invert - [Optional] Software correction for reversed wiring (Left <-> Right). Default is false.
  • minspeed - [Optional] The minimum PWM value the motors require to start moving before stalling. Default is 0.

Note on Multiple Instances: This library is fully object-oriented and its pin variables are safely encapsulated. This means you can create multiple L298N objects in the same sketch (e.g., L298N frontAxis(...) and L298N rearAxis(...)) to control 4-wheel drive robots without any pin variable conflicts.


Simple Methods

These methods are designed for quick and readable basic movements.

  • driver.stop([brake][, delay_time]) - Stops the vehicle. If brake is true, it applies an active electromagnetic brake. If false, it lets the motors coast to a halt. Waits delay_time in milliseconds.
  • driver.forward([speed][, delay_time]) - Moves both motors forward at the specified speed and waits delay_time.
  • driver.backward([speed][, delay_time]) - Moves both motors backward at the specified speed and waits delay_time.
  • driver.left([speed][, delay_time]) - Rotates the vehicle left (Motor A backward, Motor B forward) and waits delay_time.
  • driver.right([speed][, delay_time]) - Rotates the vehicle right (Motor A forward, Motor B backward) and waits delay_time.

Complex Method

driver.drive([direction][, speed][, slave_percent][, delay_time])

Sends a specific configuration order to the motors. If no direction is given, it defaults to STOP.

The slave_percent (0-100%) parameter is an advanced feature that works exclusively with the FORWARD_L, FORWARD_R, BACKWARD_L, and BACKWARD_R directions. It defines the speed of the slower "slave" motor to correct trajectory drifting.

  • Example: If speed is set to 150 and slave_percent is 50, then the slave motor's speed will be 75 (150 * 50 / 100).

Smart Hardware Protection: You don't need to worry about blowing up your L298N chip by suddenly switching from full-speed forward to full-speed backward. The drive() method features built-in deadtime protection. If it detects a drastic change in direction, it automatically cuts the power for 100ms to safely discharge back-EMF voltage before applying the new direction.


Smooth Drive Method

driver.smoothDrive([direction], [start_speed], [end_speed], [step_delay])

Executes a non-blocking progressive acceleration or deceleration. It smoothly transitions the motors from start_speed to end_speed by incrementally adjusting the PWM signal.

  • direction - [Optional] The target direction (e.g., driver.FORWARD, driver.BACKWARD). Default is driver.FORWARD.
  • start_speed - [Optional] The initial PWM speed. Default is 0.
  • end_speed - [Optional] The target PWM speed. Default is 255.
  • step_delay - [Optional] The time in milliseconds to wait between each speed increment (determines acceleration curve). Default is 5 ms.

Quick Tip: Calling driver.smoothDrive(); with no arguments will automatically accelerate the vehicle straight forward from a full stop to maximum speed.


Example

For a comprehensive example of the source code demonstrating both simple and complex methods, see the L298N_Test.ino file included in the library.


Troubleshooting

Left is right, right is left!

  • Hardware Fix: Exchange the physical motor wires A <-> B on the screw terminals.
  • Software Fix: Set the invert parameter to true when creating the driver object in your sketch.
    • Example: L298N driver(ENA, IN1, IN2, IN3, IN4, ENB, true);

Motors (one or both) do not work!

Ensure you are using hardware PWM pins for ENA and ENB.

  • On most standard Arduino boards (Uno, Nano), PWM pins are 3, 5, 6, 9, 10, and 11.
  • On the Arduino Mega, PWM pins are 2 through 13 and 44 through 46.

Clone this wiki locally