Lego/Pi Crittercam

Create a crittercam  with a Lego Mindstorms NXT set, a Raspberry Pi and  a USB Webcam.


To install the Python NXT libraries:

tar -zxvf nxt-python-2.2.2.tar.gz
cd nxt*
sudo python install

The Python NXT libraries will work with either USB or Bluetooth.

We used a simple USB Webcam connected to the Raspberry Pi to take pictures, and the ultrasonic sensor on the Lego NXT to turn on the camera when an object was within range.

For the Webcam software there were a lot of options available, we find that fswebcam is pretty easy to install by:

sudo apt-get install fswebcam

If the Webcam is working then the following Python code should create a picture:

fswebcam -r 384x288 picture1.jpg

The next step is to have the Pi communicate with the Lego NXT ultrasonic sensor. For this we use PORT 4 on the NXT brick. The NXT sensor will return a value of 0-255 cm. For our setup we put the CritterCam next to our cat feeder. You will need to set a distance that triggers the camera, we used 50 cm. For our pictures we name them 1.jpg, 2.jpg and so on. Listing below shows our full CritterCam code:

#!/usr/bin/env python
import os
import time
import nxt
from nxt.sensor import *

print 'looking for NXT ... could take 15 seconds'
b = nxt.locator.find_one_brick()

while True:
	distance = Ultrasonic(b, PORT_4,check_compatible=False).get_sample()

	if distance < 50 :
		print 'You are close Iam going to take your picture'
		filecnt = filecnt + 1
		filename = str(filecnt) + '.jpg'
		os.system('fswebcam -r 352x288 ' + filename)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s