I'm running a raspberry pi system that records fish as they swim passed an IR break beam sensor at a fish ladder. So far the system works well but I'm trying to cut down some of the false triggers. There aren't many but I believe that by making it so that video will only begin recording after the beam has been broken for 1-2 seconds instead of starting instantaneously upon the beam breaking it will help to trim them down. Here is my code:

# Imports
from picamera import PiCamera
import time
import RPi.GPIO as GPIO
import datetime 
import logging

# Variables
camera = PiCamera()
BEAM_PIN = 4                                # Pin we are using to read the IR break beam switch
log_time = datetime.datetime.now().strftime('%m%d%y')
#create logger
logger = logging.getLogger('myapp')
logger.setLevel(logging.DEBUG) # log all escalated at and above DEBUG

# Setup the GPIO
GPIO.setmode(GPIO.BCM)                 # GPIO layout mode      
GPIO.setup(BEAM_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) #Setup the gpio pin we are reading from as a pullup input

# Loop checking the switch
while True:
    # Read the switch value
    input = GPIO.input(BEAM_PIN)

    # If the GPIO reading goes from high to low, record for 30 secs
    if input != 1:
        #define time format for filenames and log output
        now = time.localtime(time.time())      #Variable plugged into asci time to allow for readable date print out 
        timestamp = datetime.datetime.now().strftime("%m%d%y_%H%M%S") #Variable to update name of video files with current date and time
        #add a file handler
        hdlr = logging.FileHandler('/media/pi/F8AF-129D/log_files/{}.log'.format(log_time))
        # create a formatter and set the formatter for the handler
        formatter = logging.Formatter('%(asctime)s %(message)s')
        #add the handler to the logger
        logger.info('Lamprey Detected!')
        camera.start_recording('/media/pi/F8AF-129D/videos/{}.h264'.format(timestamp)) #Recording video file to Lexar thumb drive
        time.sleep(0.05) #Debounce wait

I'm having a hard time finding documentation about how to implement this change into the code. I get that I need to alter the if statement in the loop but I'm not sure of the proper syntax to get where I need. Any help would be appreciated.


you've probably already tried this, but my first instinct is just to add a sleep time before the camera starts i.e:

time.sleep(2) #Camera delay, change 2 to whatever you want the delay to be
  • That won't work. Merely sleeping for a second after the instant of beam break doesn't check that the beam has been broken continuously for a second. – Chenmunka Oct 19 '20 at 13:02
  • OK I see, so you need to do a count of how long the beam break is, then if it meets the criteria, start recording? – OldGold Oct 19 '20 at 13:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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