0% found this document useful (0 votes)
14 views26 pages

L298N Arduino Motor Control Guide

The document provides an overview of the L298N motor driver, detailing its capabilities to control two DC motors with speed and direction adjustments. It includes example Arduino programs for controlling DC motors using a joystick, as well as for building a light-following and line-following robot. The examples illustrate how to set up the motor driver and implement control logic using Arduino code.

Uploaded by

shivam mehta
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views26 pages

L298N Arduino Motor Control Guide

The document provides an overview of the L298N motor driver, detailing its capabilities to control two DC motors with speed and direction adjustments. It includes example Arduino programs for controlling DC motors using a joystick, as well as for building a light-following and line-following robot. The examples illustrate how to set up the motor driver and implement control logic using Arduino code.

Uploaded by

shivam mehta
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

L298N Motor Driver – Arduino Interface

The L298N is a dual H-Bridge motor driver which allows speed and direction
control of two DC motors at the same time. The module can drive DC motors that
have voltages between 5 and 35V, with a peak current up to 2A.

Ref: by Dejan Nedelkovski, [Link]


Program to control DC motor using Joystick

Ref: by Dejan Nedelkovski, [Link]


Program (Example 01)

/* Arduino DC Motor Control - PWM | H-Bridge | L298N - Example 01

by Dejan Nedelkovski, [Link]


*/

#define enA 9
#define in1 6
#define in2 7
#define button 4

int rotDirection = 0;
int pressed = false;

