Obstacle Avoiding Robot

Explanation

Obstacle Avoiding Robot



1. Motor Control: Pins ENA and ENB control the motor speeds using analogWrite. Pins IN1, IN2, IN3, and IN4 control the motor direction.



2. Ultrasonic Sensor: Measures the distance to obstacles using the trigPin and echoPin.


3. Servo Motor: Rotates to scan left and right to find the best direction to avoid obstacles.


4. Simple Logic: Moves forward until an obstacle is detected. Stops, scans left and right, then chooses the direction with more space.


Code

                                                    // Motor Pins
                                                    int ENA = 9;   // Speed control for left motor
                                                    int IN1 = 8;   // Left motor direction
                                                    int IN2 = 7;   // Left motor direction
                                                    int IN3 = 6;   // Right motor direction
                                                    int IN4 = 5;   // Right motor direction
                                                    int ENB = 10;  // Speed control for right motor
                                                    
                                                    // Ultrasonic Sensor Pins
                                                    int trigPin = 3;  
                                                    int echoPin = 2;
                                                    
                                                    // Servo Pin
                                                    #include <Servo.h>
                                                    Servo myServo;
                                                    int servoPin = 11;
                                                    
                                                    // Variables
                                                    long duration;
                                                    int distance;
                                                    
                                                    void setup() {
                                                      // Motor Pins as Output
                                                      pinMode(ENA, OUTPUT);
                                                      pinMode(IN1, OUTPUT);
                                                      pinMode(IN2, OUTPUT);
                                                      pinMode(IN3, OUTPUT);
                                                      pinMode(IN4, OUTPUT);
                                                      pinMode(ENB, OUTPUT);
                                                    
                                                      // Ultrasonic Sensor Pins
                                                      pinMode(trigPin, OUTPUT);
                                                      pinMode(echoPin, INPUT);
                                                    
                                                      // Servo Setup
                                                      myServo.attach(servoPin);
                                                      myServo.write(90);  // Center position
                                                    
                                                      Serial.begin(9600); // Serial Monitor for debugging
                                                    }
                                                    
                                                    void loop() {
                                                      distance = measureDistance();
                                                    
                                                      if (distance < 15) { // Obstacle detected
                                                        stopMotors();
                                                        delay(500);
                                                    
                                                        // Scan left
                                                        myServo.write(45);
                                                        delay(500);
                                                        int distanceLeft = measureDistance();
                                                    
                                                        // Scan right
                                                        myServo.write(135);
                                                        delay(500);
                                                        int distanceRight = measureDistance();
                                                    
                                                        // Return to center
                                                        myServo.write(90);
                                                        delay(500);
                                                    
                                                        // Decide direction
                                                        if (distanceLeft > distanceRight) {
                                                          turnLeft();
                                                        } else {
                                                          turnRight();
                                                        }
                                                        delay(500);
                                                      } else {
                                                        moveForward();
                                                      }
                                                    }
                                                    
                                                    // Functions for Motor Control
                                                    void moveForward() {
                                                      digitalWrite(IN1, HIGH);
                                                      digitalWrite(IN2, LOW);
                                                      digitalWrite(IN3, HIGH);
                                                      digitalWrite(IN4, LOW);
                                                      analogWrite(ENA, 150); // Speed
                                                      analogWrite(ENB, 150); // Speed
                                                    }
                                                    
                                                    void stopMotors() {
                                                      digitalWrite(IN1, LOW);
                                                      digitalWrite(IN2, LOW);
                                                      digitalWrite(IN3, LOW);
                                                      digitalWrite(IN4, LOW);
                                                    }
                                                    
                                                    void turnLeft() {
                                                      digitalWrite(IN1, LOW);
                                                      digitalWrite(IN2, HIGH);
                                                      digitalWrite(IN3, HIGH);
                                                      digitalWrite(IN4, LOW);
                                                      analogWrite(ENA, 150); // Speed
                                                      analogWrite(ENB, 150); // Speed
                                                    }
                                                    
                                                    void turnRight() {
                                                      digitalWrite(IN1, HIGH);
                                                      digitalWrite(IN2, LOW);
                                                      digitalWrite(IN3, LOW);
                                                      digitalWrite(IN4, HIGH);
                                                      analogWrite(ENA, 150); // Speed
                                                      analogWrite(ENB, 150); // Speed
                                                    }
                                                    
                                                    // Measure Distance using Ultrasonic Sensor
                                                    int measureDistance() {
                                                      digitalWrite(trigPin, LOW);
                                                      delayMicroseconds(2);
                                                      digitalWrite(trigPin, HIGH);
                                                      delayMicroseconds(10);
                                                      digitalWrite(trigPin, LOW);
                                                    
                                                      duration = pulseIn(echoPin, HIGH);
                                                      return duration * 0.034 / 2; // Convert to centimeters
                                                    }