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

Arduino UNO-Based Obstacle Avoidance and Boundary Detection Robot with IR Sensors and L298N Motor Driver

View and Edit Project In Cirkit DesignerImage of Arduino UNO-Based Obstacle Avoidance and Boundary Detection Robot with IR Sensors and L298N Motor Driver

Circuit Documentation

Summary

This document provides a detailed overview of a circuit designed to control a robotic vehicle using an Arduino UNO microcontroller. The circuit includes various sensors for obstacle and boundary detection, motor drivers for controlling the vehicle's movement, and a relay for additional control. The Arduino UNO is programmed to read sensor inputs 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. 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
  3. IR Sensor

    • Description: Infrared sensor for obstacle detection.
    • Pins: out, gnd, vcc
  4. Sharp IR Sensor

    • Description: Infrared distance sensor.
    • Pins: VCC, GND, Pin
  5. LM2596

    • Description: DC-DC buck converter.
    • Pins: IN, OUT
  6. DC Mini Metal Gear Motor

    • Description: Small DC motor with metal gears.
    • Pins: IN1, IN2
  7. Motor

    • Description: Generic motor.
    • Pins: negative, positive
  8. Battery 12V

    • Description: 12V battery for power supply.
    • Pins: +, -
  9. 12V Relay

    • Description: Relay module for switching.
    • Pins: NO, COM, NC, IN, DC-, DC+

Wiring Details

Arduino UNO

L298N DC Motor Driver

IR Sensor

Sharp IR Sensor

LM2596

DC Mini Metal Gear Motor

Motor

Battery 12V

12V Relay

Documented Code

#include <Servo.h>

// Motor and ESC Pins
#define IN1 8   // Left motor forward
#define IN2 9   // Left motor backward
#define ENA 3   // Left motor speed control
#define IN3 10  // Right motor forward
#define IN4 11  // Right motor backward
#define ENB 5   // Right motor speed control
#define ESC_PIN 6

// Sensors
#define IR1_PIN 2    // IR sensor for boundary detection
#define IR2_PIN 4
#define IR3_PIN 7
#define IR4_PIN 12
#define DIST1_PIN A0 // Distance sensor 1
#define DIST2_PIN A1 // Distance sensor 2

Servo esc; // Servo object for ESC control

void setup() {
  // Motor Pins
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(ENA, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);
  pinMode(ENB, OUTPUT);

  // Sensors
  pinMode(IR1_PIN, INPUT);
  pinMode(IR2_PIN, INPUT);
  pinMode(IR3_PIN, INPUT);
  pinMode(IR4_PIN, INPUT);

  // ESC setup
  esc.attach(ESC_PIN);
  esc.write(0); // Initialize ESC

  Serial.begin(9600);
}

void loop() {
  // Read sensors
  int dist1 = analogRead(DIST1_PIN); // Distance sensor 1
  int dist2 = analogRead(DIST2_PIN); // Distance sensor 2
  int ir1 = digitalRead(IR1_PIN);    // IR edge detection
  int ir2 = digitalRead(IR2_PIN);
  int ir3 = digitalRead(IR3_PIN);
  int ir4 = digitalRead(IR4_PIN);

  // Obstacle detection
  if (dist1 < 300 || dist2 < 300) { // Example threshold for obstacle
    stopMotors();
    Serial.println("Obstacle Detected!");
    if (dist1 < dist2) {
      turnRight(); // Turn away from closer obstacle
    } else {
      turnLeft();
    }
  }

  // Boundary detection
  if (ir1 == LOW || ir2 == LOW || ir3 == LOW || ir4 == LOW) {
    stopMotors();
    Serial.println("Boundary Detected!");
    reverse(); // Move backward slightly to avoid falling
    delay(500);
    turnRight(); // Turn to change direction
    delay(500);
  }

  // Default behavior
  moveForward();
}

// Movement functions
void moveForward() {
  // Both motors move forward
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  analogWrite(ENA, 150); // Left motor speed
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
  analogWrite(ENB, 150); // Right motor speed
}

void stopMotors() {
  // Stop both motors
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
}

void reverse() {
  // Both motors move backward
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  analogWrite(ENA, 150); // Left motor speed
  digitalWrite