Advanced search FAQ Register Login

It is currently Sat Jan 20, 2018 5:54 pm

 

Postby JJG » Fri Dec 04, 2015 8:14 am

Hi,

I've connected two sensors (Motion & Sound) to my raspberry pi.
On their own, the sensors can succesfully detect sound/motion.

But I'm trying to get one of these sensors to activate another sensor.
e.g. If the motion is detected, the sensor will be activated or vice versa.

Is there anyway to do this/ python code?

JJG
n00b

Posts: 2

Joined: Fri Dec 04, 2015 8:11 am

Postby Jake » Mon Dec 07, 2015 4:32 pm

Hi,

Yes, it's very do-able.

Could you send me or link to the code you're currently using?

I would suggest that a simple "if" statement would suffice:

http://learnpythonthehardway.org/book/ex30.html

e.g. if motion is detected, turn on sound sensor

-- Jake

Jake
ModMyPi

User avatar

Posts: 1396

Joined: Thu Aug 27, 2015 3:57 pm

Forum Administrator & Technical Support

Postby JJG » Tue Dec 08, 2015 9:47 am

Hi,

Here are the codes. I've gotten them from other forums:

For Motion:

Code: Select all

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
PIR_PIN = 7
GPIO.setup(PIR_PIN, GPIO.IN)
print “PIR Module Test (CTRL+C to exit)”
time.sleep(2)
print “Ready”
while True:
               if GPIO.input(PIR_PIN):
                              print “Motion Detected!”
               time.sleep(1)


For the Sound sensor (it detects range):

Code: Select all

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
TRIG = 23
ECHO = 24
print "Distance Measurement In Progress"
GPIO.setup(TRIG,GPIO.OUT)
GPIO.setup(ECHO,GPIO.IN)
GPIO.output(TRIG, False)
print "Waiting For Sensor To Settle"
time.sleep(2)
GPIO.output(TRIG, True)
time.sleep(0.00001)
GPIO.output(TRIG, False)
while GPIO.input(ECHO)==0:
pulse_start = time.time()
while GPIO.input(ECHO)==1:
pulse_end = time.time()   
pulse_duration = pulse_end - pulse_start
distance = pulse_duration x 17150
distance = round(distance, 2)
print "Distance:",distance,"cm"
GPIO.cleanup()

JJG
n00b

Posts: 2

Joined: Fri Dec 04, 2015 8:11 am

Postby Jake » Thu Dec 10, 2015 9:51 am

Hi,

OK, so like this:

Code: Select all


import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)

PIR_PIN = 7
TRIG = 23
ECHO = 24

GPIO.setup(PIR_PIN, GPIO.IN)
GPIO.setup(TRIG,GPIO.OUT)
GPIO.setup(ECHO,GPIO.IN)

GPIO.output(TRIG, False)

print “PIR Module Test (CTRL+C to exit)”

time.sleep(2)
print “Ready”

while True:
               if GPIO.input(PIR_PIN):
                        print “Motion Detected!”
            time.sleep(2)
            print "Distance Measurement In Progress"
            time.sleep(1)
            print "Waiting For Sensor To Settle"
            time.sleep(2)
            GPIO.output(TRIG, True)
            time.sleep(0.00001)
            GPIO.output(TRIG, False)
            while GPIO.input(ECHO)==0:
            pulse_start = time.time()
            while GPIO.input(ECHO)==1:
            pulse_end = time.time()   
            pulse_duration = pulse_end - pulse_start
            distance = pulse_duration x 17150
            distance = round(distance, 2)
            print "Distance:",distance,"cm"
            GPIO.cleanup()
         time.sleep(1)
    time.sleep(1)



I've just cobbled what you had together, you'll need to check spacing etc.

Jake
ModMyPi

User avatar

Posts: 1396

Joined: Thu Aug 27, 2015 3:57 pm

Forum Administrator & Technical Support

Who is online

Users browsing this forum: No registered users and 2 guests

Board index

The teamDelete all board cookies • All times are UTC [ DST ]