'Python code for multiple PIR motion sensors on Raspberry Pi
Connecting up to three 5v motion sensors on the raspberry pi for a project and I'm pretty new to python. I've successfully coded one motion sensor which lights up an LED and makes a buzzer sound when motion detected. How would I code multiple sensors that then light up different LEDs?
# Motion detected with buzzer and LED
import RPi.GPIO as GPIO
import time
GPIO.setwarnings(False)
#Refer pins by their sequence number on the board
GPIO.setmode(GPIO.BCM)
#Read output from PIR motion sensor
GPIO.setup(18, GPIO.IN)
#LED output pin
GPIO.setup(3, GPIO.OUT)
while True:
inp = GPIO.input(18)
#When output from motion sensor is HIGH
if inp == 1:
print("Motion detected!!")
GPIO.output(3, 1) #Turn on LED & Buzzer
time.sleep(1)
#When output from motion sensor in LOW
elif inp == 0:
print("No motion, all okay.")
GPIO.output(3, 0) #Turn off LED & Buzzer
time.sleep(1)
time.sleep(0.1)
Solution 1:[1]
If anyone in the future is looking to use a pair of PIR motion sensors on a raspberry 4, this code above helped me figured it out. The code below is how i used it.
motion_1 = GPIO.add_event_detect(pirPin, GPIO.RISING, callback=LIGHTS)
motion_2 = GPIO.add_event_detect(pirPin2, GPIO.RISING, callback=LIGHTS2)
try:
while True:
if motion_1 == 1:
motion_1
print('Test for BTC') # print in command line , not LCD
time.sleep(1)
elif motion_2 == 1:
motion_2
print('Test for eth') # print in command line, not LCD
time.sleep(1)
except KeyboardInterrupt:
print('Quit')
GPIO.cleanup()
## the lights function being called above, pulls data from binance API, and prints out the price to an LCD screen, so i can trigger a PIR motion on the left and it returns BTC, i can trigger PIR motion on the right and it returns ETH prices. Kind of fun to build.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
