Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
devansh_tangri
Published © GPL3+

Object Following Robot

This robot tracks and follows an object withing a linear proximity of 50 cm and 120° spherical proximity of about 5cm to 10cm.

IntermediateShowcase (no instructions)7,339

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
Any Arduino would work
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
Proximity Sensor
Proximity Sensor
Use HW - 201
×4
SG90 Micro-servo motor
SG90 Micro-servo motor
×1
Dual H-Bridge motor drivers L298
SparkFun Dual H-Bridge motor drivers L298
×1
DC motor (generic)
×2
SparkFun Breadboard Power Supply 5V/3.3V
SparkFun Breadboard Power Supply 5V/3.3V
×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)
Hot glue gun (generic)
Hot glue gun (generic)

Story

Read more

Schematics

Connection Diagram

Connection Diagram Fritzing File

Code

Arduino Code

Arduino
The procedures (functions) left(), right(), forwards() and backwards() can be changed according to your connections and components. You can also swap in1, in2, in3 and in4 pins.
const int in1 = 2;
const int in2 = 3;
const int in3 = 4;
const int in4 = 5;
const int L = 6;
const int U = 7;
const int R = 8;
const int D = 9;

int di;

int posY = 90;
#include <Servo.h>
#include <NewPing.h>

#define T 13
#define E 12
#define MD 50

NewPing sonar(T, E, MD);
Servo Y;

void setup()
{
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  pinMode(L, INPUT);
  pinMode(U, INPUT);
  pinMode(R, INPUT);
  pinMode(D, INPUT);
  Y.attach(10);
}

void left() {
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
}
void right() {
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
}
void forwards() {
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
}
void OFF() {
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
}
void Distance() {
  di = sonar.ping_cm();

  if(di >= 10 && di <= 50) {
    forwards();
  }
  else {
    OFF();
  }
}

void loop()
{ 
  if(digitalRead(U) == 1 && digitalRead(D) == 0)
  {
    while(digitalRead(U) != 0 && digitalRead(D) != 1)
    {
      if(posY == 50)
      {break;}
      posY--;
      Y.write(posY);
      delay(3);
    }
  }
  else if(digitalRead(U) == 0 && digitalRead(D) == 1)
  {
    while(digitalRead(U) != 1 && digitalRead(D) != 0)
    {
      if(posY == 160)
      {break;}
      posY++;
      Y.write(posY);
      delay(3);
    }
  }

  if(digitalRead(L) == 0 && digitalRead(R) == 1) {
    left();
  }
  else if(digitalRead(L) == 1 && digitalRead(R) == 0) {
    right();
  }
  else {
    Distance();  
  }
}

Credits

devansh_tangri
2 projects • 10 followers
I'm a 17 yo student, making Arduino Projects and Electrical Circuits is my favorite hobby. I have been making circuits for about 8 years.
Contact

Comments

Please log in or sign up to comment.