Splint
Posts: 3
Joined: Tue Jan 28, 2020 4:15 pm

Tilt switch/acceleromter to trigger camera?

Fri Jun 26, 2020 9:02 am

Hi,

Can anyone point me in the direction of a project with code to use a tilt switch or an accelerometer to trigger a USB or pi camera to take a photo?

gordon77
Posts: 5036
Joined: Sun Aug 05, 2012 3:12 pm

Re: Tilt switch/acceleromter to trigger camera?

Fri Jun 26, 2020 10:16 am

Here's using a tilt switch.. https://www.sunfounder.com/learn/lesson ... or-pi.html
Here's using picamera.. https://picamera.readthedocs.io/en/rele ... ipes1.html

here's some python combining them..

Code: Select all

#!/usr/bin/env python3
import RPi.GPIO as GPIO
from picamera import PiCamera
from time import sleep

# initialize the camera 
camera = PiCamera()

TiltPin = 11


def setup():
	GPIO.setmode(GPIO.BOARD)       # Numbers GPIOs by physical location
	GPIO.setup(TiltPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)

def take_photo(x):
        camera.resolution = (1024, 768)
        camera.start_preview()
        # Camera warm-up time
        sleep(2)
        camera.capture('foo.jpg')
        camera.stop_preview()
        
	

def loop():
	GPIO.add_event_detect(TiltPin, GPIO.FALLING, callback=take_photo, bouncetime=100) # wait for falling
	while True:
		pass   # Don't do anything

def destroy():
	GPIO.cleanup()                     # Release resource

if __name__ == '__main__':     # Program start from here
	setup()
	try:
		loop()
	except KeyboardInterrupt:  # When 'Ctrl+C' is pressed, the child program destroy() will be  executed.
		destroy()

Return to “Beginners”