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

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

Image of Arduino UNO-Based Obstacle Avoidance Robot with IR Sensors and Ultrasonic Sensor

Circuit Documentation

Summary

This circuit is designed to control a robot with automatic and manual navigation capabilities. It includes an Arduino UNO microcontroller interfaced with IR sensors, an HC-SR04 ultrasonic sensor, DC motors, an L298N motor driver, an ESP32-CAM module, and a power supply consisting of 18650 Li-ion batteries. The robot can navigate autonomously by following a line and avoiding obstacles, or it can be controlled manually through serial commands. The ESP32-CAM module can be used for additional functionalities such as video streaming or image capture.

Component List

Arduino UNO

  • Microcontroller board based on the ATmega328P
  • Used as the main controller for the robot

IR Sensors

  • Infrared sensors used for line tracking and obstacle detection
  • Two instances of this sensor are used in the circuit

DC Motors

  • Electric motors that drive the robot's wheels
  • Four instances of this motor are used in the circuit

HC-SR04 Ultrasonic Sensor

  • Ultrasonic distance sensor
  • Used for measuring the distance to obstacles

L298N DC Motor Driver

  • Dual H-bridge motor driver
  • Used to control the speed and direction of the DC motors
  • Two instances of this driver are used in the circuit

18650 Li-ion Batteries

  • Rechargeable batteries that provide power to the circuit
  • Four instances of this battery are used in the circuit

ESP32-CAM

  • A camera module with Wi-Fi capabilities
  • Can be used for video streaming or image capture

Rocker Switches

  • Mechanical switches used to control the power supply
  • Two instances of this switch are used in the circuit

Wiring Details

Arduino UNO

  • 5V and GND are connected to the power rails of the IR sensors and the HC-SR04 ultrasonic sensor
  • Digital pins D2 and D3 are connected to the output of the IR sensors
  • Analog pins A0 and A1 are connected to the ECHO and TRIG pins of the HC-SR04 ultrasonic sensor
  • Digital pins D5 to D13 are connected to the control pins of the L298N motor drivers
  • Vin is connected to the power supply through a rocker switch
  • GND is connected to the negative terminal of the power supply
  • Digital pins D0 and D1 are connected to the VOT and VOR pins of the ESP32-CAM for serial communication

IR Sensors

  • vcc pins are connected to the 5V output of the Arduino UNO
  • gnd pins are connected to the GND on the Arduino UNO
  • out pins are connected to digital pins D2 and D3 on the Arduino UNO

DC Motors

  • The motors are connected to the OUT1, OUT2, OUT3, and OUT4 pins of the L298N motor drivers

HC-SR04 Ultrasonic Sensor

  • VCC is connected to 5V on the Arduino UNO
  • GND is connected to GND on the Arduino UNO
  • ECHO is connected to analog pin A0 on the Arduino UNO
  • TRIG is connected to analog pin A1 on the Arduino UNO

L298N DC Motor Driver

  • 12V is connected to the power supply through a rocker switch
  • GND is connected to the negative terminal of the power supply
  • 5V is connected to the 5V pin of the ESP32-CAM
  • Control pins ENA, ENB, IN1, IN2, IN3, and IN4 are connected to various digital pins on the Arduino UNO
  • OUT1, OUT2, OUT3, and OUT4 are connected to the DC motors

18650 Li-ion Batteries

  • The positive terminals are connected in series with rocker switches
  • The negative terminals are connected to the GND of the circuit

ESP32-CAM

  • 5V is connected to the 5V output of the L298N motor driver
  • GND is connected to the negative terminal of the power supply
  • VOT and VOR are connected to digital pins D0 and D1 on the Arduino UNO for serial communication

Rocker Switches

  • Used to control the connection between the batteries and the rest of the circuit

Documented Code

#include <SPI.h>
#include <MFRC522.h>

// Define sensor pins
const int leftSensorPin = 2;
const int rightSensorPin = 3;
const int trigPin = 16;
const int echoPin = 17;

// Define motor pins
const int motor1Enable = 10;
const int motor1In1 = 9;
const int motor1In2 = 8;
const int motor2In3 = 7;
const int motor2In4 = 6;
const int motor2Enable = 5;
// Define RFID pins
#define RST_PIN 14
#define SS_PIN 15

MFRC522 mfrc522(SS_PIN, RST_PIN);  // Create instance of the RFID reader

bool manualMode = false; // Flag for manual mode

void setup() {
  // Initialize motor pins
  pinMode(motor1Enable, OUTPUT);
  pinMode(motor1In1, OUTPUT);
  pinMode(motor1In2, OUTPUT);
  pinMode(motor2Enable, OUTPUT);
  pinMode(motor2In3, OUTPUT);
  pinMode(motor2In4, OUTPUT);

  // Initialize sensor pins
  pinMode(leftSensorPin, INPUT);
  pinMode(rightSensorPin, INPUT);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  // RFID initialization
  SPI.begin();        // Start SPI communication
  mfrc522.PCD_Init(); // Init MFRC522 module

  Serial.begin(9600);  // For debugging
}

void loop() {
  if (Serial.available() > 0) {
    char command = Serial.read();
    handleCommand(command);
  }

  if (!manualMode) {
    autoNavigate(); // Only run automatic navigation if not in manual mode
  }
}

void handleCommand(char command) {
  // Handle serial commands for manual control
}

// Main navigation logic
void autoNavigate() {
  // Autonomous navigation logic
}

// Function to calculate the distance using ultrasonic sensor
long getUltrasonicDistance() {
  // Ultrasonic distance measurement logic
}

// Function to move robot forward slowly for RFID scan
void slowlyApproachObject() {
  // Logic for slow approach for RFID scanning
}

// Function to check for RFID tag
bool checkForTag() {
  // RFID tag detection logic
}

// Function to avoid obstacle and check distances to left and right
void avoidObstacle() {
  // Obstacle avoidance logic
}

// Function to reverse until a specified distance from an object
void reverseUntilDistance(int targetDistance) {
  // Logic for reversing until a certain distance is reached
}

// Motor control functions
void moveForward() {
  // Logic to move the robot forward
}

void moveBackward() {
  // Logic to move the robot backward
}

void turnLeft() {
  // Logic to turn the robot left
}

void turnRight() {
  // Logic to turn the robot right
}

void stopRobot() {
  // Logic to stop all motor activity
}

This code is designed to run on the Arduino UNO and controls the robot's movement based on sensor inputs. It includes functions for manual control via serial commands, autonomous navigation, RFID scanning, and motor control. The robot can navigate autonomously by following a line and avoiding obstacles, or it can be controlled manually through serial commands. The ESP32-CAM module can be used for additional functionalities such as video streaming or image capture.