Arduino UNO-Based Obstacle Avoidance Robot with IR Sensors and Ultrasonic Sensor
View and Edit Project In Cirkit Designer
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
5VandGNDare connected to the power rails of the IR sensors and the HC-SR04 ultrasonic sensor- Digital pins
D2andD3are connected to the output of the IR sensors - Analog pins
A0andA1are connected to theECHOandTRIGpins of the HC-SR04 ultrasonic sensor - Digital pins
D5toD13are connected to the control pins of the L298N motor drivers Vinis connected to the power supply through a rocker switchGNDis connected to the negative terminal of the power supply- Digital pins
D0andD1are connected to theVOTandVORpins of the ESP32-CAM for serial communication
IR Sensors
vccpins are connected to the5Voutput of the Arduino UNOgndpins are connected to theGNDon the Arduino UNOoutpins are connected to digital pinsD2andD3on the Arduino UNO
DC Motors
- The motors are connected to the
OUT1,OUT2,OUT3, andOUT4pins of the L298N motor drivers
HC-SR04 Ultrasonic Sensor
VCCis connected to5Von the Arduino UNOGNDis connected toGNDon the Arduino UNOECHOis connected to analog pinA0on the Arduino UNOTRIGis connected to analog pinA1on the Arduino UNO
L298N DC Motor Driver
12Vis connected to the power supply through a rocker switchGNDis connected to the negative terminal of the power supply5Vis connected to the5Vpin of the ESP32-CAM- Control pins
ENA,ENB,IN1,IN2,IN3, andIN4are connected to various digital pins on the Arduino UNO OUT1,OUT2,OUT3, andOUT4are 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
GNDof the circuit
ESP32-CAM
5Vis connected to the5Voutput of the L298N motor driverGNDis connected to the negative terminal of the power supplyVOTandVORare connected to digital pinsD0andD1on 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.