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

Arduino Mega 2560 Controlled Autonomous Navigation Robot with RFID and Obstacle Avoidance

View and Edit Project In Cirkit DesignerImage of Arduino Mega 2560 Controlled Autonomous Navigation Robot with RFID and Obstacle Avoidance

Circuit Documentation

Summary

This circuit is designed to control a mobile robot equipped with various sensors and actuators. The core of the circuit is an Arduino Mega 2560 microcontroller, which interfaces with an L298N DC motor driver to control two MRB Planetary gearbox motors. The robot's navigation is assisted by three IR sensors and an HC-SR04 Ultrasonic Sensor for obstacle detection and avoidance. An RFID-RC522 module is used for identification and task assignment purposes.

Component List

Arduino Mega 2560

L298N DC Motor Driver

12V Battery

MRB Planetary Gearbox Motor (x2)

HC-SR04 Ultrasonic Sensor

IR Sensor (x3)

RFID-RC522

Wiring Details

Arduino Mega 2560

L298N DC Motor Driver

MRB Planetary Gearbox Motor

HC-SR04 Ultrasonic Sensor

IR Sensor

RFID-RC522

Documented Code

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

// Pin definitions for L293D on Arduino Mega 2560
#define IN1 22
#define IN2 23
#define IN3 24
#define IN4 25
#define ENA 5
#define ENB 6

// Pin definitions for sensors on Arduino Mega 2560
#define L_SENSOR_PIN 30
#define M_SENSOR_PIN 31
#define R_SENSOR_PIN 32
#define TRIG_PIN 26
#define ECHO_PIN 27
#define SDA_PIN 53
#define RST_PIN 49

// RFID setup
MFRC522 mfrc522(SDA_PIN, RST_PIN);

// Variables for navigation
int targetDestination = -1;
bool atDestination = false;

// Motor control functions for L293D
void moveForward() {
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
  analogWrite(ENA, 255); // Speed control for motor 1 (max speed)
  analogWrite(ENB, 255); // Speed control for motor 2 (max speed)
}

void turnLeft() {
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
  analogWrite(ENA, 200); // Adjust the speed as necessary
  analogWrite(ENB, 200);
}

void turnRight() {
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
  analogWrite(ENA, 200); // Adjust the speed as necessary
  analogWrite(ENB, 200);
}

void stop() {
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
  analogWrite(ENA, 0);
  analogWrite(ENB, 0);
}

// Ultrasonic sensor function
long readUltrasonicDistance() {
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);
  return pulseIn(ECHO_PIN, HIGH) / 58.2; // Convert to cm
}

void setup() {
  // Initialize motor pins
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);
  pinMode(ENA, OUTPUT);
  pinMode(ENB, OUTPUT);

  // Initialize sensor pins
  pinMode(L_SENSOR_PIN, INPUT);
  pinMode(M_SENSOR_PIN, INPUT);
  pinMode(R_SENSOR_PIN, INPUT);
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);

  // Initialize Serial and RFID
  Serial.begin(9600);
  SPI.begin();
  mfrc522.PCD_Init();
}

void loop() {
  // Read sensor IR
  bool lSensor = digitalRead(L_SENSOR_PIN);
  bool mSensor = digitalRead(M_SENSOR_PIN);
  bool rSensor = digitalRead(R_SENSOR_PIN);

  // Read distance from ultrasonic sensor
  long distance = readUltrasonicDistance();

  // Line following logic
  if (!atDestination) {
    if (mSensor) {
      moveForward();
    } else if (lSensor) {
      turnLeft();
    } else if (rSensor) {
      turnRight();
    } else {
      stop();
    }
  }

  // Collision avoidance logic
  if (distance < 20) { // If an obstacle is closer than 20 cm
    stop();
  }

  // RFID detection logic
  if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) {
    handleRFID();
    mfrc522.PICC_HaltA();
  }

  // Navigate to the target destination
  if (atDestination) {
    navigateToNextDestination();
  }
}

void handleRFID() {
  Serial.print("Card UID:");
  String uidString = "";
  for (byte i = 0; i < mfrc522.uid.size; i++) {
    uidString += String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
    uidString += String(mfrc522.uid.uidByte[i], HEX);
  }
  Serial.println(uidString);
  
  // Compare UID with predefined destinations
  if (uidString.equals("12 34 56 78")) {
    targetDestination = 1;
    Serial.println("Destination: Location 1");
  } else if (uidString.equals("87 65 43