Arduino Mega 2560 Controlled Autonomous Navigation Robot with RFID and Obstacle Avoidance
View and Edit Project In Cirkit Designer
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
- Microcontroller board based on the ATmega2560
- 54 digital input/output pins (of which 15 can be used as PWM outputs)
- 16 analog inputs
- 4 UARTs (hardware serial ports)
- 16 MHz crystal oscillator
- USB connection
- Power jack
- ICSP header
- Reset button
L298N DC Motor Driver
- Dual H-bridge motor driver
- Can drive two DC motors or one stepper motor
- Supply voltage up to 35V, max current 2A per channel
- Heat sink for better heat dissipation
- Screw terminal connectors for easy motor connection
12V Battery
- Provides power to the DC motors through the motor driver
MRB Planetary Gearbox Motor (x2)
- High torque DC motor with a gearbox for speed reduction
- Suitable for driving wheels or tracks of a mobile robot
HC-SR04 Ultrasonic Sensor
- Ultrasonic ranging module
- Provides 2cm - 400cm non-contact measurement function
- Ranging accuracy up to 3mm
IR Sensor (x3)
- Infrared obstacle detection sensor
- Digital output that triggers upon obstacle detection
- Used for line following or obstacle detection
RFID-RC522
- RFID reader/writer module
- Operates at 13.56 MHz
- Communicates with the Arduino via SPI
Wiring Details
Arduino Mega 2560
5VandGNDpins are used to power the IR sensors and the HC-SR04 Ultrasonic Sensor.- Digital pins
D5 PWMandD6 PWMare connected to theENAandENBpins of the L298N motor driver for PWM speed control. - Digital pins
D22,D23,D24,D25are connected toIN1,IN2,IN3,IN4on the L298N to control motor direction. - Digital pins
D30,D31,D32are connected to the output pins of the IR sensors. - Digital pins
D26andD27are connected to theTRIGandECHOpins of the HC-SR04 Ultrasonic Sensor. - Digital pins
D53andD49are connected to theSDAandRSTpins of the RFID-RC522 module.
L298N DC Motor Driver
12VandGNDpins are connected to the corresponding terminals of the 12V Battery.OUT1andOUT2are connected to one MRB Planetary gearbox motor.OUT3andOUT4are connected to the other MRB Planetary gearbox motor.
MRB Planetary Gearbox Motor
- Each motor has two terminals:
+and-which are connected to theOUTpins of the L298N motor driver.
HC-SR04 Ultrasonic Sensor
VCCandGNDare connected to the 5V and GND of the Arduino Mega 2560.TRIGandECHOpins are connected to digital pinsD26andD27of the Arduino Mega 2560.
IR Sensor
- Each sensor has
VCC,GND, andoutpins. VCCpins are connected to the 5V of the Arduino Mega 2560.GNDpins are connected to the GND of the Arduino Mega 2560.outpins are connected to digital pinsD30,D31,D32of the Arduino Mega 2560.
RFID-RC522
VCC (3.3V)is connected to the 3.3V output of the Arduino Mega 2560.RSTandSDApins are connected to digital pinsD49andD53of the Arduino Mega 2560.IRQis not used in this setup.MISO,MOSI,SCKare connected to the corresponding SPI pins of the Arduino Mega 2560.
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