stuck with Bidirectional people counter's sensors

Hi world,

I have an idea to make my room smarter than it is now.

I begin with buying Alexa in addition to some smart bulbs to control them by Alexa or my phone which is super cool.

Then, a month ago, I started a new project that controls the light automatically and counts how many people in a room. I still working on it until today.

simply it has two sensors in a row, when you entered the room, for example, the left sensor will detect you first, which means you entered the room, so we add one to the total number. Similarly, when you leave the room, the right sensor will detect you first which means
you’re leaving the room so we subtract one from the total number.

Fortunately, I succeed doing it. it worked very well and I am pleased that I could do it. However, I write the full documentation about how I do it on this website:

HERE

BUT, my door is 80 cm width and the ir sensors I used in this project detect till just 10cm more or less, so the IR sensors couldn’t able to detect any person entered or left the room.

However, I bought ultrasonic sensors and build another code for it. the problem I faced now is that when the first sensor detects the person, the second sensor is not able to detect him unless the person walks very slowly. I think it is coding problem.

this the code:

//by Bader Almazmumi ( jdawe02 )

//Ultrasonic Sensor pins (IN)
int EchoIN = 2;
int TrigIN = 3;

//Ultrasonic Sensor pins (OUT)
int EchoOUT = 5;
int TrigOUT = 6;

// Led pins
int LedIN = 4;
int LedOUT = 7;
int LedROOM = 8;

// Duration and distance
int durationIN = 0;
int distanceIN = 0;
int durationOUT = 0;
int distanceOUT = 0;

// toggles variables
boolean toggleIN = false;
boolean toggleOUT = false;

// other variables
int personCounter = 0;
int maximumDistanceByCm = 80;
boolean lighton = false;
int MAX_DISTANCE = 10;

void setup() {

  // Setup the pins
  Serial.begin(9600);
  pinMode(EchoIN, INPUT);
  pinMode(TrigIN, OUTPUT);
  pinMode(EchoOUT, INPUT);
  pinMode(TrigOUT, OUTPUT);
  pinMode(LedOUT, OUTPUT);
  pinMode(LedIN, OUTPUT);
  

}



void loop() {

  updateSensorInfo();
  

  //===============================================
  if (distanceIN <= MAX_DISTANCE && distanceIN >= 0) {
    Serial.println("DETECTED IN");
    digitalWrite(LedIN,HIGH);
    digitalWrite(LedOUT,LOW);

    
  if (distanceOUT < MAX_DISTANCE && distanceOUT > 0 ) {
      
      
      if (toggleIN) {
        personCounter--;
        delay(500);
        toggleIN = false;
        toggleOUT = false;
        Serial.print(personCounter);
        Serial.println(" | Exit");
      } else if (!toggleIN) {
        toggleOUT = true;
      }
    }
    
  } else if (distanceOUT <= MAX_DISTANCE && distanceOUT >= 0) {
    Serial.println("DETECTED out");
    digitalWrite(LedOUT,HIGH);
    digitalWrite(LedIN,LOW);

    if (distanceIN <= MAX_DISTANCE && distanceIN > 0 ) {
      
      if (toggleOUT) {
        personCounter++;
        delay(500);
        toggleIN = false;
        toggleOUT = false;
        Serial.print(personCounter);
        Serial.println(" | ENTERED");
      } else if (!toggleOUT) {
        toggleIN = true;
      }
    }

    
  }


  //===============================================

  //Arcturus-12's final modifications

  if (personCounter <= 0 && lighton == true) {
    //turnOffTheLight();
    lighton = false;

    Serial.println("OFF");

  } else if (personCounter > 0 && lighton == false) {
    //turnOnTheLight();
    lighton = true;
    Serial.println("ON");

  }

  delay(100);
//  sendData();
  //by using a system where "lighton" is checked before changing state, unnecessary POST requests are avoided
  //end of modifications

  Serial.println(personCounter);
}


void updateSensorInfo() { // This function is for first sensor.
  
  digitalWrite(TrigIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TrigIN, HIGH);
  delayMicroseconds(5);
  digitalWrite(TrigIN, LOW);
  durationIN = pulseIn (EchoIN, HIGH);
  distanceIN = (durationIN / 2) / 29.1;
  delay(200);
  digitalWrite(TrigOUT, LOW);
  delayMicroseconds(2);
  digitalWrite(TrigOUT, HIGH);
  delayMicroseconds(5);
  digitalWrite(TrigOUT, LOW);
  durationOUT = pulseIn (EchoOUT, HIGH);
  distanceOUT = (durationOUT / 2) / 29.1;
  
}

