Chase RudinDaniel Spicer
Published

3 Sensor People Counter

This project introduces a way to count the number of people entering a room using a motion sensor, IR sensor, and a button.

BeginnerFull instructions provided101
3 Sensor People Counter

Things used in this project

Hardware components

SparkFun Button
×1
Elegoo IR Emission
×1
Elegoo IR Receiver
×1
Elegoo HC-SR501 PIR Motion Sensor
×1
Argon
Particle Argon
×2
LED (generic)
LED (generic)
×2

Software apps and online services

Particle Build Web IDE
Particle Build Web IDE
ThingSpeak API
ThingSpeak API

Story

Read more

Code

IR Sensor and IR Emitter

C/C++
/* Pin diagram of IR Sensor and IR Emitter
 *
 *
 *              /----------------------------------------------------\
 *              |              +-----+                               |
 *              |   +----------| USB |---------+                     |
 *              |   | O        +-----+      O  |                     |
 *              |   | [ ] RESET                |                     |
 *              \---| [*] 3V3                  |                     |
 *                  | [ ] MODE                 |                     |
 *                  | [*] GND                  |                     |
 *                  | [ ] A0          Lipo [ ] |                     | 
 *                  | [ ] A1  +-------+ EN [ ] |                     |
 *                  | [ ] A2  |   *   | VIN[ ] |                     |
 *                  | [ ] A3  | ARGON | D8 [ ] |                     |
 *                  | [ ] A4  |       | D7 [*] |----LED---GND        |
 *                  | [ ] A5  +-------+ D6 [ ] |                     |
 *                  | [ ] D13           D5 [*] |--------IR Sensor----/
 *                  | [ ] D12           D4 [ ] |  |
 *                  | [ ] D11           D3 [ ] | GND
 *                  | [ ] D10           D2 [ ] |
 *                  | [ ] D9            D1 [ ] |                
 *                  | [ ] NC            D0 [ ] |
 *                  |   WIFI0         0NFC     | 
 *                  |O                        O|  
 *                  +--------------------------+ 
 *                                                        
 *      
 */
/*
    This program houses all the processes involving the IR sensor.
    It has a running total of the IR triggers since the program was started, which it sends to Thingspeak every 30 seconds.
    It receives the value of the motion sensor from the other device, and if th IR sensor and the Motion sensor are triggered
    at the same time, it publishes an event that updates the people counter on the other device.
*/

int input;              // initialize input as an int
int ms_val = 0;         // initialize ms_val as an int and set to 0
int IR_count = 0;       // initialize IR_count as an int and set to 0

void setup() {

  pinMode(D5, INPUT);                                                   // set IR sensor pin as an input
  pinMode(D7, OUTPUT);
  Particle.subscribe("Motion Sensor Value", msfunc, MY_DEVICES);        // subscribe to "Motion Sensor Value" event from the other device
}

int msfunc (const char *event, const char *data) {
    if (strcmp(String(data), "HIGH") == 0) {            // check if the motion sensors is HIGH based on the event published from the other device
        ms_val = HIGH;                                  // set the motion sensor value as HIGH
    }
    else {
        ms_val = LOW;                                   // set the motion sensor value as LOW
    }
    
    return ms_val;                                      // return the motion sensor value
}

void loop() {
    
    input = digitalRead(D5);                            // read the pin the IR sensor is plugged into
    if (input == HIGH)                                  // check if the IR sensor was triggered
    {
        IR_count = IR_count + 1;                        // update total IR sensor trigger count
        
        if (ms_val == HIGH && input == HIGH) {          // check if the IR sensor and the motion sensor are both triggered
            Particle.publish("Detect", "True");         // Publish that a person was detected. This will update the total people count on the other device
            digitalWrite(D7, HIGH);
        }
        
        delay(500);
    }
    
    else {
        digitalWrite(D7, LOW);
        
        delay(500);                                     // delay 500 milliseconds
    }
    
    if (Time.second() == 30) {                                                                                  // triggers every minute when the real world time is at 30 seconds
        Particle.publish( "Counter", "{ \"IR Triggers\": \"" + String(IR_count) + "\"}", 60, PRIVATE);          // Publish the number of IR Triggers to Thingspeak
    }
    
delay(1000);
}

Button and Motion Sensor

