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

Arduino UNO Line Following Robot with L298N Motor Driver and IR Sensors

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

Circuit Documentation

Summary

This circuit is designed to control a line-following robot using an Arduino UNO microcontroller, four IR sensors, an L298N DC motor driver, two DC motors, and a 12V battery. The IR sensors are used to detect the line on the ground, and the motor driver controls the motors based on the sensor inputs to steer the robot. A rocker switch is included to control the power supply from the battery to the motor driver.

Component List

Arduino UNO

IR Sensors (x4)

L298N DC Motor Driver

DC Motors (x2)

12V Battery

Rocker Switch

Wiring Details

Arduino UNO

IR Sensors

L298N DC Motor Driver

DC Motors

12V Battery

Rocker Switch

Documented Code

/*
 * Line Following Robot
 * This code controls a robot that follows a black line using IR sensors
 * to detect the line and an L298N motor driver to control the motors.
 * Sensor 2 and 3 should be inside the black line, while sensor 1 and 4
 * should be outside the black line.
 */

// Pin definitions
const int irSensor1 = 4;
const int irSensor2 = 5;
const int irSensor3 = 2;
const int irSensor4 = 3;
const int motorIn1 = 10;
const int motorIn2 = 9;
const int motorIn3 = 8;
const int motorIn4 = 7;
const int motorEnA = 11;
const int motorEnB = 6;

void setup() {
  // Initialize IR sensor pins as inputs
  pinMode(irSensor1, INPUT);
  pinMode(irSensor2, INPUT);
  pinMode(irSensor3, INPUT);
  pinMode(irSensor4, INPUT);
  
  // Initialize motor control pins as outputs
  pinMode(motorIn1, OUTPUT);
  pinMode(motorIn2, OUTPUT);
  pinMode(motorIn3, OUTPUT);
  pinMode(motorIn4, OUTPUT);
  pinMode(motorEnA, OUTPUT);
  pinMode(motorEnB, OUTPUT);
  
  // Start with motors stopped
  stopMotors();
}

void loop() {
  // Read IR sensor values
  int sensor1 = digitalRead(irSensor1);
  int sensor2 = digitalRead(irSensor2);
  int sensor3 = digitalRead(irSensor3);
  int sensor4 = digitalRead(irSensor4);
  
  // Determine robot movement based on sensor values
  if (sensor2 == LOW && sensor3 == LOW) {
    // Move forward
    moveForward();
  } else if (sensor1 == HIGH) {
    // Turn right
    turnRight();
  } else if (sensor4 == HIGH) {
    // Turn left
    turnLeft();
  } else {
    // Stop if no clear path
    stopMotors();
  }
}

void moveForward() {
  digitalWrite(motorIn1, HIGH);
  digitalWrite(motorIn2, LOW);
  digitalWrite(motorIn3, HIGH);
  digitalWrite(motorIn4, LOW);
  analogWrite(motorEnA, 255);
  analogWrite(motorEnB, 255);
}

void turnRight() {
  digitalWrite(motorIn1, LOW);
  digitalWrite(motorIn2, HIGH);
  digitalWrite(motorIn3, HIGH);
  digitalWrite(motorIn4, LOW);
  analogWrite(motorEnA, 255);
  analogWrite(motorEnB, 255);
}

void turnLeft() {
  digitalWrite(motorIn1, HIGH);
  digitalWrite(motorIn2, LOW);
  digitalWrite(motorIn3, LOW);
  digitalWrite(motorIn4, HIGH);
  analogWrite(motorEnA, 255);
  analogWrite(motorEnB, 255);
}

void stopMotors() {
  digitalWrite(motorIn1, LOW);
  digitalWrite(motorIn2, LOW);
  digitalWrite(motorIn3, LOW);
  digitalWrite(motorIn4, LOW);
  analogWrite(motorEnA, 0);
  analogWrite(motorEnB, 0);
}

This code is responsible for reading the IR sensor inputs and controlling the motors accordingly to follow a line. The setup() function initializes the sensor and motor control pins, and the loop() function continuously reads the sensor values to determine the robot's movement. The robot moves forward when both middle sensors detect the line, turns right or left when the outer sensors detect the line, and stops when no clear path is detected. The moveForward(), turnRight(), turnLeft(), and stopMotors() functions control the motor driver's inputs to achieve the desired movement.