# logic behind programming two PIR sensors as a bidirectional people counter

I'm not looking for code, but an explanation how a two sensor setup works. If a person goes through the "lane", that person passes through sensor one and two, so assuming sensor one is +1 in variable person and sensor two is -1 in variable then wouldn't the counter be stuck in zero?

Here is the idea but using arduino:

https://circuitdigest.com/microcontroller-projects/automatic-room-light-controller-with-bidirectional-visitor-counter-using-arduino?page=1

Thanks for taking the time.

I think the logic behind using 2 sensor is to be able to tell whetehr the person is coming into the room or out of the room, or lane or whatever.

So from the scenario of your question, if a person goes through the "lane", that person passes through sensor one and two. The order in which these sensors detect the person, will determine the direction of movement.

For example if sensor one goes off first, then sensor two goes off, the person would be travelling in a certain direction (let's say, entering the room). If instead sensor two goes off first, then sensor one, the person must be travelling in the opposite direction (this time leaving the room).

So your +1 and -1 are not directly linked to each sensor, but instead are linked to the order of detection of both sensors.

Hope this helps.

I've understood your logic and have read up on how to go about the problem. The closest I've seen and tried to make the code is similar to this code made from https://stackoverflow.com/questions/31352468/two-sensors-is-it-possible-to-pause-a-sensor-for-a-shohttps://stackoverflow.com/questions/31352468/two-sensors-is-it-possible-to-pause-a-sensor-for-a-short-time

``````#!/usr/bin/python3.4

from ubidots import ApiClient

import RPi.GPIO as GPIO
import time
import datetime
import os
import web

person_count = 0
ts = time.time()
st = datetime.datetime.fromtimestamp(ts).strftime('%m-%d-%Y  %I:%M:%S')

GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.IN) #IR Sensor closest to Door
GPIO.setup(25, GPIO.IN) #IR Sensor Farthest from Door
GPIO.setup(17, GPIO.OUT) #LED to indicate when data is being recorded to the
web.
api = ApiClient("165c54954ee6d8f7324366495cab70217c7c3c91")
test_variable = api.get_variable("559c18cc76254214be90fe05")

while True:
# If someone leaves, wait for one second.
if GPIO.input(4) == 0 and GPIO.input(25) == 1:
GPIO.output(17, False)
time.sleep(2)

# if both sensors are blocked, wait 1 second until clear
elif GPIO.input(4) == 1 and GPIO.input(25) == 1:
GPIO.output(17, False)
time.sleep(2)

# If neither sensor is blocked, wait 0.1 seconds.
elif GPIO.input(4) == 0 and GPIO.input(25) == 0:
GPIO.output(17, False)
time.sleep(0.1)

# If the first sensor (4) is blocked, send person count to Ubidots prog\$
elif GPIO.input(4) == 1 and GPIO.input(25) == 0:
GPIO.output(17, True)
person_count += 1
test_value = person_count
test_variable.save_value({'value':test_value})
time.sleep(1.5)
``````

This, program's function is to send a +plus one to an online file whenever someone comes in the room. I understand the logic of the program but it is malfunctioning. I've also read up on the recommendations of the comments below and am exploring the notion of global variables. Please advise if I am in the right direction.

Thanks for the feedback.

I have finished making my 1st version of the program. This project will be assembled later this month, meaning i haven't the hardware yet. I read up on global variables and functions and came up with this:

``````import time
import RPI.GPIO as GPIO

GPIO.setmode(GPIO.BCM)

sensor = 4
sensor2 = 7
LED = 26

GPIO.setup(sensor, GPIO.IN, GPIO.PUD_DOWN)
GPIO.setup(sensor2, GPIO.IN, GPIO.PUD_DOWN)
GPIO.setup(LED, GPIO.OUT,)

def display():
global x
x = 0
while True:
print ("Number of People: %d" % (x))
time.sleep(.5)

def Sit1():
while True:
# If someone leaves, wait for one second.
if GPIO.input(4) == 0 and GPIO.input(25) == 1:
GPIO.output(17, False)
time.sleep(2)
x-=1
elif x <=0:
pass

def Sit2():
# if both sensors are blocked, wait 1 second until clear
if  GPIO.input(4) == 1 and GPIO.input(25) == 1:
GPIO.output(17, False)
time.sleep(2)

def Sit3():
# If neither sensor is blocked, wait 0.1 seconds.
if  GPIO.input(4) == 0 and GPIO.input(25) == 0:
GPIO.output(17, False)
time.sleep(0.1)

def Sit4():
# If the first sensor (4) is blocked, increment the variable x +1
if  GPIO.input(4) == 1 and GPIO.input(25) == 0:
GPIO.output(17, True)
x += 1

def run():
while True:
display()
Sit1()
Sit2()
Sit3()

run()
``````

My professor hasn't had the chance to see this. Thoughts?