Trigger a Webcam with a button and Raspberry Pi

This instructable will show how to trigger a webcam using Raspberry and a push button. A bash script run at startup and it launch a Python script that survey the GPIO port. When the button is pressed, a “fswebcam” command runs.

Step 1: Materials

– Raspberry Pi 2 Model B

– Webcam (Make sure your camera is UVC complain)

– Push Button

– Breadboard

– CablesTrigger a Webcam with a button and Raspberry Pi

Step 2: Wiring

– Connect the push button to the GPIO pins: GPIO15 (pin 12) and GND (pin 6).

– Connect the Webcam to a USB port.

To make sure your camera was recognized, list the devices with:

$cd /dev/


Your camera should be listed as /video*

Step 3: Creating your program in Python

Open a window in Python 3: Menu>Programming>Python 3>File>New Window

Copy the following code and Save As /home/pi/

import subprocess

import RPi.GPIO as GPIO

import time


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



while True:

input_state = GPIO.input(12)

if input_state == 0:“fswebcam -d /dev/video0 -r 1024×768 -S0 “+str(i)+”pic.jpg”,shell=True) print(‘PIC CAPTURED')



except KeyboardInterrupt:


Step 4: Bash script

Open the terminal and run:


Add the following lines to /home/pi/


echo Launching camera

sudo python camerascript.pyTrigger a Webcam with a button and Raspberry Pi

Step 5: Make your script executable and run at startup

First, give make your bash script executable:

$chmod ugo +x /home/pi/

Second, type $nano ~/.bashrc ,and add the following lines at the end of the file to make your script run at startup:

#run startup script

echo .bash running


Third, autologin as pi user:

$sudo nano /etc/inittab

Comment this line: 1:2345:respawn:/sbin/getty –noclear 38400 tty1

Add this line: 1:2345:respawn:/bin/login -f pi tty1 /dev/tty1 2>&1

Finally, reboot your Pi

$sudo reboot

Step 6: Results

Push the button, wait for the trigger and check if a new jpeg file was created in /home/pi/

Have fun.
Thanks for reading.


For more detail: Trigger a Webcam with a button and Raspberry Pi

About The Author

Ibrar Ayyub

I am an experienced technical writer with a Master's degree in computer science from BZU Multan University. I have written for various industries, mainly home automation, and engineering. I have a clear and simple writing style and am skilled in using infographics and diagrams. I am a great researcher and is able to present information in a well-organized and logical manner.

Scroll to Top