DIY Cat Toy with a Raspberry Pi, LDR, and DC Motor
By Ryan Jones
This object detection-based cat toy will keep your cat’s head spinning for hours! Who would have thought that cats would be interested in a Raspberry
pie Pi? This Pi project is activated by moving objects, so it’s easy to trigger and pet-friendly. Set it on the floor and watch your cat go nuts!
- Raspberry Pi (model A+, B, B+, or the Pi 2) and necessary components
- Photoresistor (AKA light-dependent resistor/LDR)
- 220 Ω resistor
- 2N2222 NPN transistor
- 1N4001 diode
- 5V relay
- DC motor
- Extra batteries or a separate power supply (for the motor)
- Feathery thing
My cat Olive gets a little lonely around the house sometimes, so I wanted to make something that could keep him entertained while I’m gone. Thanks to this handy object-detection design, Olive can turn his toy on and off whenever he wants (if he ever wants to…).
When something or someone passes over the detector, the toy turns on and will remain until the detector detects a second. The elevated platform encourages Olive to pass by the photoresistor when he is walking near the toy. If you turn this project into a dog toy, it might find a bit more use; Olive didn’t seem very interested.
The entire Cat Toy, built on elevated pegboard.
How Does it Work?
The Raspberry Pi is an embedded computer that we can be programmed with Python. Thanks to the Raspberry Pi tutorial this project was based on (Build an Object Detection DC Motor Controller which has more details), I was able to do this in no time! First, we’ll talk about the circuitry, and then move on to programming.
This Raspberry Pi cat toy uses two separate circuits: a relay driver circuit (we used one in the Launchpad-Activated Fan project) and a photoresistor/LDR light-detection circuit (similar to our Laser Diode Tripwire project). As you know, a photoresistor/LDR’s resistance varies according to the intensity of the light illuminating your device.
A graph showing the nonlinear, inverse relationship between light intensity and the resistance of a photoresistor.
We convert the illumination into a changing voltage by creating a resistive voltage divider. It may seem like we just plugged the LDR directly into the GPIO pin, but that wouldn’t work. Within the code, we need to enable the pin’s internal pull-up resistor so that our input to the GPIO is actually the output of a resistive voltage divider consisting of the photoresistor/LDR and the internal pull-up resistor.
Since we are using a digital pin, the analog voltage from the divider is interpreted as a logic high or logic low. As light intensity decreases, the resistance (and our divider voltage) increases, and at some point, the Raspberry Pi will detect a logic high on GPIO4. Then, the Raspberry Pi will send a logic high to the base of the transistor in the relay circuit.
Raspberry Pi Cat Toy Schematic
Full cat toy diagram made with Fritzing. You can find a full sized diagram here.
To get you RPi up and running, you need to install Raspian, a free open-source operating system, called Raspbian onto your Pi’s SD card. With your Pi properly hooked up to a mouse, monitor, and keyboard (Or Your Laptop display) open the “Terminal” application. This is where we’ll write the Python code, in three simple steps:
Download the Object Detection Python Code and save it to the /Pi directory as-is.
Enable the Raspberry Pi’s Python GPIO library by copying and pasting the code shown below. Allow the command to run and then wait for the next prompt.
sudo apt-get install python-dev python-rpi.gpio
Next, tell the terminal to run the Object Detection program using the command below:
sudo python object_detection.py
If you don’t have a cat, there are plenty of projects you can make with this circuit! The object detection circuit can be adapted for use in a variety of applications. Wouldn’t it be nice if your house lights turned on when the Pi detected you walking through the door? Combine this with other Raspberry Pi and home automation projects and get started on your smart house!
Remember to check out previous projects, the earlier ones can help you to understand the newer ones!