Ultrasonic or IR sensor

Hi,

my project should count the number of peolple that enter or exit in a room.

With ultrasonic sensor (HCSR04) it isn’t perfect because in some cases it doesn’t count, it loss some entrance.

This is my sketch:

void loop() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print( "num_pers ");
lcd.print(pers);
digitalWrite( triggerPort1, LOW );
digitalWrite( triggerPort1, HIGH );
delayMicroseconds( 10 );
digitalWrite( triggerPort1, LOW );
long lenght = pulseIn( echoPort1, HIGH );

long distance = 0.034 * lenght / 2;

if( distance < 100 || distance > 0 ){
pers = pers + 1;
}

delay(500);
}

My question is:
can i obtain a better performance with an IR sensor? Moreover i need an ir sensor with a long detection distance.

Thank’s to everybody

Counting people is always a hard one as people can group up and there is no easy way to tell if this blob is one person or several. How sure are you that your device is not working as expected and that it is not people grouping up? Three people in one tight group as they walk in and then they leave going out with a space between them will be a problem regardless of the sensor type used.

If you have a way of preventing them from grouping up, a turn style or some such and a way for them to not fake out the IR sensor I would be temped to go that way. If they know where the beam is and want to fake it out the IR is likely easier to fake out with an extra high count than the HCSR04.

We would need to know more about what you are doing, how the sensors are set compared to the people ect. with a half second delay in your code it may be possible to walk past it while it is off. On the flip side you may have someone who stands in front of the sensor and gets counted several times.

I would suggest removing the delay(500); and adding some code to make sure the distance reading increases between people to what ever your background distance is before it can make the next count.