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

Arduino-Controlled Obstacle Avoiding Robot with IR Line Tracking

View and Edit Project In Cirkit DesignerImage of Arduino-Controlled Obstacle Avoiding Robot with IR Line Tracking

Circuit Documentation

Summary

The circuit described in the provided inputs consists of an Arduino UNO microcontroller interfaced with various components including two IR sensors, an HC-SR04 ultrasonic sensor, a 12V battery, two DC motors, and an L298N DC motor driver. The circuit is designed to control the DC motors for movement, with the ability to detect obstacles using the ultrasonic sensor and follow a line using the IR sensors. The Arduino UNO controls the motors through the motor driver and processes inputs from the sensors to navigate the environment.

Component List

Arduino UNO

Battery 12V

DC Motor (x2)

L298N DC Motor Driver

IR Sensor (x2)

HC-SR04 Ultrasonic Sensor

Wiring Details

Arduino UNO

Battery 12V

DC Motors

L298N DC Motor Driver

IR Sensors

HC-SR04 Ultrasonic Sensor

Documented Code

// Define motor driver pins
const int motorPin1 = 9; // IN1
const int motorPin2 = 8; // IN2
const int motorPin3 = 7; // IN3
const int motorPin4 = 6; // IN4
const int ENA = 10;
const int ENB = 11;

// Define ultrasonic sensor
const int trigPin = 12;
const int echoPin = 13;
float duration, distance;

// Define line follower
const int lineFollower1 = 0;
const int lineFollower2 = 1;

// Velocity variables
int targetVelocity = 200;
int collisionVelocity = 100; // Used to reduce speed when finding an obstacle

void setup() {
  // Initialize pins for motor
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(motorPin3, OUTPUT);
  pinMode(motorPin4, OUTPUT);
  pinMode(ENA, OUTPUT);
  pinMode(ENB, OUTPUT);
  // Initialize pins for ultrasonic sensor
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  // Initialize Serial Monitor
  Serial.begin(9600);
  stopMotor();
}

void loop() {
  Serial.println("Beginning Movement");

  // Read line following sensor
  int lineFollowerValue1 = analogRead(lineFollower1);
  int lineFollowerValue2 = analogRead(lineFollower2);
  Serial.print("Line follower 1:");
  Serial.print(lineFollowerValue1);
  Serial.print("Line follower 2:");
  Serial.println(lineFollowerValue2);

  // Measure distance with ultrasonic sensor
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);
  distance = (duration * 0.0343) / 2;
  Serial.print("Distance: ");
  Serial.println(distance);
  delay(100);

  // Obstacle avoidance
  if (distance > 0 && distance < 20) {
    stopMotor();
    delay(1000);
    moveBackward(collisionVelocity);
    delay(500);
    turnLeft();
    delay(300);
  } else {
    // Line Following
    if (lineFollowerValue1 > 1000 && lineFollowerValue2 > 1000) {
      turnRight();
    }
  }
}

// Function to move rover forward
void moveForward(int speed) { // Speed between 0 - 255
  analogWrite(ENA, speed);
  analogWrite(ENB, speed);
  digitalWrite(motorPin1, HIGH);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, HIGH);
  digitalWrite(motorPin4, LOW);
  Serial.println("Motor moving forward");
}

// Function to move rover backward
void moveBackward(int speed) { // Speed between 0 - 255
  analogWrite(ENA, speed);
  analogWrite(ENB, speed);
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, HIGH);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, HIGH);
  Serial.println("Motor moving backward");
}

// Function to stop rover
void stopMotor() {
  digitalWrite(ENA, LOW);
  digitalWrite(ENB, LOW);
  Serial.println("Motor stopped");
}

// Function to turn rover left
void turnLeft() {
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, HIGH);
  digitalWrite(motorPin3, HIGH);
  digitalWrite(motorPin4, LOW);
}

// Function to turn rover right
void turnRight() {
  digitalWrite(motorPin1, HIGH);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, HIGH);
}

This code is responsible for controlling the motors through the L298N motor driver, reading sensor data from the IR sensors and ultrasonic sensor, and implementing basic obstacle avoidance and line following behaviors. The code includes functions for moving the rover forward, backward, stopping, and turning left or right. It also includes a setup routine for initializing the pins and a main loop that handles sensor readings and motor control logic.