# Object tracking with single HC SR04?

Hi, I want to make a robot head that tracks objects horizontally. So, is it possible to do that with a single HC SR04 and a servo? Please help, I'm a just a 14 year old newbie!

is it possible to do that with a single HC SR04 and a servo?

That sounds quite possible, at least to a limited extent, but the sensor will need to scan for the object rather than follow it explicitly as it will not be able to determine the direction of movement of the object otherwise.

Can you read a distance using the sensor ?
Do you know how to control the servo ?

Yeah, I can do that both. But I just need help in the tracking part.

OK. Here are some ideas

Mount the sensor on the servo output arm such that it looks in the correct direction
Move the servo to 0 degrees
Move the servo through 180 degrees in 1 degree steps.
At each step measure the distance to the nearest object
If the distance measured is less that any previous distance measured in this sweep then save the current servo position and continue with the sweep.
Once the sweep is complete move the servo to the position where the shortest distance was measured.

Now you have a starting position

Sweep through a range of angles either side of the starting position.
Save the servo position for the shortest distance found.
Move the servo to the position where the shortest distance was measured.
Repeat the last 3 steps, which could usefully be in a function that takes the range of angles to be searched as a parameter. The same function could be used to perform the initial search.

Note that the servo needs to search for the nearest object and cannot just follow it. Note also that the object may move out of the range of the servo sweep if it moves quickly. If you determine the direction of movement of the object by doing 2 successive sweeps then you can confine the subsequent sweep to search in that direction. However, this will not work if the object changes direction or goes out of the scan angle on one side and appears back on the other. These cases must ne taken into account by your program.

Ok, I understood. Itâ€™s pretty easy! Thanks for the help!