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

Arduino Mega 2560 Line-Following Robot with Obstacle Detection

View and Edit Project In Cirkit DesignerImage of Arduino Mega 2560 Line-Following Robot with Obstacle Detection

Circuit Documentation

Summary

This circuit is designed to control a line-following robot with the capability to detect intersections and obstacles. It uses an Arduino Mega 2560 as the main controller, two IR sensors for line tracking, an ultrasonic sensor for obstacle detection, and a motor driver to control two DC motors. The robot is powered by a 12V battery and is programmed to perform specific maneuvers at intersections and in response to obstacles.

Component List

Arduino Mega 2560

DC Motors (x2)

L298N DC Motor Driver

12V Battery (mini)

IR Sensors (x2)

Ultrasonic Sensor

Wiring Details

Arduino Mega 2560

DC Motors

L298N DC Motor Driver

Documented Code

/*
 * Line Following Robot with Intersection Logic
 * This code controls a line-following robot that detects intersections
 * and stops the robot when it detects an obstacle in front, then moves
 * again if the obstacle is not in front. It performs specific actions
 * based on IR sensor readings.
 *
 * - The robot follows a line using two IR sensors.
 * - When both IR sensors detect black, it counts as an intersection.
 * - At the first intersection, the robot turns 90 degrees right.
 * - At the second intersection, the robot turns 180 degrees and returns
 *   to the original position.
 */

// Pin definitions
const int motorENA = 2;
const int motorENB = 7;
const int motorIN1 = 3;
const int motorIN2 = 4;
const int motorIN3 = 5;
const int motorIN4 = 6;
const int irSensor1 = A0;
const int irSensor2 = A3;
const int ultrasonicTrig = 28;
const int ultrasonicEcho = 29;

int intersectionCount = 0;

void setup() {
  // Initialize motor control pins
  pinMode(motorENA, OUTPUT);
  pinMode(motorENB, OUTPUT);
  pinMode(motorIN1, OUTPUT);
  pinMode(motorIN2, OUTPUT);
  pinMode(motorIN3, OUTPUT);
  pinMode(motorIN4, OUTPUT);
  // Initialize IR sensor pins
  pinMode(irSensor1, INPUT);
  pinMode(irSensor2, INPUT);
  // Initialize ultrasonic sensor pins
  pinMode(ultrasonicTrig, OUTPUT);
  pinMode(ultrasonicEcho, INPUT);
  Serial.begin(9600);
}

void loop() {
  // Check for obstacles
  if (detectObstacle()) {
    stopRobot();
  } else {
    followLine();
  }
  Serial.print("Intersections passed: ");
  Serial.println(intersectionCount);
}

bool detectObstacle() {
  digitalWrite(ultrasonicTrig, LOW);
  delayMicroseconds(2);
  digitalWrite(ultrasonicTrig, HIGH);
  delayMicroseconds(10);
  digitalWrite(ultrasonicTrig, LOW);
  long duration = pulseIn(ultrasonicEcho, HIGH);
  long distance = duration * 0.034 / 2;
  return distance < 20; // Obstacle within 20 cm
}

void stopRobot() {
  analogWrite(motorENA, 0);
  analogWrite(motorENB, 0);
}

void followLine() {
  int sensor1 = analogRead(irSensor1);
  int sensor2 = analogRead(irSensor2);
  if (sensor1 < 500 && sensor2 < 500) {
    intersectionCount++;
    if (intersectionCount == 1) {
      turnRight();
    } else if (intersectionCount == 2) {
      turn180();
      intersectionCount = 0;
    }
  } else if (sensor1 < 500) {
    analogWrite(motorENA, 255);
    analogWrite(motorENB, 128);
  } else if (sensor2 < 500) {
    analogWrite(motorENA, 128);
    analogWrite(motorENB, 255);
  } else {
    analogWrite(motorENA, 255);
    analogWrite(motorENB, 255);
  }
}

void turnRight() {
  digitalWrite(motorIN1, HIGH);
  digitalWrite(motorIN2, LOW);
  digitalWrite(motorIN3, LOW);
  digitalWrite(motorIN4, HIGH);
  delay(500);
}

void turn180() {
  digitalWrite(motorIN1, HIGH);
  digitalWrite(motorIN2, LOW);
  digitalWrite(motorIN3, HIGH);
  digitalWrite(motorIN4, LOW);
  delay(1000);
}

This code is responsible for the operation of the line-following robot. It initializes the necessary pins, contains functions to detect obstacles, stop the robot, follow the line, and perform turns. The robot's behavior is determined by the readings from the IR sensors and the ultrasonic sensor.