Hello again. Today I am going to show an ongoing project I am working on. My company has bestowed upon me the pleasure to build a demo kit for one of our product lines. I work in the low voltage industry that includes fire alarm, security, video surveillance and structured cable (voice and data). While designing the demo kit, it was brought to my attention that there are many situations that we are unable to simulate at trade shows or at a customer site, especially when we are talking about releasing chemical agent or setting off sirens.

Anyways, a coworker came up to me and said that it would be cool if we could have video play when an alarm occurs on a panel. Immediately, I thought of a Raspberry Pi. Virtually all security and fire alarm panels are nothing more than microcontrollers on steroids, thus have no video capabilities. They do have IO though....lots and lots of IOs.

Step 1: BOM

Here is the bill of material you will need for this project:


  • Raspberry Pi (I used a Model B)
  • SD card or micro SD card with adapter (I used a class 10 16gb micro SD card with adapter)
  • Micro USB power cable (700ma minimum)
  • Keyboard and mouse
  • GPIO ribbon cable and cobbler
    • you can use female to male jumpers if you have them
  • Breadboard
  • Jumper wires
  • HDMI cable
  • TV
  • Computer
  • SD card reader
  • Internet connection (i used a usb wifi adapter but a hardwired connection is always better)


  • Raspbian Jessie (my version is from 11/21/2015)
  • omxplayer (built in to Raspbian)
  • Python 3 (built in to Raspbian)

Step 2: Setting Up the Pi


First things first, we need to set up the SD card with the OS for the Raspberry Pi.

On your computer, go to https://www.raspberrypi.org/downloads/raspbian/ to download Raspbian.

Once you have the file downloaded, unzip the file.

Follow the instructions Here based on your type of computer to install Raspbian onto your SD card.

I have followed the instructions for Windows and Mac and they have all worked great. I will try the Linux version when my Linux boxes are freed up from their duties.


Now, insert your SD card into your Pi and power it up. Make sure you have your keyboard, mouse, TV and internet connection all plugged in as well.


Go to the Menu dropdown

Menu --> Preferences --> Raspberry Pi Configuration

  • Under the System tab, click the Expand Filesystem button
  • Under the Performance tab, you can overclock your Pi if you want to. I have mine selected to Modest(800MHz). Be CAREFUL when selecting the overclock speed. If you go too much without heatsinks or some form of cooling, you can damage your Raspberry Pi. You have been warned.
  • Under the Localisation tab, set your Locale, Timezone and Keyboard layout to match where you live.

Click OK and reboot your Pi.

After your Pi has rebooted, open the Terminal.:

Enter "sudo apt-get update"

When that is complete, enter "sudo apt-get upgrade"

If there are any upgrades needed, you will be prompted. Press "y" to install them. Reboot your Pi after that.

Now your Pi is set up and configured for what we need. On to the next task.

Step 3: Videos and Python

Before we can start to code, we need our material. To do this, you need some video files. As I am not a video editor in any sense of the word, I am leaving the creation of the required videos for the end-goal project to someone else in my company. I used .mp4 files as they are virtually universally played.

To test my code functionality, I transferred some music videos onto my Pi with a USB flash drive and saved them to the Videos folder.

There are other ways to transfer files to your Pi. One method is FTP. I did not use that method, but there are many good tutorials on it on this site and google.

Once you have your video files on your Pi, it is time to get down to coding.

Go to Menu --> Programming --> Python 3 (IDLE)

In Python 3, Go to File -- New File

Save that file as "videoplayer.py"

Now for the code:

import the needed libraries

import RPi.GPIO as GPIO
import os import sys from subprocess import Popen

Set GPIO pin format


Setup the GPIO buttons

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

Setup movie destination variable

movie1 = ("/home/pi/Videos/movie1.mp4")
movie2 = ("/home/pi/Videos/movie2.mp4")

Make boolean variables

last_state1 = True
last_state2 = True

input_state1 = True
input_state2 = True

quit_video = True
player = False

Now to put it all to work

while True:
	#Read states of inputs
	input_state1 = GPIO.input(17)
	input_state2 = GPIO.input(18)
	quite_video = GPIO.input(24)

	#If GPIO(17) is shorted to ground
	if input_state1 != last_state1:
		if (player and not input_state1):
			os.system('killall omxplayer.bin')
			omxc = Popen(['omxplayer', '-b', movie1])
			player = True
		elif not input_state1:
			omxc = Popen(['omxplayer', '-b', movie1])
			player = True
	#If GPIO(18) is shorted to ground
	elif input_state2 != last_state2:
		if (player and not input_state2):
			os.system('killall omxplayer.bin')
			omxc = Popen(['omxplayer', '-b', movie2])
			player = True
		elif not input_state2:
			omxc = Popen(['omxplayer', '-b', movie2])
			player = True

	#If omxplayer is running and GPIO(17) and GPIO(18) are NOT shorted to ground
	elif (player and input_state1 and input_state2):
		os.system('killall omxplayer.bin')
		player = False

	#GPIO(24) to close omxplayer manually - used during debug
	if quit_video == False:
		os.system('killall omxplayer.bin')
		player = False

	#Set last_input states
	last_state1 = input_state1
	last_state2 = input_state2

