This circuit is designed to control an obstacle-avoiding robot using an Arduino UNO microcontroller. The robot utilizes four infrared (IR) sensors to detect obstacles and two DC motors for movement. The motors are driven by an L298N DC motor driver, which is controlled by the Arduino UNO. The IR sensors provide input to the Arduino, which then processes the data and sends commands to the motor driver to maneuver the robot accordingly. A 12V battery is used to power the motor driver, which also supplies 5V to the Arduino and the IR sensors.
/*
Obstacle Avoiding Arduino Robot Sketch
by Roland
www.teachmemicro.com/arduino-robot-obstacle-avoiding
*/
// Motor Driver Pins
int Motor_INA = 3;
int Motor_INB = 5;
int Motor_INC = 6;
int Motor_IND = 9;
// Sensor Pins
int Sensor_IN1 = 10;
int Sensor_IN2 = 11;
int Sensor_IN3 = 12;
int Sensor_IN4 = 13;
void setup() {
// Initialize motor driver pins
pinMode(Motor_INA, OUTPUT);
pinMode(Motor_INB, OUTPUT);
pinMode(Motor_INC, OUTPUT);
pinMode(Motor_IND, OUTPUT);
// Initialize sensor pins
pinMode(Sensor_IN1, INPUT);
pinMode(Sensor_IN2, INPUT);
pinMode(Sensor_IN3, INPUT);
pinMode(Sensor_IN4, INPUT);
// Start Serial Communication
Serial.begin(9600);
}
void goReverse(){
analogWrite(Motor_INA, 0);
analogWrite(Motor_INB, 50);
analogWrite(Motor_INC, 0);
analogWrite(Motor_IND, 50);
Serial.println("Moving Reverse");
delay(500);
}
void goRight(){
analogWrite(Motor_INA, 0);
analogWrite(Motor_INB, 0);
analogWrite(Motor_INC, 50);
analogWrite(Motor_IND, 0);
Serial.println("Turning Right");
}
void goLeft(){
analogWrite(Motor_INA, 50);
analogWrite(Motor_INB, 0);
analogWrite(Motor_INC, 0);
analogWrite(Motor_IND, 0);
Serial.println("Turning Left");
}
void stopBot(){
analogWrite(Motor_INA, 0);
analogWrite(Motor_INB, 0);
analogWrite(Motor_INC, 0);
analogWrite(Motor_IND, 0);
Serial.println("Stopping");
}
void goStraight(){
analogWrite(Motor_INA, 50);
analogWrite(Motor_INB, 0);
analogWrite(Motor_INC, 50);
analogWrite(Motor_IND, 0);
Serial.println("Moving Straight");
}
void loop() {
// Read sensor values
int sensor1 = digitalRead(Sensor_IN1);
int sensor2 = digitalRead(Sensor_IN2);
int sensor3 = digitalRead(Sensor_IN3);
int sensor4 = digitalRead(Sensor_IN4);
// Print sensor values to Serial Monitor
Serial.print("Sensor IN1: "); Serial.print(sensor1);
Serial.print(" | Sensor IN2: "); Serial.print(sensor2);
Serial.print(" | Sensor IN3: "); Serial.print(sensor3);
Serial.print(" | Sensor IN4: "); Serial.println(sensor4);
// Delay for stability
delay(500);
// Determine movement based on sensor inputs
if(sensor1 == 0 || sensor2 == 0){
Serial.println("Obstacle Detected on the Left Side");
goReverse();
delay(100);
stopBot();
delay(100);
goRight();
delay(100);
stopBot();
delay(100);
goStraight();
} else if(sensor3 == 0 || sensor4 == 0){
Serial.println("Obstacle Detected on the Right Side");
goReverse();
delay(100);
stopBot();
delay(100);
goLeft();
delay(100);
stopBot();
delay(100);
goStraight();
} else if(sensor1 == 0 && sensor2 == 0 && sensor3 == 0 && sensor4 == 0){
Serial.println("Obstacle All Around! Stopping.");
stopBot();
} else {
Serial.println("No Obstacle, Moving Straight");
goStraight();
}
}
This code is responsible for controlling the robot's movements based on the input from the IR sensors. It defines functions for moving the robot in different directions and stopping it. The loop
function continuously checks the sensor inputs and decides the robot's movement accordingly. If an obstacle is detected on either side, the robot will reverse, stop, turn in the opposite direction, and then proceed straight. If obstacles are detected all around, the robot will stop. If no obstacles are detected, the robot will move straight ahead.