Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino-Controlled Line Following Robot with L298N Motor Driver and IR Sensors

View and Edit Project In Cirkit DesignerImage of Arduino-Controlled Line Following Robot with L298N Motor Driver and IR Sensors

Circuit Documentation

Summary

This circuit is designed to control a set of DC motors using an L298N DC motor driver, which is interfaced with an Arduino UNO microcontroller. The Arduino UNO reads inputs from two IR sensors to determine the motion of the motors (forward, backward, left, right, or stop). A toggle switch is used to control the power supply from a 12V battery to the motor driver. The motors are expected to respond to the sensor inputs for navigation purposes.

Component List

DC Motor

L298N DC Motor Driver

Toggle Switch

Battery 12V

Arduino UNO

IR Sensor

Wiring Details

DC Motors

L298N DC Motor Driver

Toggle Switch

Battery 12V

Arduino UNO

IR Sensors

Documented Code

#define in1 9
#define in2 8
#define in3 7
#define in4 6
#define enA 10
#define enB 5
int M1_Speed = 80; // speed of motor 1
int M2_Speed = 80; // speed of motor 2
int LeftRotationSpeed = 250; // Left Rotation Speed
int RightRotationSpeed = 250; // Right Rotation Speed

void setup() {
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(A0, INPUT); // initialize Left sensor as an input
  pinMode(A1, INPUT); // initialize Right sensor as an input
}

void loop() {
  int LEFT_SENSOR = digitalRead(A0);
  int RIGHT_SENSOR = digitalRead(A1);
  if (RIGHT_SENSOR == 0 && LEFT_SENSOR == 0) {
    forward(); //FORWARD
  } else if (RIGHT_SENSOR == 0 && LEFT_SENSOR == 1) {
    right(); //Move Right
  } else if (RIGHT_SENSOR == 1 && LEFT_SENSOR == 0) {
    left(); //Move Left
  } else if (RIGHT_SENSOR == 1 && LEFT_SENSOR == 1) {
    Stop(); //STOP
  }
}

void forward() {
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
  analogWrite(enA, M1_Speed);
  analogWrite(enB, M2_Speed);
}

void backward() {
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
  analogWrite(enA, M1_Speed);
  analogWrite(enB, M2_Speed);
}

void right() {
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
  analogWrite(enA, LeftRotationSpeed);
  analogWrite(enB, RightRotationSpeed);
}

void left() {
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
  analogWrite(enA, LeftRotationSpeed);
  analogWrite(enB, RightRotationSpeed);
}

void Stop() {
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
}

This code is responsible for controlling the DC motors through the L298N motor driver based on the input from the IR sensors. The forward, backward, right, left, and Stop functions are used to control the direction of the motors. The setup function initializes the pins, and the loop function reads the sensor inputs to determine the direction of motion.