Cirkit Designer Logo
Cirkit Designer
Your all-in-one circuit design IDE
Home / 
Project Documentation

Arduino-Controlled Obstacle Avoiding Robot with IR Line Tracking

Image 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

  • Microcontroller board based on the ATmega328P
  • It has 14 digital input/output pins, 6 analog inputs, a 16 MHz quartz crystal, a USB connection, a power jack, an ICSP header, and a reset button.

Battery 12V

  • A 12-volt power source used to power the motor driver and, indirectly, the motors.

DC Motor (x2)

  • Electric motors that convert DC electrical energy into mechanical energy.
  • Used for propelling the vehicle by controlling the wheels.

L298N DC Motor Driver

  • A motor driver module capable of driving two DC motors.
  • It has an onboard 5V regulator which can be used to power the connected microcontroller.

IR Sensor (x2)

  • Infrared sensors used for line following or obstacle detection.
  • Each sensor has an output pin, a ground pin, and a VCC pin for power.

HC-SR04 Ultrasonic Sensor

  • An ultrasonic ranging module that provides 2cm to 400cm non-contact measurement functionality.
  • It has four pins: VCC for power, TRIG for triggering the ultrasonic pulse, ECHO for receiving the echo, and GND for ground.

Wiring Details

Arduino UNO

  • 5V connected to the 5V power bus for sensors and motor driver.
  • GND connected to the ground bus.
  • A0 connected to the output of the first IR sensor.
  • A1 connected to the output of the second IR sensor.
  • D13 connected to the ECHO pin of the HC-SR04 ultrasonic sensor.
  • D12 connected to the TRIG pin of the HC-SR04 ultrasonic sensor.
  • D11 connected to the ENB pin of the L298N motor driver.
  • D10 connected to the ENA pin of the L298N motor driver.
  • D9 connected to the IN1 pin of the L298N motor driver.
  • D8 connected to the IN2 pin of the L298N motor driver.
  • D7 connected to the IN3 pin of the L298N motor driver.
  • D6 connected to the IN4 pin of the L298N motor driver.

Battery 12V

  • + connected to the 12V input of the L298N motor driver.
  • - connected to the ground bus.

DC Motors

  • Motor 1 pin 1 connected to OUT1 of the L298N motor driver.
  • Motor 1 pin 2 connected to OUT2 of the L298N motor driver.
  • Motor 2 pin 1 connected to OUT3 of the L298N motor driver.
  • Motor 2 pin 2 connected to OUT4 of the L298N motor driver.

L298N DC Motor Driver

  • 12V connected to the + of the 12V battery.
  • GND connected to the ground bus.
  • 5V connected to the 5V power bus.
  • ENA, ENB connected to the Arduino UNO for speed control.
  • IN1, IN2, IN3, IN4 connected to the Arduino UNO for direction control.
  • OUT1, OUT2, OUT3, OUT4 connected to the DC motors.

IR Sensors

  • VCC connected to the 5V power bus.
  • GND connected to the ground bus.
  • OUT connected to the analog input pins A0 and A1 of the Arduino UNO.

HC-SR04 Ultrasonic Sensor

  • VCC connected to the 5V power bus.
  • GND connected to the ground bus.
  • TRIG connected to the D12 pin of the Arduino UNO.
  • ECHO connected to the D13 pin of the Arduino UNO.

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.