Hi,
I am currently working on a project where I would like to mimic a surveillance camera using:
- PIR Shield
- OpenMV Cam H7 Plus
I can capture images without a problem, but I can’t seem to figure out how to use the PIR shield as a trigger.
I removed the jumper for Pin 11 and soldered Pin 9 to use as wakeup pin since the Cam H7 Plus has no Pin 11?
I can set the white LED’s and the IR LED’s on the shield, but I get no reading of the PIR sensor itself.
Here is the code I am using:
import sensor
import time
import image
import machine
import pyb
record_time = 2000 # 2 seconds in milliseconds
debounce_time = 200 # Minimum time between detections in milliseconds
required_detections = 2 # Minimum number of consecutive detections to confirm motion
stream_counter = 0
sensor.reset() # Reset and initialize the sensor.
sensor.set_pixformat(sensor.RGB565) # Set pixel format to RGB565 (or GRAYSCALE)
sensor.set_framesize(sensor.QVGA) # Set frame size to UXGA (1600x1200)
sensor.set_auto_gain(False)
sensor.set_framebuffers(3)
sensor.skip_frames(time=2000) # Wait for settings to take effect initially
pir_pin = pyb.Pin("P9", pyb.Pin.IN, pull=pyb.Pin.PULL_UP)
white_led_pin = pyb.Pin("P7", pyb.Pin.OUT_PP)
try:
# The camera will now focus on whatever is in front of it.
sensor.ioctl(sensor.IOCTL_TRIGGER_AUTO_FOCUS)
except:
raise (Exception("Auto focus is not supported by your sensor/board combination."))
led = machine.LED("LED_RED")
led.off()
# Declare the stream variable here so it's accessible throughout the loop
stream = None
last_detection_time = 0
motion_confirmed = False
detection_count = 0
while True:
# Check if the PIR sensor detects motion
if pir_pin.value() == 0:
# Check for debounce
current_time = time.ticks_ms()
if time.ticks_diff(current_time, last_detection_time) > debounce_time:
last_detection_time = current_time
# Confirm motion with multiple consecutive detections
while detection_count < required_detections:
detection_count += 1
# Only confirm motion if required detections are reached
if detection_count == required_detections:
print("motion confirmed")
white_led_pin.high()
motion_confirmed = True
detection_count = 0
else:
motion_confirmed = False
if motion_confirmed:
# Start new recording and reset timers upon confirming motion
if stream is None: # Only open a new stream if one isn't already open
stream_counter += 1
stream = image.ImageIO("/stream_{:04d}.bin".format(stream_counter), "w")
led.on()
# Set the start time for recording
start_time = time.ticks_ms()
# Continue recording while motion is confirmed and within record_time
while time.ticks_diff(time.ticks_ms(), start_time) < record_time:
img = sensor.snapshot()
stream.write(img)
# Calculate total recording time since the start of recording
elapsed_time = time.ticks_diff(time.ticks_ms(), start_time) / 1000 # Milliseconds to seconds
print("Recording time: {:.2f} seconds".format(elapsed_time))
# Close stream after recording time expires
stream.close()
stream = None # Reset stream to None so it can be reopened on the next motion
led.off()
motion_confirmed = False # Reset motion confirmation after recording
else:
print("no motion detected")
white_led_pin.low()
detection_count = 0
My questions are:
- Is my idea possible of using the PIR shield as a trigger?
- Is there a mistake in my code?
- Is there an easy way I could check if my PIR sensor works?
Kindest regards