any idea about the suitable sensors for this project or idea how to fix the code.

THANKS.

I just wonder if you believe small children and babies are not people?

Paul

Paul_KD7HB:
I just wonder if you believe small children and babies are not people?

Paul

I believe that your question is not related to mine or I don't get you .. :slight_smile:

jdawe02:
I believe that your question is not related to mine or I don't get you .. :slight_smile:

You are counting people! Some people are 2 feet tall. Some people are carrying other very small people. Are you sure you are counting people or just adult people?

Paul

I think you are kidding me Paul. For now, it Should count any object whatever it is , approximately from 30 cm to 100 cm height.

Do you Have any helpful idea?

jdawe02:
I think you are kidding me Paul. For now, it Should count any object whatever it is , approximately from 30 cm to 100 cm height.

Do you Have any helpful idea?

No, not kidding. You think you are detecting people, but you are in fact detecting clothing the people are wearing. And there is your problem using ultrasonic sensors. Some fabric will just absorb the sound, Some will reflect it at an angle, so the receiver will not see it.

Paul

I don't understand why you use ultrasound sensors for that in the first place. I would take two IR beams, with either the detector or a reflector in the opposite door post, and place them about a meter above the ground.
This way:

  • it doesn't matter if someone tries to skirt the opposite post, they will be detected.
  • it's low enough for children to not go under it (unless of course they intentionally do so),
  • it's high enough not to count the two legs of a tall adult as two passes i.e. two people.

Babies are ignored - assuming they are being carried anyway, and will leave the same way they enter, and together with their guardian.

Your only problem may be people that turn around while inside the doorway, or if people pass through together (though at 80 cm wide that's a bit of a squeeze). Whether to account for this depends on how important those corner cases are to you.

Paul_KD7HB:
No, not kidding. You think you are detecting people, but you are in fact detecting clothing the people are wearing. And there is your problem using ultrasonic sensors. Some fabric will just absorb the sound, Some will reflect it at an angle, so the receiver will not see it.

Paul

Yeah, I got your point. good points.

After I did it by IR sensors, I wanted in first place a sensor that detects long distance,moreover, I watched this video which show what I want to do and he used the ultrasonic sensor.

So what is the best sensor should I use here.

wvmarle:
I don't understand why you use ultrasound sensors for that in the first place. I would take two IR beams, with either the detector or a reflector in the opposite door post, and place them about a meter above the ground.
This way:

  • it doesn't matter if someone tries to skirt the opposite post, they will be detected.
  • it's low enough for children to not go under it (unless of course they intentionally do so),
  • it's high enough not to count the two legs of a tall adult as two passes i.e. two people.

Babies are ignored - assuming they are being carried anyway, and will leave the same way they enter, and together with their guardian.

Your only problem may be people that turn around while inside the doorway, or if people pass through together (though at 80 cm wide that's a bit of a squeeze). Whether to account for this depends on how important those corner cases are to you.

That is what I looking for, IR Beams, do you have any article or tutorial that giude me to do it.

About the last case you mentioned, it is necessary for now.

Big thanks to you and Paul.

Check out adafruit, they have such sensors and all the info you need to go with it.

Yeah, I checked that out it has range of 50cm only and I want 90 cm more or less.

80 cm IR distance sensor
150 cm IR distance sensor

A distance sensor is not the right sensor.

Look for one with an LED on one side and a receiver on the other. That makes a beam sensor.

A sensor to detect the IR heat signature of warm bodies?

Paul

MorganS:
A distance sensor is not the right sensor.

Look for one with an LED on one side and a receiver on the other. That makes a beam sensor.

I got them today, and the seller said to me that you should set the duty cycle to 50% and set the frequency to 40kHz.

To be honest I don't know how :slight_smile:

Like those in the picture

2E3E7063-FF49-4AE2-95AF-1716BB5F652F-335-00000094D8E52279.png

Any help about how are those sensors ( transmitter and detector ) wired and programmed?

jdawe02:
I got them today, and the seller said to me that you should set the duty cycle to 50% and set the frequency to 40kHz.

To be honest I don't know how :slight_smile:

PWM.

jdawe02:
Any help about how are those sensors ( transmitter and detector ) wired and programmed?

From the picture best guess: Vcc to either 5V or 3.3V (check data sheet on what voltage you need); GND to GND; the DATA lines to a digital pin.

Receiver DATA will probably go HIGH when a beam is detected, LOW when it's broken (maybe it's opposite).

Sender DATA no idea what that's supposed to do. I don't see any driver transistor or candidate for this, so maybe you have to connect Vcc to your PWM pin instead.