

This circuit is designed to control a pair of DC motors using an Arduino UNO microcontroller in conjunction with an L298N DC motor driver. The system also includes two infrared (IR) sensors for line tracking, which allows the robot to follow a path marked on the ground. A 12V 7Ah battery provides power to the system, and a rocker switch (SPST) is used to turn the power on and off.
5V and GND pins are connected to the power rails to provide power to the IR sensors and the L298N motor driver's logic.D12 and D11 are connected to the output pins of the IR sensors for line tracking.D10, D9, D8, and D7 are connected to the IN4, IN3, IN2, and IN1 pins of the L298N motor driver to control the motors.D6 and D5 are connected to the ENA and ENB pins of the L298N motor driver to enable and control the speed of the motors.5V pin is connected to the 5V power rail.GND pin is connected to the ground rail.12V pin is connected to one terminal of the rocker switch to receive power from the battery.OUT1, OUT2, OUT3, and OUT4 pins are connected to the respective motor terminals to drive the motors.Vcc pins are connected to the 5V power rail.GND pins are connected to the ground rail.Out pins are connected to the Arduino UNO digital pins D12 and D11 for signal output.OUT1/OUT3 pins of the L298N motor driver.OUT2/OUT4 pins of the L298N motor driver.12v + terminal is connected to one terminal of the rocker switch.12v - terminal is connected to the ground rail.12v + terminal of the battery.12V pin of the L298N motor driver.#define IR_SENSOR_RIGHT 11
#define IR_SENSOR_LEFT 12
#define MOTOR_SPEED 180
// Right motor
int enableRightMotor = 6;
int rightMotorPin1 = 7;
int rightMotorPin2 = 8;
// Left motor
int enableLeftMotor = 5;
int leftMotorPin1 = 9;
int leftMotorPin2 = 10;
void setup() {
// Configure timer for PWM frequency adjustment
TCCR0B = TCCR0B & B11111000 | B00000010;
// Set motor control pins as outputs
pinMode(enableRightMotor, OUTPUT);
pinMode(rightMotorPin1, OUTPUT);
pinMode(rightMotorPin2, OUTPUT);
pinMode(enableLeftMotor, OUTPUT);
pinMode(leftMotorPin1, OUTPUT);
pinMode(leftMotorPin2, OUTPUT);
// Set IR sensor pins as inputs
pinMode(IR_SENSOR_RIGHT, INPUT);
pinMode(IR_SENSOR_LEFT, INPUT);
// Initialize motors to stop
rotateMotor(0, 0);
}
void loop() {
// Read IR sensor values
int rightIRSensorValue = digitalRead(IR_SENSOR_RIGHT);
int leftIRSensorValue = digitalRead(IR_SENSOR_LEFT);
// Control logic based on sensor input
if (rightIRSensorValue == LOW && leftIRSensorValue == LOW) {
// Move forward
rotateMotor(MOTOR_SPEED, MOTOR_SPEED);
} else if (rightIRSensorValue == HIGH && leftIRSensorValue == LOW) {
// Turn right
rotateMotor(-MOTOR_SPEED, MOTOR_SPEED);
} else if (rightIRSensorValue == LOW && leftIRSensorValue == HIGH) {
// Turn left
rotateMotor(MOTOR_SPEED, -MOTOR_SPEED);
} else {
// Stop
rotateMotor(0, 0);
}
}
void rotateMotor(int rightMotorSpeed, int leftMotorSpeed) {
// Set motor direction and speed
if (rightMotorSpeed < 0) {
digitalWrite(rightMotorPin1, LOW);
digitalWrite(rightMotorPin2, HIGH);
} else if (rightMotorSpeed > 0) {
digitalWrite(rightMotorPin1, HIGH);
digitalWrite(rightMotorPin2, LOW);
} else {
digitalWrite(rightMotorPin1, LOW);
digitalWrite(rightMotorPin2, LOW);
}
if (leftMotorSpeed < 0) {
digitalWrite(leftMotorPin1, LOW);
digitalWrite(leftMotorPin2, HIGH);
} else if (leftMotorSpeed > 0) {
digitalWrite(leftMotorPin1, HIGH);
digitalWrite(leftMotorPin2, LOW);
} else {
digitalWrite(leftMotorPin1, LOW);
digitalWrite(leftMotorPin2, LOW);
}
// Apply PWM to enable pins to control motor speed
analogWrite(enableRightMotor, abs(rightMotorSpeed));
analogWrite(enableLeftMotor, abs(leftMotorSpeed));
}
This code is responsible for reading the IR sensor inputs and controlling the motor speeds and directions accordingly. The rotateMotor function takes care of the motor control logic, setting the direction based on the sign of the speed value and using PWM to control the speed.