Using a Sharp 2Y0A21 IR sensor to detect movement

Hi all,

I’m looking for a way to use a IR sensor to detect movement. I’ve seen some examples on the web on how to use a IR sensor, but I’m not sure about it. For instance I’ve seen IR sensors on parking doors and they have a reflector on the other side. None of the examples I’ve seen mentions the use of a reflector and I don’t know which role it really plays.

Can somebody fill me in here?, some link, tutorial, article I might have missed?

Thanks in advance!

The one on parking doors is a pair of emitter (on one side) and a receiver ( opposite side). If the receiver can't detect IR, it means there is an obstacle under the door so the garage door should not drop down. This is the transmission mode. If you are careful, you will find a similar mechanism at the end of a supermarket check out above the conveyer belt. If your items are blocking the beam, the belt is stopped. It only runs when there is nothing blocking the beam, supposedly bringing in your items from the cashier. The Sharp 2Y0A21 IR sensor has both emitter and receiver. The emitter emits outwards and the receiver will not receive anything unless an object is nearby and reflects the IR back for the receiver to receive. This is the so-called reflective mode. All bathroom automation (flush, soap, towel paper), supermarket door, etc work on this mode. I hope this all makes sense.

I'm looking for a way to use a IR sensor to detect movement.

A lot will depend over what distance you want to detect movement and what is moving that you want to detect.

The best way for people or animals is to use a PIR sensor as used in lamp movement detectors and burglar alarms. The P in PIR stands for passive and uses much lower wavelengths of IR light than your TV remote control.

The 2Y0A21 sensors return an analogue value so you can monitor those at a fixed interval and when the change from one interval to the next exceeds a threshold you consider you have detected movement. Much in the way of this game I made:- http://www.thebox.myzen.co.uk/Hardware/Sneak_Thief.html

Cool thanks for the info. The model I have operates between 10 and 80 cm (4 to 31.3 inches). So I guess that if I have to nearby object to reflect the beam, this is where the reflector comes in, right? Mainly I was thinking about using it to photograph birds flying and similar. My wife would kill me if I build a burglar alarm and it doesn't work when needed...

How reliable are these things?

I’ve just hooked one up on my Arduino Mega, put a box in front of the sensor and try it out.

It’s more or less ok, but every few readings, the measured distance drops significantly without any interaction from my side. For example the sensor is reading 16 cm and every 15 or 20 readings the distance drops to 11 or 12 cm.

Is that a normal behaviour? I’ve just hooked the +5V, the GND and the Vo to the pin A1 on the Arduino. The sketch I’m using to test it out is as follows:

#define ir 1


void setup()
{
  
  analogReference(DEFAULT);
  Serial.begin(9600);
  pinMode (ir, INPUT);
}

void loop()
{

  
  int raw=analogRead(ir);
  int volt=map(raw, 0, 1023, 0, 5000);
  int cm=(21.61/(volt-0.1696))*1000;
  
  Serial.println(cm); 
  
  delay(500);
  
  }

this is where the reflector comes in, right?

Not for this sort of sensor, it uses the reflections off an object and measures the return angle. If you put a reflector in the path that would confuse it because the returned angle is not constant. Think what happens with a visible light reflector it looks all speckelly.

How reliable are these things?

A lot depends on the type of surface you are reflecting off and the ambient lighting conditions. I made a theremin with two of them and didn't see the issue you report, it was detecting the distance from my hands pointing upwards.

Grumpy_Mike: A lot depends on the type of surface you are reflecting off and the ambient lighting conditions. I made a theremin with two of them and didn't see the issue you report, it was detecting the distance from my hands pointing upwards.

I have a pellet stove right behind me as I'm testing this, so that could have something to do with it. Another thing is that I bought these things on Amazon and they came from China. So I don't really know if they are the real deal or clones...

Edit: the stove has just turned itself off and there is no change. I've also tried different surfaces. It drops by 25-35% every once in a while...

You should not use this sensor outdoors unless it's night time. The sun has strong IR radiation that can interfere with it. Stove can do the same. I think Sharp has its sensors made in China so it's "real" I guess.

not that I don't trust China's quality, but I had some first-hand bad experience in the past, so...

Do you know of a sensor which could be useful at day light?

IR sensors are good, but you want some shielding from the sun and possibly one with transmission mode. Describe how the bird might trip the sensor? Is it next to some bird feed etc? If you can avoid sun light, say stay under something then it's ok to use your sensor.

Might be worth pointing out that, that kind of sensor doesn't detect movement, it detects presence. OP's subject line is about movement.

To have it detect movement, you would need to have the sketch compare the old distance with a new distance. Something then moving slightly sideways would not change its distance, and therefore not be deemed to have moved.

PIR is specifically meant to detect movement, because it "memorises" the heat pattern during its settling time and then alarms when that pattern is disturbed.

OK, if it's about movement sensing, a raspberry pi and web camera with proper code would do. I wonder if a computer mouse with a lens will do the same for small areas.