1

I'm having trouble programming the logic of 2 PIR sensors to print a message in console whenever a user wave their hands on the PIR sensors. I have been able to make 1 of the PIR sensors work which is GPIO 4 but having trouble getting both of them to activate when I wave my hand across the PIR sensors. What I did was duplicate the codes.

.I have managed to successfully attach the PIR sensors to the raspberry pi using GPIO,GND and 5v port and I am reading the sensors from GIPO 4 and 1. enter image description here

The program is pretty simple. The Raspberry Pi GPIO pins to allow us to use pin 4 as an input; it can then detect when the PIR module sends power. The pin continually check for any changes, uses a while True loop for this. This is an infinite loop so the program will run continuously unless we stop it manually with Ctrl + C.

import RPi.GPIO as GPIO
import time

sensor = 4
sensor2 = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(sensor, GPIO.IN, GPIO.PUD_DOWN)
GPIO.setup(sensor2, GPIO.IN, GPIO.PUD_DOWN)

previous_state = False
current_state = False

previous_state2 = False
current_state2 = False

while True:
    time.sleep(0.1)
    previous_state = current_state
    current_state = GPIO.input(sensor)
    if current_state(TRUE) != previous_state(FALSE):
        new_state = "HIGH" if current_state else "LOW"
        print("GPIO pin %s is %s" % (sensor, new_state))

while True:
    time.sleep(0.1)
    previous_state2 = current_state2
    current_state2 = GPIO.input(sensor2)
    if current_state2(TRUE) != previous_state2(FALSE):
        new_state2 = "HIGH" if current_state2 else "LOW"
        print("GPIO pin %s is %s" % (sensor2, new_state2))

I have been able to make 1 PIR works but it's not reading input from GIPO 17. I should receive some message depicted below.

GPIO pin 17 is high

1 Answer 1

3

Delete the second while True: line.

Currently that line is not reached as the previous while True: never completes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.