-
Notifications
You must be signed in to change notification settings - Fork 5
Software
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.
- In the Arduino IDE, navigate to Sketch > Include Library > Add .ZIP Library.
- You will be prompted to select the library you would like to add. Navigate to the downloaded
.zipfile's location and open it. - Return to the Sketch > Include Library menu. You should now see the
L298Nlibrary 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
L298N driver(ena, in1, in2, in3, in4, enb [, invert][, minspeed]);
-
ena- Enables/Disables and regulates MOTOR_Aspeed(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_Bspeed(must be a PWM-capable pin). -
invert- [Optional] Software correction for reversed wiring (Left <-> Right). Default isfalse. -
minspeed- [Optional] The minimum PWM value the motors require to start moving before stalling. Default is0.
Note on Multiple Instances: This library is fully object-oriented and its pin variables are safely encapsulated. This means you can create multiple
L298Nobjects in the same sketch (e.g.,L298N frontAxis(...)andL298N rearAxis(...)) to control 4-wheel drive robots without any pin variable conflicts.
These methods are designed for quick and readable basic movements.
-
driver.stop([brake][, delay_time])- Stops the vehicle. Ifbrakeistrue, it applies an active electromagnetic brake. Iffalse, it lets the motors coast to a halt. Waitsdelay_timein milliseconds. -
driver.forward([speed][, delay_time])- Moves both motors forward at the specifiedspeedand waitsdelay_time. -
driver.backward([speed][, delay_time])- Moves both motors backward at the specifiedspeedand waitsdelay_time. -
driver.left([speed][, delay_time])- Rotates the vehicle left (Motor A backward, Motor B forward) and waitsdelay_time. -
driver.right([speed][, delay_time])- Rotates the vehicle right (Motor A forward, Motor B backward) and waitsdelay_time.
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
speedis set to 150 andslave_percentis 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.
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 isdriver.FORWARD. -
start_speed- [Optional] The initial PWM speed. Default is0. -
end_speed- [Optional] The target PWM speed. Default is255. -
step_delay- [Optional] The time in milliseconds to wait between each speed increment (determines acceleration curve). Default is5ms.
Quick Tip: Calling
driver.smoothDrive();with no arguments will automatically accelerate the vehicle straight forward from a full stop to maximum speed.
For a comprehensive example of the source code demonstrating both simple and complex methods, see the L298N_Test.ino file included in the library.
- Hardware Fix: Exchange the physical motor wires A <-> B on the screw terminals.
-
Software Fix: Set the
invertparameter totruewhen creating the driver object in your sketch.-
Example:
L298N driver(ENA, IN1, IN2, IN3, IN4, ENB, true);
-
Example:
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.