Object tracker with two sharp GP2D12

Object tracker with two sharp GP2D12


This is my first attempt at the object tracker and yeah it turned out to be very good.

Now some more details about this project

Components list:

A standard servo


Two sharp GP2D12 sensors(10 cm to 80 cm)


Arduino uno and a roboshield

For mounting the sensors on the servo you will need sensor brackets or you can try the method i used

I used acrylic strips


Then i stuck the two sensors with double sided tape as shown below


After that i mounted the above assembly on the servo using the double sided tape as you can see below


Heres the completed work


#include <Servo.h>
int sharp1 = 0;             // right sensor analogue pin(0)
int sharp2 = 1;            // left sensor analogue pin(1)
int leftdist = 0;           // variable to store left sensor reading
int rightdist = 0;        // variable to store right sensor reading
int headpin = 8;       // pin to which servo is connected
int pos = 90;          // center position for servo

Servo head;      // create a servo object

void setup() {
  head.write(pos);         //center the servo

void loop() {
  rightdist = analogRead(sharp1);
  leftdist = analogRead(sharp2);
  if (rightdist < 600 && leftdist < 600) faceforward();
  if (rightdist < 600 && leftdist < 400) faceright();
  if (leftdist < 600 && rightdist < 400) faceleft();

void faceright() {
  pos = pos + 1;    //  adding 1 to the servo command

void faceleft() {
  pos = pos - 1;    //subtracting 5 from the servo command

void faceforward () {

Any comments are appreciated

Thank you for viewing it !!

Embedded video:
See video


Red zinc's picture

hey can i get the code fro

hey can i get the code fro this... can u add some thing in this code.... i wanna use some dc motor with it that a any moving object is sensed by the sensor ... the robot start moving behind it... plz... can u generate that code for arduino...

kawal's picture

Hi I cant provide the ready


I cant provide the ready to use code for your robot because i dont know what all components you have used for your robot and what do you want your robot to do.

No one can give you the ready to use code.Read similar project's code and adapt that code for your own robot.

If you want you can ask in the forum


Rex's picture

This is really

This is really interesting.

Kawal, you're so cool.



Smoke 'em if you got 'em!

kawal's picture

Thanks Rex.

Thanks Rex.

Lasrin's picture


Love it!  Nice work!  Now maybe had a upper/lower sharp sensor to give you vertical tracking as well?

kawal's picture

Ya planning on to buy some

After all your code revision helped a lot

For vertical tracking planning on to buy some more sensors.Maybe ultrasonic sensors

and thanks for joining the website

Add a new comment

Login or register to post comments

New to community of robots ?