Now you should be able to run videoplayer.py and start triggering your videos via your GPIOs.

Step 4: And....Action

It is time to put our code to use. If you want to run it through IDLE, press F5.

You can also run through the terminals by entering:

pi@raspberrypi:~$ python3 videoplayer.py

Now it it time for the test.

Start shorting out your GPIOs and watch the videos play!

How it works

When you short out GPIO 17, the appropriate video will play. If you short out the GPIO 18 while GPIO 17's movie is playing, the video will stop and start playing GPIO 18's video. If all GPIOs are not shorted, omxplayer will close.

Step 5: What's Next

Now that this part if done, the next step is to have this script run at startup.

After that, who knows? I just got this crazy idea to utilize a a windows box that will be connected to the TV/Monitor to host the videos and send the signals from the Pi via wifi. Who knows? Maybe another 'ible?

Thank you for viewing my instructable. If you like it, please vote for me.

<p>ThothLoki,</p><p>This is a great Instructable, it really helped me! I have a few notes for you to correct (if I understand properly). I'm sure the downloadable python file is correct, but above, I typed it in by hand to learn more, and I note that two things are off, if you don't mind me saying so.</p><p>1) The import of RPi.GPIO is incorrectly listed at Rpi.GPIO. Python is case-sensitive.</p><p>2) The line</p><p>player = False</p><p>is missing from your boolean variables section above</p><p>I do have a question while I'm here - Because my project will show to &quot;guests&quot;, when they push the button to start the video (I have it set to momentary only), I don't want them to see the desktop anytime. So how can I make the screen BLACK before the video plays and after the video plays? Any idea?</p><p>Thanks!</p><p>Jeff</p><p>P.S. I am new to Instructables, I don't see how to &quot;vote&quot; for you. What can I do that will help you??? </p>
<p>Thank you DrychronRed for the corrections. I have edited the ible to reflect the .py file.</p><p>As for the black between videos, I am still at a loss on how to do that with OMXplayer. You could remove all of the icons from the desktop and set the background as black, but that is just a bandaid.</p><p>Honestly, to do something like seamless video or black screen afterwards, I think you will need to use something other than OMXplayer. You may have better luck with VLC. I have been out of the game on my Pis for a while, so I don't know of all of the updates. Plus this project got scrapped about two months after the ible posted, so I did not pursue it much after that.</p>
<p>hi, how to go the audio in the video?</p><p>omxc = Popen(['omxplayer', '-b', movie2])<br></p><p>i tried in this line, but nothing :/</p>
<p>What are you using for the video output? HDMI or RCA?</p><p>please look at this:</p><p><a href="https://www.raspberrypi.org/documentation/configuration/audio-config.md" rel="nofollow">https://www.raspberrypi.org/documentation/configur...</a></p>
<p>hi!</p><p>im using -o local</p><p>(3.5 jack)</p>
<p>Sorry about the short replies earlier, I was on my phone. I am nowhere near a Pi right now, but you may want to try this:</p><p>omxc = Popen(['omxplayer', '-b -o local', movie2])</p><p>I am not sure it will work. I had the same issue when building this project, but was always using the hdmi audio so the default worked for me. Give it a try and let me know.</p>
<p>Thanks ThothLoki, I already have audio but hdmi which is good enough.</p><p>Is it possible to have a looped video, and that it is interrupted to the short GPIO17, and when it finishes that it follows the previous video in loop?</p><p>regards</p>
I am sure you can with --loop<br>Just set up 17 as a omxplayer kill button. <br><br>Google python omxplayer loop and you should get your answer. <br><br>Sorry I can't be much more help ron guy now. My Pi was turned into an Echo for another project
<p>very thansk, i try tonight when see the Pi!<br><br>regrads</p>
<p>When i run</p><p>Omxplayer -or local video1.mp4 sounds all good.</p><p>But with the GPIO and videplayer.py I can not get sound in the same video</p>
<p>Hello i have a few Questions Sense I'm still new to raspberry pi. Oh by the way awesome Tut you have here. Is it possible To run a video file on start up automatically? Second Question is the videplayer.py file where do i put that at? The other question is I would like to use a remote control they is used for a TV is it possible to set one up so i can say press Channel 1 and it plays the first file. and so forth and also if i hit the stop button it goes back to playing that first file that booted up?</p>
<p>Thanks josephchrzempiec! Sorry about the late reply. I hope you are enjoying the raspberry pi world as much as I am. Now, on to your questions.</p><p>Yes, you can get this to run at startup. I haven't set it up on mine that way, but google how to set up cron jobs.</p><p>You can put the python file anywhere you can find it. You will just need to know the path to run it from the terminal or cron</p><p>I do not see why you couldn't set up a remote to function that way. You just need to set up the hardware and assign to the correct GPIOs (some script editing will be required) All of that sounds like IF/THEN statements to me.</p><p>Have a look at this site as well. It looks similar to mine but a lot cleaner.</p><p><a href="http://raspberrypi.stackexchange.com/questions/47738/omxplayer-wrapper-run-separated-files-in-specific-sequence" rel="nofollow">http://raspberrypi.stackexchange.com/questions/477...</a></p>
<p>hi</p><p>i have connected 3 laser trigger to arduino and connected arduiono to pi using usb. whenever the laser is blocked the arduino sends char to pi serially. fro 1st laser the pi receives char a, for 2nd b and 3rd laser c. so can i play a video for each laser trigger ?</p>
<p>That should not be a problem. Assuming you have the code to read the arduino in the python code (google &quot;rpi arduino serial communication python&quot; if you haven't), all you should have to do is change the IF statements in my code.</p><p> if received_char == &quot;a&quot;: </p><p>etc.....</p><p>Now, it depends on what you want to do. If you plan to just kill the playing video and play another, you just need to make sure what happens in that IF statement matches.</p><p>Give it a shot and report back. Honestly, I have never played with arduino and Rpi serially. I am more of a wireless communication guy between those two boards. If you get it working, want to post the code here? I would love to see it. If you are still having troubles, post the code and I will try to help out.</p>
<p>thank u sir,</p><p>but nothing is happening.even the print line is not showing. can you help me out with it.</p><p>this is the python script:</p><p>import sys</p><p>import os</p><p>from subprocess import Popen</p><p>import serial</p><p>movie1 = (&quot;/home/pi/sss5/vid1.mp4&quot;)</p><p>movie2 = (&quot;/home/pi/sss5/vid2.mp4&quot;)</p><p>movie3 = (&quot;/home/pi/sss5/vid3.mp4&quot;)</p><p>ser = serial.Serial('/dev/ttyUSB0',9600)</p><p>while True:</p><p> data = ser.readline()</p><p> if data == &quot;a&quot;:</p><p> os.system('killall omxplayer.bin')</p><p> print &quot;a&quot;</p><p> omxc = Popen(['omxplayer','-b',movie1])</p><p> elif data == &quot;b&quot;:</p><p> os.system('killall ommxplayer.bin')</p><p> print &quot;b&quot;</p><p> omxc = Popen(['omxplayer','-b',movie2])</p><p> elif data == &quot;c&quot;:</p><p> os.system('killall omxplayer.bin')</p><p> print &quot;c&quot;</p><p> omxc = Popen(['omxplayer','-b',movie3])</p>
<p>https://oscarliang.com/raspberry-pi-and-arduino-connected-serial-gpio/</p><p><br><br>Shramik4954<br>Looks like you need to import serial<br>Check this site</p>
<p>sir, the problem is solved</p><p>&quot;data = ser.readline()&quot; had to be &quot;data= ser.read()&quot;</p><p>the python script:</p><p>import sys</p><p>import os</p><p>from subprocess import Popen</p><p>import serial</p><p>movie1 = (&quot;/home/pi/sss5/vid1.mp4&quot;)</p><p>movie2 = (&quot;/home/pi/sss5/vid2.mp4&quot;)</p><p>movie3 = (&quot;/home/pi/sss5/vid3.mp4&quot;)</p><p>ser = serial.Serial('/dev/ttyUSB0',9600)</p><p>while True:</p><p>data = ser.read()</p><p>if data == &quot;a&quot;:</p><p>os.system('killall omxplayer.bin')</p><p>print &quot;a&quot;</p><p>omxc = Popen(['omxplayer','-b',movie1])</p><p>elif data == &quot;b&quot;:</p><p>os.system('killall ommxplayer.bin')</p><p>print &quot;b&quot;</p><p>omxc = Popen(['omxplayer','-b',movie2])</p><p>elif data == &quot;c&quot;:</p><p>os.system('killall omxplayer.bin')</p><p>print &quot;c&quot;</p><p>omxc = Popen(['omxplayer','-b',movie3])</p><p>and now the videos are playing but the transition from one video to next is not fast and i can see the terminal script running between the change of two video.</p><p>also sometimes two videos are mixed and played together like one video is running in background while other is playing.</p><p>is there a way in which the lag between two video transition can be minimised and how to avoid two video mixing.</p>
<p>sir, next things which i had done is .</p><p>the python script:</p><p>import sys</p><p>import os</p><p>from subprocess import Popen</p><p>import serial</p><p>movie1 = (&quot;/home/pi/sss5/vid1.mp4&quot;)</p><p>movie2 = (&quot;/home/pi/sss5/vid2.mp4&quot;)</p><p>movie3 = (&quot;/home/pi/sss5/vid3.mp4&quot;)</p><p>movie4 =(&quot;/home/pi/sss5/vid4.mp4&quot;)</p><p>os.system('killall omxplayer.bin')</p><p>omxc = Popen(['omxplayer','-b',movie4])</p><p>ser = serial.Serial('/dev/ttyUSB0',9600)</p><p>while True:</p><p>data = ser.readline()</p><p>if data == &quot;a&quot;:</p><p>os.system('killall omxplayer.bin')</p><p>print &quot;a&quot;</p><p>omxc= Popen(['omxplayer','-b',movie1])</p><p>elif data == &quot;b&quot;:</p><p>os.system('killall ommxplayer.bin')</p><p>print &quot;b&quot;</p><p>omxc = Popen(['omxplayer','-b',movie2])</p><p>elif data == &quot;c&quot;:</p><p>os.system('killall omxplayer.bin')</p><p>print &quot;c&quot;</p><p>omxc = Popen(['omxplayer','-b',movie3])</p><p>when this script is run the movie4 has to play contionously until the some data is read serially and then play the respected video.</p><p>it starts with the movie4 but then something happens and nothing is displayed on the screen.</p><p>can you help me out with this ??</p>
<p>Let me look into it when I get back to my RPis. The code looks fine to me. When you say &quot;something happens&quot;, can you explain a bit more?</p>
<p>when i run the python script.</p><p>the 1st movie starts playing. then when the serial data is recieved the terminal some script is run and the pi goes to power saver mode and if &quot;esc&quot; is pressed it returns to play the video.</p>
<p>Have a look at this site. It looks like what I did but set up a lot cleaner. I wish I would have found this before.</p><p><a href="http://raspberrypi.stackexchange.com/questions/47738/omxplayer-wrapper-run-separated-files-in-specific-sequence" rel="nofollow">http://raspberrypi.stackexchange.com/questions/477...</a></p><p>That probably won't take care of the power save mode but it will get you your looping video and maybe a more seamless transition between videos.</p>
<p>Great to hear it is working. As for the lag, I have not figured that out. I am sure something like VLC would have to be used instead of omxplayer. It is a really simple video player.</p>
<p>beside lag, sometimes two videos are mixed and played together. can u help me out with this?</p>
<p>Could I be able to trigger a video transition gaplessly ? I would like to use it for a haunted video scare =) Triggered by motion sensor</p>
<p>Hey AlejandroS46. That sounds like a really cool project!</p><p>I am not sure. omxplayer is very simple and the way I have it running, I have to close the instance that I am using to trigger another instance, otherwise both instances run at the same time. That is what causes the transition gap.</p><p>You may want to look into alternative video players such as VLC. They may have better options. A quick google search found this <a href="https://www.raspberrypi.org/forums/viewtopic.php?f=38&t=26277"> https://www.raspberrypi.org/forums/viewtopic.php?...</a></p><p>Sorry for the lack of an answer. I had the same problem with the transition gap, but my project did not need to be gapless, so omxplayer worked for me. If you find a solution, that sounds like a great instructable. Let me know too!</p>
<p>Great, to see this project.</p><p>Could you also plan a similar project to work with UART input. With PI-0 this would be really great, a media player working with Serial Input.</p><p>Thank you for this project</p>
<p>Thanks for the comment charudatt! I am glad you liked it. I am sure you could do something very similar with a Pi0 pretty easily. </p><p>While I do know some, UART still hangs me up. Most of what I learn is from sites like this and through my own trial and error. Thus, I have not worked with UART (at least that I know of) much.</p>
<p>Awesome! could you use the same code if you used buttons?</p>
Thanks dbrett21! The way the code is right now, it only works with latching switches and not momentary. If you want to use momentary buttons, just comment out lines 51-53 and it should work (just tested it out)<br> Here is the code to comment out:<br> <br> elif (player and input_state1 and input_state2):<br> &nbsp; &nbsp; &nbsp;os.system('killall omxplayer.bin')<br> &nbsp; &nbsp; &nbsp;player = False<br> <br> Hope that helps. Let me know how it works for you.
Thank you
<p>This is super cool!!! Great idea.</p>
very clever. well done

About This Instructable




Bio: Guy in his 30's who has always had an interest in building things since a kid. I build hardware durning the summer and software ... More »
More by ThothLoki:Play Video With Python and GPIO Portable Arduino Uno Temperature and Humidity Sensor with LCD Screen 
Add instructable to: