The pin connections of the L298N module are as follows:
Motor A:
OUT1 and OUT2 pins are connected to the terminals of the first DC motor.
IN1 and IN2 pins are connected to Arduino's digital pins (e.g., pin 2 and 3).
ENA pin receives a PWM signal for motor speed control (e.g., pin 9).
Motor B:
OUT3 and OUT4 pins are connected to the terminals of the second DC motor.
IN3 and IN4 pins are connected to Arduino's digital pins (e.g., pin 4 and 5).
ENB pin receives a PWM signal for motor speed control (e.g., pin 10).
Power Connections:
+12V pin is connected to the positive terminal of a 9V battery.
GND pin is connected to the battery's negative terminal and Arduino's GND pin.
5V pin can be used to power the Arduino.
Note: Jumpers on ENA and ENB pins can be removed to apply PWM signals directly to them.
The following code makes both motors rotate forward and backward. Additionally, motor speeds are controlled using PWM signals.
int motor1pin1 = 2;
int motor1pin2 = 3;
int motor2pin1 = 4;
int motor2pin2 = 5;
void setup() {
pinMode(motor1pin1, OUTPUT);
pinMode(motor1pin2, OUTPUT);
pinMode(motor2pin1, OUTPUT);
pinMode(motor2pin2, OUTPUT);
pinMode(9, OUTPUT); // ENA
pinMode(10, OUTPUT); // ENB
}
void loop() {
// Hız kontrolü (0 = kapalı, 255 = maksimum hız)
analogWrite(9, 100); // Motor A hızı
analogWrite(10, 200); // Motor B hızı
// Motorları ileri yönde döndür
digitalWrite(motor1pin1, HIGH);
digitalWrite(motor1pin2, LOW);
digitalWrite(motor2pin1, HIGH);
digitalWrite(motor2pin2, LOW);
delay(3000);
// Motorları geri yönde döndür
digitalWrite(motor1pin1, LOW);
digitalWrite(motor1pin2, HIGH);
digitalWrite(motor2pin1, LOW);
digitalWrite(motor2pin2, HIGH);
delay(3000);
}
This code rotates the motors forward for 3 seconds and backward for 3 seconds. analogWrite
function is used to control motor speeds.
L298N module can provide up to 2A current per motor.
The module can control motors with voltages ranging from 5V to 46V.
Motor speeds can be finely adjusted using PWM signals.
The 5V regulator on the module can be used to power the Arduino.