Thanks to visit codestin.com
Credit goes to github.com

Skip to content

X-croot/L298N-XCR

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🚀 L298N-XCR

Advanced Arduino library for the L298N motor driver with servo-like interface, dual motor sumo mode, and manual control.

motor

✨ Features

  • Servo-style control (attach and setSpeed)
  • Dual motor sumo mode
  • Manual pin & PWM control
  • Debug output (optional)
  • Clean, object-oriented Arduino design

📦 What is L298N?

L298N is a popular dual H-bridge motor driver, ideal for controlling two DC motors with speed and direction in robotics projects.

L298N Pinout Diagram

Function Purpose
attach(pwmPin, in1Pin, in2Pin, name) Initialize and attach the motor to the given pins. The name parameter is a label shown in debug messages.
setSpeed(speed) Set motor speed and direction. Range: -255 (full reverse) to +255 (full forward).
stop() Immediately stop the motor (PWM = 0).
enableSumoMode(leftMotor, rightMotor) Link this motor with another to control both together (dual motor sumo or differential drive).
sumoControl(leftSpeed, rightSpeed) Move both motors simultaneously by setting individual speeds. Useful for forward, backward, and turning.
manualControl(in1, in2, pwm, duration) Manually set IN1/IN2 pin states (1/0), PWM value (0–255), and duration in milliseconds. Gives low-level manual control.
debugOn() / debugOff() Enable or disable debug messages over Serial for troubleshooting.

motor

🛠️ Example

#include <L298N-XCR.h>

L298N_XCR motorLeft;
L298N_XCR motorRight;

void setup() {
  motorLeft.attach(5, 2, 3, "Left Motor");
  motorRight.attach(6, 4, 7, "Right Motor");
  motorLeft.enableSumoMode(&motorLeft, &motorRight);
}

void loop() {
  motorLeft.sumoControl(150, 150); // forward
  delay(1000);
  motorLeft.sumoControl(-150, -150); // backward
  delay(1000);
  motorLeft.sumoControl(150, -150); // turn left
  delay(700);
  motorLeft.sumoControl(-150, 150); // turn right
  delay(700);
  motorLeft.sumoControl(0, 0); // stop
  delay(500);
}