void setup() {
pinMode(enA, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(button, INPUT);
// Set initial rotation direction
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
}

void loop() {
int potValue = analogRead(A0); // Read potentiometer value
int pwmOutput = map(potValue, 0, 1023, 0 , 255); // Map the potentiometer
value from 0 to 255
analogWrite(enA, pwmOutput); // Send PWM signal to L298N Enable pin

// Read button - Debounce


if (digitalRead(button) == true) {
pressed = !pressed;
}
while (digitalRead(button) == true);
delay(20);

Ref: by Dejan Nedelkovski, [Link]


// If button is pressed - change rotation direction
if (pressed == true & rotDirection == 0) {
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
rotDirection = 1;
delay(20);
}
// If button is pressed - change rotation direction
if (pressed == false & rotDirection == 1) {
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
rotDirection = 0;
delay(20);
}
}

Arduino Robot Car Control using L298N Motor Driver

Program :

/* Arduino DC Motor Control - PWM | H-Bridge | L298N


Example 02 - Arduino Robot Car Control
*/

Ref: by Dejan Nedelkovski, [Link]


#define enA 9
#define in1 4
#define in2 5
#define enB 10
#define in3 6
#define in4 7

int motorSpeedA = 0;
int motorSpeedB = 0;

void setup() {
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
}

void loop() {
int xAxis = analogRead(A0); // Read Joysticks X-axis
int yAxis = analogRead(A1); // Read Joysticks Y-axis

// Y-axis used for forward and backward control


if (yAxis < 470) {
// Set Motor A backward
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
// Set Motor B backward
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
// Convert the declining Y-axis readings for going backward from 470 to 0 into 0
to 255 value for the PWM signal for increasing the motor speed
motorSpeedA = map(yAxis, 470, 0, 0, 255);
motorSpeedB = map(yAxis, 470, 0, 0, 255);
}

Ref: by Dejan Nedelkovski, [Link]


else if (yAxis > 550) {
// Set Motor A forward
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
// Set Motor B forward
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
// Convert the increasing Y-axis readings for going forward from 550 to 1023
into 0 to 255 value for the PWM signal for increasing the motor speed
motorSpeedA = map(yAxis, 550, 1023, 0, 255);
motorSpeedB = map(yAxis, 550, 1023, 0, 255);
}
// If joystick stays in middle the motors are not moving
else {
motorSpeedA = 0;
motorSpeedB = 0;
}

// X-axis used for left and right control


if (xAxis < 470) {
// Convert the declining X-axis readings from 470 to 0 into increasing 0 to 255
value
int xMapped = map(xAxis, 470, 0, 0, 255);
// Move to left - decrease left motor speed, increase right motor speed
motorSpeedA = motorSpeedA - xMapped;
motorSpeedB = motorSpeedB + xMapped;
// Confine the range from 0 to 255
if (motorSpeedA < 0) {
motorSpeedA = 0;
}
if (motorSpeedB > 255) {
motorSpeedB = 255;
}
}
if (xAxis > 550) {
// Convert the increasing X-axis readings from 550 to 1023 into 0 to 255 value
int xMapped = map(xAxis, 550, 1023, 0, 255);

Ref: by Dejan Nedelkovski, [Link]


// Move right - decrease right motor speed, increase left motor speed
motorSpeedA = motorSpeedA + xMapped;
motorSpeedB = motorSpeedB - xMapped;
// Confine the range from 0 to 255
if (motorSpeedA > 255) {
motorSpeedA = 255;
}
if (motorSpeedB < 0) {
motorSpeedB = 0;
}
}
// Prevent buzzing at low speeds (Adjust according to your motors. My motors
couldn't start moving if PWM value was below value of 70)
if (motorSpeedA < 70) {
motorSpeedA = 0;
}
if (motorSpeedB < 70) {
motorSpeedB = 0;
}
analogWrite(enA, motorSpeedA); // Send PWM signal to motor A
analogWrite(enB, motorSpeedB); // Send PWM signal to motor B
}

Ref: by Dejan Nedelkovski, [Link]


// Light Following Robot using Arduino UNO

Program
#define LDR_LEFT A0 // Left LDR connected to A0
#define LDR_RIGHT A1 // Right LDR connected to A1
#define MOTOR_LEFT_FORWARD 5 // Left motor forward
#define MOTOR_LEFT_BACKWARD 6 // Left motor backward
#define MOTOR_RIGHT_FORWARD 9 // Right motor forward
#define MOTOR_RIGHT_BACKWARD 10 // Right motor backward

void setup() {
pinMode(LDR_LEFT, INPUT);
pinMode(LDR_RIGHT, INPUT);
pinMode(MOTOR_LEFT_FORWARD, OUTPUT);
pinMode(MOTOR_LEFT_BACKWARD, OUTPUT);
pinMode(MOTOR_RIGHT_FORWARD, OUTPUT);
pinMode(MOTOR_RIGHT_BACKWARD, OUTPUT);
[Link](9600);
}
void loop() {
int lightLeft = analogRead(LDR_LEFT);
int lightRight = analogRead(LDR_RIGHT);
if (lightLeft > lightRight + 50) { // Turn left
moveLeft();
} else if (lightRight > lightLeft + 50) { // Turn right
moveRight();
} else { // Move forward
moveForward();
}
}
void moveForward() {
digitalWrite(MOTOR_LEFT_FORWARD, HIGH);
digitalWrite(MOTOR_LEFT_BACKWARD, LOW);
digitalWrite(MOTOR_RIGHT_FORWARD, HIGH);
digitalWrite(MOTOR_RIGHT_BACKWARD, LOW);
}
void moveLeft() {
digitalWrite(MOTOR_LEFT_FORWARD, LOW);
digitalWrite(MOTOR_LEFT_BACKWARD, HIGH);
digitalWrite(MOTOR_RIGHT_FORWARD, HIGH);
digitalWrite(MOTOR_RIGHT_BACKWARD, LOW);
}

void moveRight() {
digitalWrite(MOTOR_LEFT_FORWARD, HIGH);
digitalWrite(MOTOR_LEFT_BACKWARD, LOW);
digitalWrite(MOTOR_RIGHT_FORWARD, LOW);
digitalWrite(MOTOR_RIGHT_BACKWARD, HIGH);
}
// Line Following Robot using Arduino UNO

#define leftIR 2 // Left IR sensor


#define rightIR 3 // Right IR sensor
#define leftMotor1 5 // Left Motor forward
#define leftMotor2 6 // Left Motor backward
#define rightMotor1 9 // Right Motor forward
#define rightMotor2 10 // Right Motor backward
void setup() {
pinMode(leftIR, INPUT);
pinMode(rightIR, INPUT);
pinMode(leftMotor1, OUTPUT);
pinMode(leftMotor2, OUTPUT);
pinMode(rightMotor1, OUTPUT);
pinMode(rightMotor2, OUTPUT);}
void loop() {
int leftSensor = digitalRead(leftIR);
int rightSensor = digitalRead(rightIR);

if (leftSensor == LOW && rightSensor == LOW) {


// Both sensors on black → Move forward
moveForward();}
else if (leftSensor == HIGH && rightSensor == LOW) {
// Left sensor on white → Turn right
turnRight();
}
else if (leftSensor == LOW && rightSensor == HIGH) {
// Right sensor on white → Turn left
turnLeft();
}
else {
// Both sensors on white → Stop
stopMotors();
}
}
void moveForward() {
digitalWrite(leftMotor1, HIGH);
digitalWrite(leftMotor2, LOW);
digitalWrite(rightMotor1, HIGH);
digitalWrite(rightMotor2, LOW);
}
void turnRight() {
digitalWrite(leftMotor1, HIGH);
digitalWrite(leftMotor2, LOW);
digitalWrite(rightMotor1, LOW);
digitalWrite(rightMotor2, LOW);
}
void turnLeft() {
digitalWrite(leftMotor1, LOW);
digitalWrite(leftMotor2, LOW);
digitalWrite(rightMotor1, HIGH);
digitalWrite(rightMotor2, LOW);
}
void stopMotors() {
digitalWrite(leftMotor1, LOW);
digitalWrite(leftMotor2, LOW);
digitalWrite(rightMotor1, LOW);
digitalWrite(rightMotor2, LOW);
}

You might also like