I am building a motion following robot. It has two PIR sensors on the left and right. It has an ultrasonic sensor in the front. When either of the PIR sensors detect motion then it turns to that side.

When both the PIR sensors sense motion then I calculate the distance first( with the ultrasonic sensor, if distance <= 50 cm and > 5 cm then the robot moves forward. But it could mean that the motion is either in the front or in the back. Thus, my robot moves forward even the motion is behind it.

I need some help devising a method so that when motion is detected behind then the robot will turn back. I tried making the robot turn right when both the sensors detect motion, then it will turn towards the left(i.e. front) or right(i.e. back). But in reality this method is not so practical as it would to a lot of confusion when the intruder we are trying to detect is still in motion. Plz help. A sample sketch would help.

  • PIR sensors only detect moving heat sources. That is, moving relative to the PIR sensor. If the PIR sensor is moving then everything is moving relative to it, so who knows what it will be detecting. PIR is not, IMHO. a good choice for this kind of thing.
    – Majenko
    Jan 24, 2016 at 14:21
  • Dont worry about that. The if statement conditions state that when only one PIR detects motion, then the robot turns towards that side. Then it again checks the outputs.
    – HDatta
    Jan 24, 2016 at 15:00
  • Couldn't you point the PIRs slightly to the back. So you essentially split the 360degrees into three sections.
    – Gerben
    Jan 24, 2016 at 19:58
  • Was the answer helpful? If so, please accept it by clicking on the "tick" icon next to the answer, and also possibly upvote it. This indicates to other users that you found the answer useful, and it also stops Stack Exchange from periodically "bumping" your question in the hope of getting an accepted answer. Thanks for your understanding and cooperation!
    – Majenko
    Jan 25, 2016 at 22:57

1 Answer 1


This is a basic problem of only having two sensors. There is no way that two sensors of this simple type (some motion yes/no) can be used to work out where the motion comes from except when there is a difference between the two sensors. Anything else is, and always will be, ambiguous.

If you think of it as a circle divided in two, you have only three possible conditions:

enter image description here

That is, left, right or somewhere. Not ideal.

So you need more data to be able to make a decision. That means more sensors. The ideal scenario is four sensors - left, right, front and rear. That gives you many possible scenarios, including:

enter image description here

As you can see from that you have a full 360° idea of where the motion is. You can even know how far to the left it is by if the front or rear is also on. For instance, using sailing terminology, those 8 circles would mean (in order):

  1. Motion on the left beam
  2. Motion on the port bow
  3. Motion dead ahead
  4. Motion on the starboard bow
  5. Motion on the starboard beam
  6. Motion on the starboard quarter
  7. Motion dead astern
  8. Motion on the port quarter

Also the dead-ahead and dead-astern could be indicated by just the forward or rear sensor on and the left/right sensors both off. You could even use that fact to give some indication of the distance of the motion depending on the angular coverage of the different sensors.

  • OK that is a good answer, I had thought of a similar answer but using only 3 PIR and 1 ultrasonic. Do u think it is possible?
    – HDatta
    Jan 24, 2016 at 15:53
  • 3 PIR could be possible, but harder to manage. You could either have the three as an equilateral triangle, but then you won't really know if it's left or left and behind, etc. You could have three of the four laid out as above - just the 4 with one missing - and assume that if the front is off the rear must be on when both left and right are also on - which is most likely the case. So you could save one PIR that way.
    – Majenko
    Jan 24, 2016 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.