Introduction: Motion Following Robot

Picture of Motion Following Robot

Hello instructables community!

After messing around with an Arduino for a couple months and avidly reading instructables, I decided that I would finally publish my own. I wanted to create something that moves by itself. I also wanted to create a system that reacts to the outside environment. After some thought I decided on a simple motion follower.

Potential Applications:

• Motion following camera
• Robots that will acknowledge their environments
• Motion following turret
• Educational projects
• Obstacle avoiding robots

Required Materials:

• Breadboard
• Ultrasonic Range Sensor x2 (I used the four pin version so if you are using the ping version you'll have to change the code a bit)
• Servo (I used a micro size)
• Arduino UNO or similar microcontroller
• Jumper Cables (female to male and male to male)
• Particle Board
• 9v Battery and connector
• Something to mount the servo on (I used some poster mounting putty)

Required Tools:

• Hot glue gun
• Xacto knife
• Computer with Arduino IDE
• Electrical tape or similar

Step 1: Build the Sensor Mount

Picture of Build the Sensor Mount

First, we will need to build the sensor mount. For this I built a prototype out of cardboard and then built my final project in particle board. In the future I plan to 3d print a perfect mount for the sensor.

Cut a rectangle that is about the same height as one of the sensors and an 1/8th inch (about 3mm) longer than two of them together. Set the rectangle aside and cut out two identical isosceles triangles with the odd angle out being 120 degrees. I cut it at 120 degrees because the range sensors have a 15 degree cone that they measure, this allows for no blind spots while optimizing the area that is sensed.

Finally, place the sensors on the mount to determine where to cut the rectangular holes for them to fit in. Make the hole as small as possible because this snug fit makes up for not using any other adhesives or connectors. Now cut another small rectangular hole on the bottom triangle of the mount to allow for jumper cables to be passed through. Once the jumper cables are plugged in, the sensors should stay in place. If they don't, you will need to fasten them with some hot glue or pins.

When you are done with the mount, attach it to the servo.

Step 2: Wire Everything!

Picture of Wire Everything!

Now all you need to do is connect everything! I used Fritzing to create a circuit diagram. I have also provided some pictures of the final product.

The code provided uses digital pins 9 through 13. Pin 9 is the data pin for the servo. Pins 10 and 11 are the echo and trig pins, respectively, of the left sensor. Pins 12 and 13 are the echo and trig pins, respectively, of the right sensor. I connected the 5v and gnd pins from the Arduino to the breadboard and then used jumpers to connect the servo and sensors.

Step 3: Code

Picture of Code

The code that I provided allows for the changing of the distance threshold. So in other words, how far the sensors can see. I will be uploading a new instructable soon to show how you can make the sensors even more accurate using a temperature sensor. Finally, my friend showed me a cool way of debugging really quick. All you do is use a boolean and some if statements. If the boolean is true, then serial communication will be on and communicating. If the boolean is false, then the program will run much faster but not communicate.

I will attach the .ino file, otherwise you can copy it from here:


Motion Follow Created by Calvin Kielas-Jensen

Using an Arduino UNO, check for the circuit diagram.

This script allows two ultrasonic range sensors to follow movement while mounted on the top of a servo. The distance threshold can be changed but should not be set too far as the sensors will begin to fail.

Anyone is welcome to use and modify this code as long as I am given credit. Thank you for respecting the open source movement!

************************************************************************************************** *************************************************************************************************/


Servo myservo;

const int Lin = 10, Rin = 12, Lout = 11, Rout = 13, serv = 9; //setting sensor pins and servo pin

// establish variables for duration // and the distance result in inches long Rduration, Lduration, Rinches, Linches;

int threshold = 10; //Sensor threshold in inches

int angle = 80; //Initial angle

boolean debug = false; //Serial communication for debuging. Set to true for serial communication.

void setup() { // initialize serial communication: if (debug) { Serial.begin(9600); } myservo.attach(9); //attach servo to pin 9 }

void loop() { //Most of the sensor code has been taken from David Mellis's PING sensor code //I modified it for a 4 pin sensor as oppsed to the 3 pin sensor // Give a short LOW pulse beforehand to ensure a clean HIGH pulse: pinMode(Rout, OUTPUT); digitalWrite(Rout, LOW); delayMicroseconds(2); digitalWrite(Rout, HIGH); delayMicroseconds(5); digitalWrite(Rout, LOW);

Rduration = pulseIn(Rin, HIGH); pinMode(Lout, OUTPUT); digitalWrite(Lout, LOW); delayMicroseconds(2); digitalWrite(Lout, HIGH); delayMicroseconds(5); digitalWrite(Lout, LOW);

Lduration = pulseIn(Lin, HIGH);

// convert the time into a distance Rinches = microsecondsToInches(Rduration); Linches = microsecondsToInches(Lduration); if (debug) { Serial.print("Left: "); Serial.print(Linches); Serial.println(" in"); Serial.print("Right: "); Serial.print(Rinches); Serial.println(" in"); } follow(); }

long microsecondsToInches(long microseconds) {

// According to Parallax's datasheet for the PING))), there are

// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per

// second). This gives the distance travelled by the ping, outbound

// and return, so we divide by 2 to get the distance of the obstacle.

// See:

return microseconds / 74 / 2; }

void follow() { if (Linches <= threshold || Rinches <= threshold) { if (Linches + 2 < Rinches) { angle = angle - 2; } if (Rinches + 2 < Linches) { angle = angle + 2; } } if (angle > 160) { angle = 160; } if (angle < 0) { angle = 0; } myservo.write(angle); }

Step 4: Conclusion

Picture of Conclusion

Once the code has been loaded, plug in the 9v battery and watch it start following anything that comes within the threshold range!


  • If the sensors keep turning the wrong way, try switching the cables in pins 10 and 11 with the cables in pins 12 and 13.
  • If the sensors don't move at all, or just a little bit, check the wiring. It is really easy to accidentally move the jumper cables over one spot on the breadboard.
  • If the sensors are moving really slowly, go back to the code and make sure that the debug is false. The serial communications can really slow down the reaction time of the robot.
  • If you are still having issues, first make sure that the Arduino is on and that all jumper cables are in the correct spots. Switch the debug to true and check to see if the range sensors are working. You might also want to test the servo to make sure that it is also in working condition.

Some improvements I am planning on for the future:

  • Improving the accuracy of the sensors with the use of a temperature sensor
  • 3d printing the sensor mount and servo mount
  • Adding another servo and sensor to allow for vertical movement
  • Switching out the servos with stepper motors to allow for 360 degree following

I had a great time writing this instructable and hope to create many more! Please let me know how I did and how I can improve my future instructables.


thesquidguy78 (author)2017-11-09

does it matter which way the sensors are placed

It does. You'll have to look at the specs of your sensors and do some testing on your own to determine the best placement of the sensors. If you look through the comments here, you'll see why I picked the angle I did for the sensors I was using. It was a simple approximation though and I did not perform any thorough engineering analysis so be sure to take that with a grain of salt.

thesquidguy78 (author)2017-10-31

@MagicByCalvin do you have any methods to print the real time data into the arduino serial monitor?

with still having a decent time reaction (not delayed by more than 4 seconds

Try increasing the Baud rate to 115200 (9600 is the default on Arduino). Even with a baud of 9600 though, you shouldn't be delayed by 4 seconds, you should be delayed for a couple milliseconds at most.


but will it track smoothly or stiffly

It should still track smoothly. However, due to the nature of the system, even without serial communication, the follower doesn't move very smoothly. In order to move smoothly, a lot more work would have to go into the design. For one, more sensors would need to be used to increase the resolution of detection. Two, an output smoothing controller should be designed. Three, a better motor should be used. Those are some of the design tweaks I can come up with off the top of my head if you want to keep the system from jerking as it tries to follow you.

aditya.15jics007 (author)2017-09-08

Sir, i want to know about specification of the requirement for this project.

I am not sure what you are asking.

I want to ask about the specification of requirements as which sensor or servo you have used in project

nigel.trewartha (author)2017-02-18


May I suggest puting the source + construction info in GIT?


Thank you for the suggestion, I have been learning GIT and plan on uploading it there.

Bonzadog (author)MagicByCalvin2017-09-08


Thank you for your reply.

I think I will implement this -when I have time - on a Raspberry Pi. I want to do this in C and Python.

MagicByCalvin (author)Bonzadog2017-09-08

Sounds like an excellent idea! In case you aren't very familiar with embedded systems and electronics, I would recommend that you get a motor driver board that can run off of 3V3. If I recall correctly, the Raspberry Pi uses 3V3 logic and does not have as much protection as an Arduino does when it comes to driving devices from the GPIO pins.

Botyard (author)2016-03-17

If anybody wants to 3D print the parts I added my design at Thingiverse:

MagicByCalvin (author)Botyard2017-09-08

That's awesome! Thanks for providing that. This is why I love these types of communities!

shwami (author)2016-03-17

could you talk about why you chose to angle the sensors outwards away from each other? would angling them inwards (inside of a /\ instead of outside of the /\) make a difference? In stereo imagery, we angle the camera sensors inwards focusing on a point lying on an axis at equal distance from the sensors. Would that work better here as well?

MagicByCalvin (author)shwami2016-03-28

That is a great question. I did not apply any special analysis, I chose the angle based on the cone of sound produced by the sensors. I figured that angling the sensors so that the cones just touched would produce the best result. Upon reflecting on my design, I should have probably done some analysis and testing to produce the ideal angle. I am not familiar with the physics of stereo imagery but my thoughts are that I would not want to apply that for this design. I wanted the design to be able to pick up objects within the widest range in front of the sensors. My thoughts are that angling the sensors inwards would produce better detection of an object in front of them but would not detect objects as well that are further off center. I would love to hear your thoughts on it though!

shwami (author)MagicByCalvin2016-03-28

Indeed! now that I also had some more time to think about this, angling the sensors inwards would make the detection range focused right in the front, not exactly the requirement in this case. Having said that, I would go for a larger overlap of the cones to avoid interference with return sound waves. I am assuming that sounds picked up at the edges of the cone will lead to noisy readings as compared to the center of the cone. Sensitivity to the sounds at the edge of the cone may also get affected due to returning waves (after bouncing off nearby walls, perhaps?). By listening closer to the center of the cone, even with a tighter combined cone, you might get more accurate decisions.

MagicByCalvin (author)shwami2017-09-08

I believe that you both are probably correct. When I originally created this Instructable, I was quite new to everything and hadn't thought about the physics and engineering behind ultrasonic range sensors. If I were to redo this project, I would certainly study the physics behind ultrasonic sensing. Thanks for the tip!

afridave (author)MagicByCalvin2016-07-28

and...of course the more sensors you add the better it will detect...why not sensors all the way around?....just a thought.

kimosubby (author)2016-12-29

It is very easy to just copy and paste the code BUT doing that also copys spaces and unseen asci code as well. You need to go through the sketch and backspace any spaces and then re-introduce them with the space or tab key.
Also before that move all curly brackets to new lines using the return key and that way you can see the code packets and not delete tham in error.I know, been there and got the T shirt.

MagicByCalvin (author)kimosubby2017-09-08

Thanks for the advice. I will probably be uploading this to GitHub and providing a link to it rather than have the raw code posted.

deepanker9 (author)2017-03-24

want to add wheels to this so it can follow the direction. Can you provide sketch and wire diagram please..

MagicByCalvin (author)deepanker92017-09-08

You are asking a very low effort question. I am more than happy to help people with parts of their projects, but I am not going to build it for you. I would recommend that you look into how differential drive robots work. Then figure out how to power the motors as a function of how close the object is to each sensor.

onaroll (author)2017-04-29

Hi there,

This was really fun to build and it works just great. Is there a way to add two blinking led's that activate when the servo turns?


MagicByCalvin (author)onaroll2017-09-08

You certainly could do that. There are multiple ways this could be achieved. The easiest would be to use additional output pins and have them turn on any time you have the servo turn.

BatL1 (author)2017-07-17

What is the range of it? and how small of things can it detect?

MagicByCalvin (author)BatL12017-09-08

The range and size is completely dependent upon the ultrasonic range sensors that are used. Here is a datasheet I found from SparkFun:
It says the range is about 2cm to 4m. The measuring angle is 15 degrees which means you can use trigonometry to determine how large of an object could be sensed at a certain range. It is also important to note that the surface of the object being detected also influences how well it will be detected. If you are trying to detect a piece of noise-muffling foam, chances are pretty good that you won't be able to detect it since it will not reflect the sound waves back to the sensor. A flat rigid piece of acrylic or similar would be easily detected though.

This project could also be done using different sensors such as IR sensors or (if you have a ton of money) LIDAR.

Tony--K (author)2017-09-08

This is a great project.

I noticed that another author copied most of your descriptions into his project, word for word, after the opening paragraph. The Fritzing wiring diagram is identical too.

I posted a comment in his project, which is

It's sad that someone would copy another author's descriptions and pretend they are theirs. ----- Sad!

MagicByCalvin (author)Tony--K2017-09-08

Thank you for bringing that to my attention! This will have been the second time this has happened.

dasari12 (author)2016-12-27

hi i connected in a correct way....but not getting result....any one help me

MagicByCalvin (author)dasari122016-12-27

We need to see a picture of how you connected it. Could you tell us what the problem is? Is it not compiling, not uploading, not moving, moving erratically?

dasari12 (author)MagicByCalvin2016-12-28

the motor is not moving..

dasari12 (author)MagicByCalvin2016-12-27

i used the code which was attached to the article

MagicByCalvin (author)dasari122016-12-27

Ok, but what is wrong? What doesn't work? Your Arduino could be dead, you might not be uploading correctly, the motor may not be hooked up correctly, or a large number of other factors could be causing an issue. I need to know what your problems are and I need to see pictures of what you have hooked up in order to help you.

finlaye (author)2016-04-01

how could i change the threshhold so that i can add a camera

MagicByCalvin (author)finlaye2016-11-02

Were you hoping to have a camera mounted on the motion tracker or did you want to replace the ultrasonic sensors with a camera?

I would recommend replacing the sensors with a camera if you can send a live feed of the camera to a computer. You would then use computer vision in order to detect and track motion. A comment I left above regarding following a laser pointer might be helpful to you.

OlivD (author)2016-11-02

Hi, your project is really nice. I would like to get the same result but with sensor that follow a laser dot, so I can point where I want the sensor to "look". Do you know if it's a matter of programmation of the sensor or if i'll need to use other sensors.

MagicByCalvin (author)OlivD2016-11-02

Thank you!

You would need a sensor that can pick up the laser dot. In a very low light situation, you might be able to use photoresistors. But realistically, unless you were using a flashlight, a laser even in low light probably wouldn't work with a few discrete light sensors. I would recommend using OpenCV. OpenCV is a computer vision package that is built for both C++ and Python (as far as I know). You will need a computer to do the processing since an Arduino does not have sufficient power to process images. You will also need a camera. The camera doesn't need to be super fancy, in fact it could probably work with a cheap $20 webcam. The most important aspect of the camera would be its ability to differentiate the laser signal from the noise of the image. You would want to use OpenCV (or your own image processing algorithm of course, since this should be among the simpler tasks) to determine where in the image the laser dot was. Using that information, you could then drive the motor to, for example, always keep the laser dot in the middle of the image. For better results, you could pulse the laser at a specific frequency and use a filter to make sure that it is your laser and not a different laser or other sources of light that your algorithm picks up. Finally, I would recommend looking into control theory (if you have the time, desire, and ability) in order to better control the motor. A very basic, yet incredibly effective control system is a PID controller. I hope this helps!

HckmstrRahul (author)2015-08-12

can i try it with single Ultrasonic sensor? right now i have only one of them. if so, then suggest changes in the code that shd be done to achieve this. thanks fr the projct.

It would be possible with one ultrasonic sensor, but you would have to use a different approach to following whatever is in front of the sensor. The way I wrote the code looks at which sensor is closer to the object. If the object is closer to the right sensor, it moves right, if it is closer to the left, it moves left.
I am sure that there are other ways to do it, but I would have the sensor be wiggling left and right a couple degrees. If the sensor sees the object when it wiggles to the left, then it should turn further left, the same for the right. Let me know if that makes sense! If you are still interested I could write up a new Instructable sometime in the next two weeks.

Moo'L (author)MagicByCalvin2016-09-19

Really nice work! im in the same situation, i got one sensor and a servo, but im not good in coding at all. I try to implement your code in other (firmata) tying to use this behaviour with the max connection kit in ableton live.

beatniksailor (author)2016-08-25

I was working on something like this to get back at my cat for jumping on my head when I was sleeping(LOL). I H-glued the sensors on an old dvr that didn't burn right. The height of the servo left enough room to place a arduino Nano underneath. Good size to put on a $5 RC car.

niq_ro made it! (author)2016-04-22

I tested partial project, but is ok.. congratulations !

BrentR10 (author)2016-03-28

Any chance anyone has done any tinkering with pneumatic motion tacking? Or even high torque DC Gear Motor motion tracking? I'm trying to get a pneumatic character to follow people as they draw near, but not sure how to efficiently do it. I was thinking position feedback cylinders, but a significantly cheaper alternative would be preferred. reversable DC Gear motors would also be a option

MagicByCalvin (author)BrentR102016-03-28

While I am not familiar with pneumatics, I do think you could create a simple feedback system with DC motors. All you would need it to use a gear on the motor shaft to turn a potentiometer. The potentiometer would then create a voltage divider and the voltage can be measured. That voltage can then be mapped to angular position (and velocity and acceleration too if you add one or two differentiators to the control system). Depending on the system you are building, you will want to look into some control theory if you haven't already. For heavier and more powerful systems, if you have an unstable system, you could destroy your project or even bring harm to yourself or others.

chriscolon23 (author)2016-03-02

In the case of make this project but with a ultrasonic sensor of 3-pin, what I need to change in the code. I'm a beginner.

You'll have to look for the 3 pin code. I believe that I referred to it in my code. Essentially, instead of using two different pins, you use one pin as both of them by changing pins from output (make the ultrasonic sound) to input (receive the ultrasonic sound).

About This Instructable




Bio: I am currently a student in South Dakota. All my life I have been interested in tinkering with electronic amd mechanical gizmos, however after working ... More »
More by MagicByCalvin:Improve Ultrasonic Range Sensor Accuracy Motion Following Robot
Add instructable to: