I am trying to make a python program that uses GPIO buttons to play videos on a Raspberry pi.

I do not really understand Python and I need some help with the code to make the videos play.

I have tried to adapt the code from this source https://www.instructables.com/Play-Video-With-Python-and-GPIO/ to accept more button inputs.

The code I have used is:

import RPi.GPIO as GPIO

import os

import sys

from subprocess import Popen


GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)

GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)

GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_UP)

GPIO.setup(14, GPIO.IN, pull_up_down=GPIO.PUD_UP)

GPIO.setup(5, GPIO.IN, pull_up_down=GPIO.PUD_UP)

GPIO.setup(11, GPIO.IN, pull_up_down=GPIO.PUD_UP)

GPIO.setup(7, GPIO.IN, pull_up_down=GPIO.PUD_UP)

GPIO.setup(8, GPIO.IN, pull_up_down=GPIO.PUD_UP)

GPIO.setup(19, GPIO.IN, pull_up_down=GPIO.PUD_UP)

GPIO.setup(3, GPIO.IN, pull_up_down=GPIO.PUD_UP)

GPIO.setup(2, GPIO.IN, pull_up_down=GPIO.PUD_UP)

GPIO.setup(26, GPIO.IN, pull_up_down=GPIO.PUD_UP)

hint1 = ("/home/pi/Videos/P1H1.mp4")

repeats 2-12

last_state1 = True

repeats 2-12

input_state1 = True

repeats 2-12

player = False

While True:

    input_state1 = GPIO.input(8)

repeats 1-12

    if input_state1 != last_state1:

        if (player and not input_state1):

            os.system('killall omxplayer.bin')

            omxc = Popen(['omxplayer', '-b', hint1])

            player = True

        elif not input_state1:

            omxc = Popen(['omxplayer', '-b', hint1])

            player = True

repeats 2-12 but if is instead elif

elif (player and input_state1 and input_state2 repeats 3-12

    os.system('killall omxplayer.bin')

    player = False

last_state1 = input_state1

repeats 2-12

To save time and space, I have used repeats. To save any confusion, repeats are the same as above but with the numbers changed to the repeats.

The results of this vary by button. Some play the indicated video, some will play the video when the button is held down, and most do nothing.

What I am trying to accomplish overall is to have 24 buttons that each play their own videos when pressed but not held in. Ideally, the videos will loop once the video finishes, and pressing another button switches to that video.

  • 1
    If you want any help you need to post the code you are using. I would't even bother with a 4 year old tutorial for an obsolete OS.
    – Milliways
    Mar 3, 2021 at 5:48

1 Answer 1


I think the issue might be that with so many buttons, using the GPIO.input() function may result in the button press events being missed while other parts of the code are being executed.

I suggest using event detection for your button inputs. About halfway down this webpage is the event_detected() function heading which explains the usage. Also check out the Switch debounce heading close to the bottom of the same webpage, you may need to debounce the switches to get them to work properly.

Good luck!

Your Answer

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

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