This object detection toy will keep your cat's head spinning for hours!

Who ever thought your cat would be interested in a raspberry pie pi? This Pi project can be activated simply by a moving object, so it is easy to trigger and pet-friendly. Set it on the floor and watch their heads spin!

### Why?

Olive gets a little lonely sometimes around the house, so I wanted to create something that he could use while I am gone. Thanks to this handy object-detection design, Olive is able to turn his toy on and off whenever he pleases (if he ever pleases).

When something passes over the detector, the toy turns on and remains on until a second pass is detected. The elevated platform encourages Olive to pass by the photoresistor when he is walking toward or away from the toy. If you turn this into a dog toy, it might find a bit more use; Olive didn't seem to care.

### How?

The Raspberry Pi is an embedded computer that we can program with Python. Thanks to the original AAC project (which has more details), I was able to do this in no time! Let's first talk about the circuitry, and then we'll move on to programming.

The Object Detection Cat Toy involves two separate circuits: a photoresistor light-detection circuit (somewhat similar to our Laser Tripwire project) and a relay-driver circuit (also used in our Crop Duster Buster project). As you know, the resistance of a photoresistor varies according to the intensity of the light illuminating the device.

##### A graph showing the nonlinear, inverse relationship between light intensity and the resistance of a photoresistor.

We convert this to a changing voltage by creating a resistive voltage divider. You may think that we plugged the LDR directly into the GPIO pin, but that wouldn't work. Within the code, we enable the pin's internal pull-up resistor so that the input to the GPIO is actually the output of a resistive voltage divider consisting of the photoresistor and the internal pull-up resistor.

We're using a digital pin here, so the analog voltage from the divider is simply interpreted as a logic high or logic low. As light intensity decreases, resistance (and hence the divider voltage) increases, and at some point the Pi will detect a logic high on GPIO4. Then, the Pi sends a logic high to the base of the transistor in the relay circuit.

##### Full Cat Toy circuit diagram, made using Fritzing. Click to enlarge.

To get up and running, you must first install the free open-source operating system, called Raspbian, onto your Pi's SD card. With your Pi properly hooked up to a monitor, mouse, and keyboard, open the "Terminal" application. This is where the Python code will be written, in three easy steps:

1. Download the Python code by clicking on the orange button (scroll down a bit) and then save it to the /Pi directory as-is.
2. Enable the Pi's Python GPIO library by copying and pasting the following code. Allow the command to run and then wait for the next prompt.

                    sudo apt-get install python-dev python-rpi.gpio


3. Then, tell the terminal to run the Object Detection program using the following command:

                    sudo python object_detection.py


You've done it! If you have any issues, remember to refer to the original project for further details. You can also check out the Adafruit tutorial on using the Pi's GPIO!

Take this and run with it! The object detection circuit can be adapted for use in a variety of applications. My first instinct is to always think of my cat, but maybe you have a clever plan for touchless power control! Wouldn't it be nice if your house lights turned on when the Pi detected you walking through the door? Combine this with other Pi home-automation projects and have your first Smart House!

Remember to always check out our previous projects (the earlier ones just might help you to understand the later ones) and let us know what you think!

#### Other MIT-i Innovations:

Give this project a try for yourself! Get the BOM.