Skip to content

Instantly share code, notes, and snippets.

@ShawnHymel
Last active September 14, 2017 16:19
Show Gist options
  • Save ShawnHymel/1d5ef8b625c958e8d4e3974a01da4695 to your computer and use it in GitHub Desktop.
Save ShawnHymel/1d5ef8b625c958e8d4e3974a01da4695 to your computer and use it in GitHub Desktop.
Raspberry Pi time lapse and pan camera
#!/usr/bin/env python
import smbus
import calendar
import time
import picamera
import os, errno
wait = 5.0
iso = 100
bus = smbus.SMBus(1)
addr = 0x40
camera = picamera.PiCamera(resolution=(1920, 1080))
photo_dir = '/home/pi/' + str(calendar.timegm(time.gmtime()))
#############################################################
# Functions
#############################################################
def pan(pwm):
bus.write_word_data(addr, 0x06, 0)
bus.write_word_data(addr, 0x08, pwm)
def tilt(pwm):
bus.write_word_data(addr, 0x0a, 0)
bus.write_word_data(addr, 0x0c, pwm)
def deg2pwm(deg):
if deg < 0:
deg = 0
elif deg > 180:
deg = 180
return ((deg * 834) / 180) + 833
#############################################################
# Main
#############################################################
# Create directory for pictures
try:
os.makedirs(photo_dir)
except OSError as e:
if e.errno != errno.EEXIST:
raise
# enable the PC9685 and enable autoincrement
bus.write_byte_data(addr, 0, 0x20)
bus.write_byte_data(addr, 0xfe, 0x1e)
# Set servos to default positions
pan(deg2pwm(0))
tilt(deg2pwm(90))
time.sleep(1)
# Set ISO and let camera automatically set other config
# NOTE: Disabled for all-automatic mode. White balance may change over time.
#print 'Configuring camera'
#camera.iso = iso
#time.sleep(2)
#camera.shutter_speed = camera.exposure_speed
#camera.exposure_mode = 'off'
#g = camera.awb_gains
#camera.awb_mode = 'off'
#camera.awb_gains = g
# Commence timelapse!
print 'Starting timelapse'
for i in range(0, 835):
pan(i + 833)
time.sleep(wait / 2)
filename = photo_dir + '/'
if i < 10:
filename += '00'
elif i < 100:
filename += '0'
filename += str(i) + '.jpg'
camera.capture(filename)
time.sleep(wait / 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment