Understanding motion

By now you must have figured that the PIR sensor is not the most idealistic sensor for us to switch the lights on or off. Mostly because, although the motion is one of the best indicators of presence, there can be times when you might not move at all, for example, while resting, reading a book, watching a movie, and so on.

What do we do now? Well, we can do a little trick. Remember in the last chapter we used our proximity sensor to sense whether a person has crossed a specific area or not? We will implant a similar logic here; but rather than just copy pasting the code, we will improve it and make it even better.

So rather than using one single IR proximity sensor, we will be using two of these things. The mounting will be as shown in the following diagram:

Now it is very evident that whenever a person walks in from the door side to the room side the Sensor 1 will show a lower reading when detecting a body. Then, while he is walking towards the room side, Sensor 2 will show a similar reading.

If first Sensor 1 is triggered and thereafter Sensor 2 is triggered, then we can safely assume that the person is travelling from the door side to the room side. Similarly, if the opposite is happening, then it is understood that the person is walking out of the room.

Now, this is fairly simple. But how do we implement it in a real-life situation? Firstly, we need to connect the circuit as follows:

Once that is done, upload the following code:

import GPIO library
import RPi.GPIO as GPIO
import time

import Adafruit_ADS1x15
adc0 = Adafruit_ADS1x15.ADS1115()

GAIN = 1
LIGHT = 23

adc0.start_adc(0, gain=GAIN)
adc1.start_adc(1, gain=GAIN)

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)


while True:


F_value = adc0.get_last_result()
F1 = (1.0 / (F_value / 13.15)) - 0.35

time.sleep(0.1)

F_value = adc0.get_last_result()
F2 = (1.0 / (F_value / 13.15)) - 0.35

F0_final = F1-F2

if F0 > 10 :

Time0 =
time.time()



F_value = adc1.get_last_result()
F1 = (1.0 / (F_value / 13.15)) - 0.35

time.sleep(0.1)

F_value = adc1.get_last_result()
F2 = (1.0 / (F_value / 13.15)) - 0.35

F1_final = F1-F2

if F1 > 10:

Time1 = time.time()


if Time1 > Time0:

GPIO.output(LIGHT, GPIO.HIGH)


if Time1 < Time0:

GPIO.output(LIGHT, GPIO.LOW) }

Now, let's see what are we doing here. As always, most of the syntax is very simple and straightforward. The most important part is the logic. So, let's understand in proper steps as to what we are doing.

   F_value = adc0.get_last_result()
F1 = (1.0 / (F_value / 13.15)) - 0.35

time.sleep(0.1)

F_value = adc0.get_last_result()
F2 = (1.0 / (F_value / 13.15)) - 0.35

In the preceding lines of code, we are taking the value of the IR proximity sensor and calculating the distance corresponding to it and storing that value in a variable called F1. Once that is done, we are stopping for a brief period of 0.1 seconds using the time.sleep(0.1) function. Thereafter, we are taking the reading from the same sensor again and storing the value in a variable called F2. Why are we doing this? We have already understood that in the previous chapters.

  F0_final = F1-F2

Once the value of F1 and F0 is acquired, we will calculate the difference to find out whether someone has passed through it or not. If no one has passed, then the reading will almost be the same and the difference will not be considerable. However, if a person does pass, then the reading will be considerable and that value will be stored in a variable called F0_final.

   if F0 > 10 :

Time0 =
time.time()

If the value of the F0 or the difference in distance between the first and the second reading is more than 10 centimeters, then the if condition will be true. Once true, it will set the value of the Time0 variable as the current value of time. The time.time() function will make a note of the exact time. 

   F_value = adc1.get_last_result()
F1 = (1.0 / (F_value / 13.15)) - 0.35

time.sleep(0.1)

F_value = adc1.get_last_result()
F2 = (1.0 / (F_value / 13.15)) - 0.35

F1_final = F1-F2

if F1 > 10:

Time1 = time.time()

Now, we'll perform the exact same step for Sensor 2 as well. There is nothing new to tell here; it's all self explanatory.

    if Time1 > Time0:

GPIO.output(LIGHT, GPIO.HIGH)

Once all of this is done, we compare if Time1 > Time0. Why are we comparing it? Because Time0 is the time noted for Sensor 1. If the person is moving inside, then Sensor 1 would be the first one to be triggered and then the Sensor 2 would be triggered. Hence, the time noted would be greater for Sensor 2 and relatively earlier for Sensor 1. If that happens, then we can assume that the person is coming inside. Well, if a person is coming inside, then we simply need to switch the light on, which is exactly what we are doing here. 

    if Time1 < Time0:

GPIO.output(LIGHT, GPIO.LOW)

Similarly, when a person is going out, the first sensor to be triggered would be Sensor 2, thereafter Sensor 1 will be triggered. Making the time noted for Time1 earlier than Time2; hence, whenever this condition is true, we will know that the person is moving out of the room and the lights can be switched off.

Go ahead and mount it near the door and see how it reacts. I'm sure this will be way better than what we had done through PIR. Have fun with it and try to find any flaws that it might have. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.153.38