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


haritsc's picture

how about if i want to use

how about if i want to use just 1 sensor? any difference?

Y-Geo's picture

Source for Sharp GP2 D12


 I am still finding my way around the world of Robot part suppliers. The hassle is finding the part, like the one I give as an example of in the title,

A} at a reasonable price

B} does not charge 15X times the cost of the item to send it to me

C} accepts PayPal as payment.

Am I asking the impossible with these points in mind?

Anyone on the Forum care to share their supplier with me?

Thanks for reading


PS after days of Google & Ebay digging  I posted this comment then I get a shout from Kawal (Thank you) to look on Ebay.UK and I think oh what the heck lets try again

  I GET 7 HITS mostly free post out of China and PAYPAL Grrrrr! I hate search engines!  LOL

Y-Geo's picture

Top Man


 Thanks that coding will help a bunch when I get my wireless camera /Tea carrying robot :-}

Jayanto's picture


you use distance sensor same as mine. It was sharp 2d120x, if i don't wrong LOL :D

kawal's picture

Ya those are sharp

Ya those are sharp GP2D12.


Jayanto's picture

same w/ me i use those sensor

same w/ me i use those sensor for my autonomous sumo robot but i wanna to use them on my land slide detector robot ( still research ) :D 

Canadian Robot's picture


HI Kawal, where can I found a Roboshield like this ? 




kawal's picture


You can buy roboshield from here.


Michel's picture

HI Kawal, can I get the code

HI Kawal, can I get the code fro this project I expect to use with your small robot ( Robot builder's shield V2 )

Canadian Robot

kawal's picture

I have updated the post with

I have updated the post with the code in it.Scroll down the post for code.

Add a new comment

Login or register to post comments

New to community of robots ?