L298N Motor Driver Module
The L298N is a high-voltage, high-current motor driver chip produced by STMicroelectronics. It comes in a 15-pin package. Its main features are: high operating voltage, with a maximum voltage of 46V; high output current, with a peak instantaneous current of up to 3A and continuous working current of 2A; rated power of 25W. It contains two H-bridge high-voltage, high-current full-bridge drivers, which can be used to drive DC motors, stepper motors, relay coils, and other inductive loads. It uses standard logic level signals for control and has two enable control pins, allowing or disabling the device's operation regardless of the input signal. It also has a logic power input pin, which enables the internal logic circuit to work under low voltage. External detection resistors can be connected to provide feedback to the control circuit based on changes. When using the L298N chip to drive motors, it can control a two-phase or four-phase stepper motor, or two DC motors.
Module Source
Specifications
Driving voltage: 5V~24V
Driving current: 2A
Logic voltage: 5V
Logic current: 36mA
Control method: PWM
Instructions for Use
When the driving voltage is between 7V and 12V, i.e., when the VCC motor driving terminal is connected to the driving power supply, the onboard 78M05 voltage regulator provides the logic power to the chip, and the indicator light will turn on. In this case, there is no need to supply an external logic power. However, if using the onboard 5V power supply for the motor drive, the +5V supply terminal in the interface should not receive any input voltage. It can, however, be used to output 5V for external use (this is the typical application).
When the driving voltage is greater than 12V but less than or equal to 24V (the chip manual states that it can support up to 35V, but in practice, it is considered safe to use up to 24V for conservative applications), for example, when driving a motor rated at 18V, you must first disconnect the onboard 5V enable signal and turn off the indicator light. Do not use the onboard 78M05 to supply the logic power for the chip. Instead, connect an external 5V voltage to the 5V output port to supply power to the L298N's internal logic circuits (this is an unconventional high-voltage driving application).
The 5V enable signal is a 5V control signal. When this signal is valid and the motor drive module has proper power supply, the module will output current to the motor. Otherwise, even if the power supply is normal, no current will flow to the motor.
The L298N enable pin (high level effective, typically connected to VCC via a jumper cap) can control PWM speed adjustment through two pins: ENA and ENB. These are connected to the PWM signal. Pins 1N1, 1N2, 1N3, and 1N4 are connected to high and low levels to control motor direction (forward, reverse, or stop).
Note: If the 5V supplied by L298N is powered by an external source (i.e., not shared with the microcontroller's power supply), then the microcontroller's GND must be connected to the module's GND. This ensures that the logic signals from the microcontroller have a reference point (0V). The input pin of the onboard 5V voltage regulator and the motor power supply terminal are connected internally.
Hardware Connection
The L298N module's main pins include: ENA, IN1, IN2, IN3, IN4, ENB, OUT1, OUT2, OUT3, OUT4, VSS (logic power), VS (motor power), and GND.
- ENA and ENB are used to control the speed of the motors (via PWM control signals).
- IN1 and IN2 control the direction of the first motor (Motor A).
- IN3 and IN4 control the direction of the second motor (Motor B).
- OUT1, OUT2, OUT3, OUT4 are connected to the two motors.
- VS is connected to the motor power supply (choose according to the motor specifications, usually between 6V and 12V).
- VSS is connected to the 5V power supply of the development board.
- GND is connected to the GND of the development board and the power supply.
The following wiring is adapted to this example:
Usage Method
#define ENA 5 // 定义ENA连接到Arduino的PWM引脚5
#define IN1 2
#define IN2 3
#define ENB 6 // 定义ENB连接到Arduino的PWM引脚6
#define IN3 4
#define IN4 7
void setup() {
pinMode(ENA, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(ENB, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
}
void loop() {
// 第一个电机正转
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
analogWrite(ENA, 200); // 设置PWM值以控制速度
// 第二个电机正转
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
analogWrite(ENB, 200); // 设置PWM值以控制速度
delay(2000); // 电机运行2秒
// 停止所有电机
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
analogWrite(ENA, 0); // 停止PWM输出
analogWrite(ENB, 0);
delay(1000); // 停车1秒
// 第一个电机反转
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
analogWrite(ENA, 200); // 设置PWM值以控制速度
// 第二个电机反转
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
analogWrite(ENB, 200); // 设置PWM值以控制速度
delay(2000); // 电机运行2秒
// 停止所有电机
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
analogWrite(ENA, 0); // 停止PWM输出
analogWrite(ENB, 0);
delay(1000); // 停车1秒
// 重复上述步骤
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63