A Raspberry Pi or other Linux-running portable computer can be used as a small sensing platform; typical USB computer peripherals like webcams can be plugged in directly and controlled via simple Python scripts. There are already many easy-to-follow tutorials online for accomplishing this, and the associated instructions are usually even easier to follow that typical Linux OS instructions: the first step is usually to grab a Raspberry Pi and download a 'fresh distribution' of the operating system, so everyone following the tutorial starts with precisely the same hardware and software. So, if you've any interest in playing around with Linux, it's a great way to start -- there's a huge, supportive community online, and the hardware is relatively cheap.
Below I'm posting some quick notes on setting up a Raspberry Pi with button control, so that arbitrary python scripts can be run on the device by pressing a button -- in this case, taking a webcam snapshot. Then, for fun, I added in servo control -- for a 'panning timelapse', perhaps.
Controlling a python script with a button on the Raspberry Pi
- Download Occidentalis image and burn it to an SD card
- Boot up the RPi with the SD card, and configure settings on pi as prompted -- especially "expand root" and "locale" and "timezone". This, and any of the software-update steps below, will require that the RPi is connected via ethernet.
- "sudo apt-get update"
- "sudo apt-get install python-dev"
- "sudo apt-get install python-rpi.gpio"
- "supo apt-get install fswebcam"
- Grab a webcam and plug it into the USB port
- For button action, wire up as per diagram here
- Demo python code for using a button here
Sample code for triggering a webcam with a button:
#!/usr/bin/env python from time import sleep import os import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(25, GPIO.IN) while True: if ( GPIO.input(25)== False ): #os.system('mpg321 vader.mp3 &') #os.system('fswebcam test.jpeg') print "Switch connected" os.system('fswebcam test.jpeg') else: print "Switch disconnected" sleep(0.1);
Adding servo control
Code for combining button presses with camera shots and servo action:
#!/usr/bin/env python from time import sleep import os import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(25, GPIO.IN) def set(property, value): try: f = open('/sys/class/rpi-pwm/pwm0/' + property, 'w') f.write(value) f.close() except: print('Error writing to: ' + property + " value: " + value) def setServo(angle): set('servo', str(angle)) set("delayed", "0") set("mode", "servo") set("servo_max", "180") set("active", "1") delay_period = 3 while True: if ( GPIO.input(25)== False ): print "switch connected" setServo(45) sleep(1) os.system('fswebcam test.jpeg') sleep(2) setServo(90) sleep(1) os.system('fswebcam test.jpeg') sleep(2) else: print "waiting ..." sleep(0.1);