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 using an Arduino UNO microcontroller. The robot is equipped with an HC-SR04 ultrasonic sensor for obstacle detection, two IR sensors for line following, and two hobby gearmotors controlled by an L298N DC motor driver. The circuit is powered by a 4 x AAA battery mount and includes a rocker switch for power control.

Component List

  1. 4 x AAA Battery Mount

    • Description: Provides power to the circuit.
    • Pins: -, +
  2. HC-SR04 Ultrasonic Sensor

    • Description: Measures distance to obstacles.
    • Pins: VCC, TRIG, ECHO, GND
  3. Arduino UNO

    • Description: Microcontroller that controls the robot.
    • 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
  4. IR Sensor (Left)

    • Description: Detects the line for line following.
    • Pins: out, gnd, vcc
  5. IR Sensor (Right)

    • Description: Detects the line for line following.
    • Pins: out, gnd, vcc
  6. L298N DC Motor Driver

    • Description: Controls the speed and direction of the motors.
    • 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
  7. Rocker Switch

    • Description: Controls the power to the circuit.
    • Pins: output, input
  8. Hobby Gearmotor with 48:1 Gearbox (Left)

    • Description: Drives the left wheel of the robot.
    • Pins: pin 1, pin 2
  9. Hobby Gearmotor with 48:1 Gearbox (Right)

    • Description: Drives the right wheel of the robot.
    • Pins: pin 1, pin 2

Wiring Details

4 x AAA Battery Mount

HC-SR04 Ultrasonic Sensor

Arduino UNO

IR Sensor (Left)

IR Sensor (Right)

L298N DC Motor Driver

Rocker Switch

Hobby Gearmotor with 48:1 Gearbox (Left)

Hobby Gearmotor with 48:1 Gearbox (Right)

Documented Code

Arduino UNO Code

// Define pins for motor speed control (EnA and EnB)
int VelocidadMotor1 = 6;
int VelocidadMotor2 = 5;

// Define control pins for motor direction (In1, In2, In3, In4)
int Motor1A = 13;
int Motor1B = 12;
int Motor2C = 11;
int Motor2D = 10;

// Infrared sensors - left and right
int infraPin = 2; // Right IR sensor
int infraPin1 = 4; // Left IR sensor

// Variables to capture sensor values: 0 - light background, 1 - black line
int valorInfra = 0;
int valorInfra1 = 0;

// Ultrasonic sensor pins
#define PIN_TRIG 9
#define PIN_ECHO 8

long duration, distance;

// Initial setup
void setup() {
    Serial.begin(9600);
    delay(1000);
    // Configure ultrasonic sensor
    pinMode(PIN_TRIG, OUTPUT);
    pinMode(PIN_ECHO, INPUT);
    // Set up IR sensor pins
    pinMode(infraPin, INPUT);
    pinMode(infraPin1, INPUT);

    // Set up motor control pins
    pinMode(Motor1A, OUTPUT);
    pinMode(Motor1B, OUTPUT);
    pinMode(Motor2C, OUTPUT);
    pinMode(Motor2D, OUTPUT);
    pinMode(VelocidadMotor1, OUTPUT);
    pinMode(VelocidadMotor2, OUTPUT);

    // Set initial motor speeds
    analogWrite(VelocidadMotor1, 150);
    analogWrite(VelocidadMotor2, 180);

    // Initialize motor direction
    digitalWrite(Motor1A, LOW);
    digitalWrite(Motor1B, LOW);
    digitalWrite(Motor2C, LOW);
    digitalWrite(Motor2D, LOW);
}

// Main loop
void loop() {
    // Ultrasonic sensor measurement
    digitalWrite(PIN_TRIG, LOW);
    delayMicroseconds(5);
    digitalWrite(PIN_TRIG, HIGH);
    delayMicroseconds(10);
    digitalWrite(PIN_TRIG, LOW);
    duration = pulseIn(PIN_ECHO, HIGH);
    distance = (duration / 2) / 29.1; // Convert time to distance in cm
    Serial.print("Distance to object: ");