Anang Tadar
Published © Apache-2.0

Obstacle Avoiding Robot- Compact

It's a obstacle avoiding robot which uses HC-SRF-05 for obstacle detection. Best of its class, compact, rechargeable and precisely made.

IntermediateShowcase (no instructions)4 hours1,751
Obstacle Avoiding Robot- Compact

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
You can use any other arduino board, probably an arduino UNO if you are new.
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
I am currently using a HC-SRF-05 now because its more precise. if you want to upgrade to same you can do it, you don't need to change the code.
×1
Dual H-Bridge motor drivers L298
SparkFun Dual H-Bridge motor drivers L298
This is the motor driver I am using. its best because its small and compact for my compact robot.
×1
BO motor with wheel
you can use from 60 rpm to 150 rpm.
×1
18650 lithium Ion battery
I am using 4 18650 lithium ion cell connected in series. Buy from the link to get some discount
×1
Castor wheel
Buy from the link and get discount.
×1
SG90 Micro-servo motor
SG90 Micro-servo motor
×1
Slide Switch
Slide Switch
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

Wiring diagram

Just connect the wire as shown. You can connect a switch in between the battery and the l298 circuit.

Code

Code for Autonomous Robot

Arduino
Just copy and paste it on to your Arduino IDE or download it. Don't forget to include or install the NEwping library
//This is the code for the robot
// make sure to  include the NewPing and Servo Libraries  before uploading else it will show error

#include <NewPing.h>
#include <Servo.h>
#define TRIG_PIN A4
#define ECHO_PIN A5
#define MAX_DISTANCE 200
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE);
Servo myservo;

boolean goesForward = false;
int distance = 100;
int speedSet = 0;

//Motor A
const int motorPin1  = 11;
const int motorPin2  = 10;
//Motor B
const int motorPin3  = 6;
const int motorPin4  = 5;


void setup()
{


  myservo.attach(9);
  myservo.write(115);
  delay(2000);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  
}


void loop()
{

  int distanceR = 0;
  int distanceL =  0;
  delay(40);

  if (distance <= 15)  // Adjust the value 15 accordingly to let your robot stop at desired distance from the robot
  {
    moveStop();
    delay(100);
    moveBackward();
    delay(600);
    moveStop();
    delay(200);
    distanceR = lookRight();
    delay(200);
    distanceL = lookLeft();
    delay(200);
    if (distanceR >= distanceL)
    {
      turnRight();
      moveStop();
    } else
    {
      turnLeft();
      moveStop();
    }
  }
  
  else
  {
    moveForward();
  }
  distance = readPing();
}





int lookRight()
{
  myservo.write(50);
  delay(500);
  int distance = readPing();
  delay(100);
  myservo.write(115);
  return distance;
}

int lookLeft()
{
  myservo.write(170);
  delay(500);
  int distance = readPing();
  delay(100);
  myservo.write(115);
  return distance;
  delay(100);
}

int readPing() {
  delay(70);
  int cm = sonar.ping_cm();
  if (cm == 0)
  {
    cm = 250;
  }
  return cm;



}


void moveStop() {
  analogWrite(motorPin1, 0);
  analogWrite(motorPin2, 0);
  analogWrite(motorPin3, 0);
  analogWrite(motorPin4, 0);
}


void moveForward() {
  analogWrite(motorPin1, 255);   //the value 0 - 255 here is a PWM so you can change it to increase or decrease your motor speed
  analogWrite(motorPin2, 0);
  analogWrite(motorPin3, 235);    // this one too is the PWM for other motor
  analogWrite(motorPin4, 0);

}
void moveBackward() {

  analogWrite(motorPin1, 0);
  analogWrite(motorPin2, 255);
  analogWrite(motorPin3, 0);
  analogWrite(motorPin4, 255);

}
void turnRight() {
  analogWrite(motorPin1, 255);
  analogWrite(motorPin2, 0);
  analogWrite(motorPin3, 0);
  analogWrite(motorPin4, 255);
  delay(500);
  moveForward();

}

void turnLeft() {
  analogWrite(motorPin1, 0);
  analogWrite(motorPin2, 255);
  analogWrite(motorPin3, 255);
  analogWrite(motorPin4, 0);
  delay(500);
  moveForward();
}

Credits

Anang Tadar

Anang Tadar

2 projects • 2 followers
I love tech

Comments