Using an L298N Module to Control Two DC Motors with a Library
Using an L298N Module to Control Two DC Motors with a Library
In this tutorial, we will learn how to control two DC motors using the L298N motor driver module. This module allows for precise control over the speed and direction of the motors, making it ideal for various robotic applications. We will walk through the necessary hardware, wiring, and code to get everything up and running smoothly.
By the end of this tutorial, you will be able to implement motor control using the provided library, which simplifies the process significantly. To see the entire process in action, make sure to check the accompanying video (in video at 00:00).
Hardware Explained
The L298N module is a dual full-bridge motor controller that allows you to control the speed and direction of two DC motors. Each motor is controlled using two inputs for direction and one enable pin for speed control through PWM (Pulse Width Modulation). The module can handle voltages up to 35V and currents up to 2.5A per motor, making it suitable for a variety of applications.
Each motor's direction is controlled by the input pins IN1 and IN2 for one motor, and IN3 and IN4 for the other. The enable pins ENA and ENB allow for speed control via PWM signals. The L298N also has built-in diodes to protect against back EMF generated by the motors, ensuring safe operation.
Datasheet Details
| Manufacturer | STMicroelectronics |
|---|---|
| Part number | L298N |
| Logic/IO voltage | 5 V |
| Supply voltage | 7–35 V (VS) |
| Output current (per channel) | 2 A max/channel (continuous) |
| Peak current (per channel) | 3 A max/channel |
| PWM frequency guidance | 5–20 kHz |
| Input logic thresholds | 0.8 V (high) / 2.0 V (low) |
| Voltage drop / RDS(on) / saturation | 1.8 V at 2 A |
| Thermal limits | 150 °C |
| Package | Multiwatt15 |
| Notes / variants | Dual full-bridge driver |
- Ensure proper heat sinking for reliable operation.
- Use PWM on the enable pins for speed control.
- Do not exceed the voltage or current specifications.
- Double-check wiring to avoid short circuits.
- Common pitfalls include floating inputs; ensure all pins are connected properly.
Wiring Instructions

To wire the L298N to your Arduino and connect the DC motors, follow these steps:
Start by connecting the power supply to the L298N module. Connect the positive terminal of your battery to the +12V pin and the negative terminal to the GND pin. Next, connect your first motor to the OUT1 and OUT2 terminals, and the second motor to OUT3 and OUT4.
For the control pins, connect ENA (pin 3) to a PWM-enabled pin on your Arduino. Connect IN1 (pin 2) and IN2 (pin 4) to digital pins on the Arduino for the first motor. For the second motor, connect IN3 (pin 7) and IN4 (pin 8), and connect ENB (pin 9) to another PWM-enabled pin. Finally, connect the 5V output from the L298N to the Arduino's 5V input to power the Arduino when not connected to a computer.
Code Examples & Walkthrough
In the code, we first include the L298N library and define the pins used for the motors. The identifiers IN1, IN2, ENA are defined for motor one, while IN3, IN4, ENB are for motor two. The constants CCW and CW are defined for counterclockwise and clockwise rotation, respectively.
#include
#define IN1 2
#define IN2 4
#define ENA 3 // this pin must be PWM enabled pin
#define IN3 7
#define IN4 8
#define ENB 9 // this pin must be PWM enabled pin
This excerpt shows the initial setup of the library and pin definitions. The use of PWM for speed control is crucial, as indicated by the comments in the code.
void setup() {
Serial.begin(115200);
motor.begin();
}
In the setup() function, we initialize the serial communication and the motor control library. This prepares the system to control the motors effectively.
void loop() {
motor.rotate(motor1, 60, CW);//run motor1 at 60% speed in CW direction
motor.rotate(motor2, 60, CCW);//run motor2 at 60% speed in CCW direction
delay(3000);
}
The main loop demonstrates how to control the motors by calling the rotate() function. Here, both motors are set to run at 60% speed for three seconds. This is a straightforward example of how the library simplifies motor control.
Demonstration / What to Expect
Upon completing the wiring and uploading the code, you should see both motors rotate according to the specified speeds and directions. Common pitfalls include ensuring that the power supply matches the motor specifications and that all connections are secure. If the motors do not operate as expected, check for floating inputs or incorrect wiring (in video at 00:00).
Video Timestamps
- 00:00 Start
- 01:28 Introduction
- 02:06 Hardware Explained
- 09:10 Wiring explained
- 11:43 Code Explained
- 18:42 L298N 2 motor demonstration
- 20:40 Larger motor demonstration
- 22:10 Connecting motor into parallel mode
- 25:12 Parallel mode demonstration
This code has not been parsed yet. Please return to the admin panel to parse it.
مواردی که ممکن است به آنها نیاز داشته باشید
-
آمازون
-
آمازون
-
علیاکسپرسPurchase L298N Module from AliExpresss.click.aliexpress.com
-
علیاکسپرسPurchase L298N Module from AliExpress (2)s.click.aliexpress.com
-
علیاکسپرسPurchase L298N Module from eBayebay.us
منابع و مراجع
-
خارجی
-
داخلی
فایلها📁
فایل فریزینگ
-
درایور موتور DC L298N
L298N DC motor driver.fzpz0.11 MB
سایر فایلها
-
کتابخانه موتور DC Robojax L298N
robojax_L298N-DC-Motor_library.zip0.10 MB