I am trying to do the following: I connected a PIR-Sensor to my raspberry pi to detect motion. If there is motion detected my pi should play random videos from the folder "videos" (about 20-30 videos in this folder). If there is no motion detected a static image should be shown.

With the following code I am able to start the specified video only (on motion detected).

import RPi.GPIO as GPIO
import time
import os


while True:  
  if (GPIO.input(PIR)==True): 
    os.system("omxplayer -o hdmi videos/biene.mp4 -b > /dev/null") 
    while (GPIO.input(PIR) == True): time.sleep(.2);

Additionaly I would like to check every 30 seconds for motion, if there is motion detected keep playing the currently playing video and continue with random videos, if there is no motion detected stop playing videos and show the above mentioned specific image.

I am totally new to raspberry pi´s and python.

  • 1
    This seems to be a how do I program question. The Pi is pretty irrelevant. Perhaps work through some on-line tutorials and work through the raspberrypi.org site.
    – joan
    Dec 16, 2022 at 16:29
  • Ditto -- general programming questions belong on Stack Overflow. Python is extremely well documented online; you need to learn about flow control, which is a basic topic common to all programming.
    – goldilocks
    Dec 18, 2022 at 15:32


Browse other questions tagged or ask your own question.