C/C++
/* Pin diagram of motion sensor and button
 *
 *
 *  /-------------------------------------------------------------------\
 *  |              +-----+                                              |
 *  |   +----------| USB |---------+                                    |
 *  |   | O        +-----+      O  |                                    |
 *  |   | [ ] RESET                |                                    |
 *  \---| [*] 3V3                  |                                    |
 *      | [ ] MODE                 |                                    |
 *      | [*] GND                  |                                    |
 *      | [ ] A0          Lipo [ ] |                                    | 
 *      | [ ] A1  +-------+ EN [ ] |                                    |
 *      | [ ] A2  |   *   | VIN[ ] |                                    |
 *      | [ ] A3  | ARGON | D8 [ ] |                                    |
 *      | [ ] A4  |       | D7 [ ] |            GND                     |
 *      | [ ] A5  +-------+ D6 [ ] |            |                       |
 *      | [ ] D13           D5 [ ] |            1k Resistor             |
 *      | [ ] D12           D4 [ ] |            |                       |
 *      | [ ] D11           D3 [*] |------------|-----Motion Sensor-----|
 *      | [ ] D10           D2 [*] |----------------|--Button-----------/
 *      | [ ] D9            D1 [ ] |                |
 *      | [ ] NC            D0 [*] |----|---LED     |
 *      |   WIFI0         0NFC     |    |           1k Resistor
 *      |O                        O|    |           |
 *      +--------------------------+    |           |
 *                                     GND         GND
 *      
 */


int led = 0;                // the pin that the LED is atteched to
int ms_sensor = 3;          // the pin that the motion sensor is atteched to
int button = 2;             // the pin that the button is a attached to
int state = LOW;            // by default, no motion detected
int ms_val = 0;             // variable to store the motion sensor status (value)
int ms_state = 0;
int button_val = 0;         // variable to store the button sensor status (value)
int IR_val = 0;             // variable to store the IR sensor status (value)
int IR_state = 0;
int count = 0;              // variable to store total people counted since device was turned on or reset
int button_presses = 0;
int IR_triggers = 0;

void setup() {
  pinMode(led, OUTPUT);         // initalize LED as an output
  pinMode(ms_sensor, INPUT);    // initialize motion sensor as an input
  pinMode(button, INPUT);       // initialize button as an input
  digitalWrite(led, LOW);       // set the LED as off
  Particle.subscribe("Detect", IR_func, MY_DEVICES);
}

int IR_func(const char *event, const char *data) {
    if (data) {                 // check if any data was sent
        count = count + 1;          // set IR_val to HIGH
    }
    
    return count;              // return IR_val so it carries over to the rest of the code
}


void loop(){
  ms_val = digitalRead(ms_sensor);      // read motion sensor value
  button_val = digitalRead(button);     // read button value
  
  if (ms_val == HIGH) {
      if (ms_state == LOW) {
          Particle.publish("Motion Sensor Value", "HIGH");
          ms_state = HIGH;
      }
  }
  else {
      if (ms_state == HIGH) {
          Particle.publish("Motion Sensor Value", "LOW");
          ms_state = LOW;
      }
  }

  if (button_val == HIGH) {
        button_presses = button_presses + 1;                            // running total of total button presses

        if (button_val == HIGH && ms_val == HIGH) {                     // check if button is pressed and motion sensor is activated
            count = count + 1;
            digitalWrite(led, HIGH);                                    // turn LED on
        
            if (state == LOW) {                                         // check if state is LOW
                state = HIGH;                                           // check if state is HIGH
            }
        }
        
        delay(500);                                                     // delay 500 milliseconds
    }
  /*
  if (IR_val == HIGH) {
        IR_triggers = IR_triggers + 1;                              // running total of total IR sensor
  
        if (IR_val == HIGH && ms_val == HIGH) {                     // check if IR and motion sensors are activated
            count = count + 1;
            digitalWrite(led, HIGH);                                // turn LED on

            delay(500);                                             // delay 500 milliseconds
        
            if (state == LOW) {                                     // check if state is LOW
                state == HIGH;                                      // update variable state to HIGH
            }
        }
        
        IR_val = LOW;                                               // set IR val to low
    }
  */
  
  else {
      digitalWrite(led, LOW);                       // turn LED OFF
      delay(500);                                   // delay 500 milliseconds 
      
      if (state == HIGH){                           // check if state is high
        state = LOW;                                // update variable state to LOW
    }
  }
  
  
  
  
  if (Time.second() == 0) {
       Particle.publish( "Counter", "{ \"People Counted\": \"" + String(count) + "\","+              //Publishes people counted, button presses, and IR sensor triggers to Thingspeak
       "\"Button Presses\": \"" + String(button_presses) + "\"}", 60, PRIVATE);
  }
  
}

Credits

Chase Rudin

Chase Rudin

1 project • 1 follower
Daniel Spicer

Daniel Spicer

1 project • 1 follower

Comments

Add projectSign up / Login