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

Arduino UNO-Based Line Following and Obstacle Avoidance Robot with Ultrasonic and IR Sensors

View and Edit Project In Cirkit DesignerImage of Arduino UNO-Based Line Following and Obstacle Avoidance Robot with Ultrasonic and IR Sensors

Circuit Documentation

Summary

This document provides a detailed overview of a circuit designed to control a robot with obstacle avoidance and line-following capabilities. The circuit includes an Arduino UNO microcontroller, an HC-SR04 ultrasonic sensor, IR sensors, a 12V battery, an L298N motor driver, DC motors, and an MB102 breadboard power supply module. The Arduino UNO is programmed to read sensor data and control the motors accordingly.

Component List

  1. Arduino UNO

    • Description: Microcontroller board based on the ATmega328P.
    • Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0, A1, A2, A3, A4, A5, SCL, SDA, AREF, D13, D12, D11, D10, D9, D8, D7, D6, D5, D4, D3, D2, D1, D0
  2. HC-SR04 Ultrasonic Sensor

    • Description: Ultrasonic distance sensor.
    • Pins: VCC, TRIG, ECHO, GND
  3. 12V 7Ah Battery

    • Description: Power source for the circuit.
    • Pins: 12v +, 12v -
  4. L298N DC Motor Driver

    • Description: Dual H-Bridge motor driver.
    • Pins: OUT1, OUT2, 12V, GND, 5V, OUT3, OUT4, 5V-ENA-JMP-I, 5V-ENA-JMP-O, +5V-J1, +5V-J2, ENA, IN1, IN2, IN3, IN4, ENB
  5. DC Motor

    • Description: Motor for driving the robot.
    • Pins: pin 1, pin 2
  6. IR Sensor

    • Description: Infrared sensor for line following.
    • Pins: out, gnd, vcc
  7. MB102 Breadboard Power Supply Module 3.3V/5V

    • Description: Power supply module for breadboard.
    • Pins: VCC, GND, 3.3V, 5V

Wiring Details

Arduino UNO

HC-SR04 Ultrasonic Sensor

12V 7Ah Battery

L298N DC Motor Driver

DC Motor (Left)

DC Motor (Right)

IR Sensor (Left)

IR Sensor (Right)

MB102 Breadboard Power Supply Module

Documented Code

// Define ultrasonic sensor pins
const int trigPin = 9;
const int echoPin = 10;

// Define line-following sensor pins
const int lineFollower1 = A0; // Left sensor
const int lineFollower2 = A1; // Right sensor

// Define motor control pins
const int ENA = 3;  // PWM for left motor speed
const int ENB = 5;  // PWM for right motor speed
const int IN1 = A2; // Left motor direction pin 1
const int IN2 = A3; // Left motor direction pin 2
const int IN3 = A4; // Right motor direction pin 1
const int IN4 = A5; // Right motor direction pin 2

// Define constants
const int safeDistance = 20; // Safe distance in cm
const int lineThreshold = 800; // Threshold for line detection
const int motorSpeed = 150;

// Function to initialize pins and components
void setup() {
    Serial.begin(9600); // Initialize Serial Monitor

    // Motor control pins
    pinMode(ENA, OUTPUT);
    pinMode(ENB, OUTPUT);
    pinMode(IN1, OUTPUT);
    pinMode(IN2, OUTPUT);
    pinMode(IN3, OUTPUT);
    pinMode(IN4, OUTPUT);

    // Ultrasonic sensor pins
    pinMode(trigPin, OUTPUT);
    pinMode(echoPin, INPUT);
}

// Function to calculate distance from ultrasonic sensor
long getDistance() {
    digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);

    long duration = pulseIn(echoPin, HIGH);
    long distance = duration * 0.034 / 2;

    // Debugging: Print distance
    if (distance > 0) {
        Serial.print("Ultrasonic Distance: ");
        Serial.print(distance);
        Serial.println(" cm");
    } else {
        Serial.println("No echo received.");
    }
    return distance;
}

// Function to stop motors
void stopMotors() {
    analogWrite(ENA, 0);
    analogWrite(ENB, 0);
    Serial.println("Motors stopped.");
}

// Function to move forward
void moveForward() {
    digitalWrite(IN1, HIGH);
    digitalWrite(IN2, LOW);
    digitalWrite(IN3, HIGH);
    digitalWrite(IN4, LOW);
    analogWrite(ENA, motorSpeed);
    analogWrite(ENB, motorSpeed);
    Serial.println("Motors moving forward.");
}

// Function to move backward
void moveBackward() {
    digitalWrite(IN1, LOW);
    digitalWrite(IN2, HIGH);
    digitalWrite(IN3, LOW);
    digitalWrite(IN4, HIGH);
    analogWrite(ENA, motorSpeed);
    analogWrite(ENB, motorSpeed);
    Serial.println("Motors moving backward.");
}

// Obstacle avoidance with debugging
void avoidObstacle() {
    Serial.println("Obstacle detected! Starting avoidance maneuver...");
    stopMotors();