This is a walkthrough of a raspberry-pi powered, motion sensing, AI-trained, cat-detecting, publisher/subscriber broker (MQTT), app-enabled cat feeder I built for my cat Basil (pictured below).
A 1.5 minute demonstration of Basil and I using it. Here is a link of me using it. This solo project was completed for CS 498 Internet of Things during my Fall 2020 semester at University of Illinois Urbana-Champaign.
Materials
Installation
Motion Sensing
Google's Object Detection API
Servo's
Usage
Credits
License
- Dremel: $60
- Raspberry Pi 4 Model B 4gb: $55
- Raspberry Pi Camera Module 2: $30
- Motion Sensor: $8
- Servo MG995/MG996R: $10
- Power Supply 3A: $7
- Double Cat Feeder Bowls: $9
- [Any Power Drill]
- [Pringles Can]
- [25 ounce or larger can]
- [Zip ties or super glue]
- [Eye Protection] ** DO NOT IGNORE **
Warning Sparks flying while cutting metal. Please be sure to wear eye Protection
- Get both your pringles can and soup can and remove the paper wrapping from the soup can. You can use wrapping paper on your pringles to make it look nicer at the end but thats optional.
- Get a sharpie and mark the bottom of both cans like this:
- Get out a .5 inch spade bit from your drill bit set, and WEAR YOUR EYE PROTECTION
- It should look like this afterwards
- Time to break out the dremel. Keep your eye pro on. If you havnt used it before, watch some youtube videos please. Here is a link of me using it.
- Make sure to cut a hole in the back for the raspberry pi power cable, 4 holes for the servo, and an opening for the food to dispense. Insert zip ties to attach MG995 servo.
- This step is optional but I wrapped my can with cute kitty wrapping paper, although dont get one that is glittery like my wife bought for me.
- We'll come back to installing the pi/camera/motion sensor/servo after you've tested it all out.
The purpose of the motion sensor is to activate the object detection algorithm. It's compute heavy and activates the camera. We don't want to always be running the camera, so a more low-powered approach is using a simple motion sensor for when Basil (cat) walks in front of the cat feeder, and only then it will start the object detection api.
Attach your motion sensor like this:
Here is a good tutorial if you are having difficulty but it is pretty self exaplanatory. https://projects.raspberrypi.org/en/projects/parent-detector/1
Install the rapberry camera pi. There are a few tutorials on how to do so, so there is no need to repeat their great work: https://projects.raspberrypi.org/en/projects/getting-started-with-picamera https://thepihut.com/blogs/raspberry-pi-tutorials/16021420-how-to-install-use-the-raspberry-pi-camera
After you have installed the camera and tests that it has worked. Please check out TensorFlow's object detection API that was designed and specialized for Raspberry Pi. The AI model has been trained to detect 100 or so items. You can check out the list and instructions on the link.
When you have finished the guide take a look at tf_image_dection.py
file which includes the following code:
for detected_obj_data in results:
if detected_obj_data["label"] in ['cat']:
print (detected_obj_data["label"], "has been detected")
return True
When motion is sensed, and activates the object detection, it will put into a list, from the detect_objects
method.
def detect_objects(interpreter, image, threshold, labels):
"""Returns a list of detection results, each a dictionary of object info."""
set_input_tensor(interpreter, image)
interpreter.invoke()
If you have never worked with servos, then it is a good time to read a guide or two on how to control servo motors on the raspberry pi.
https://www.digikey.com/en/maker/blogs/2021/how-to-control-servo-motors-with-a-raspberry-pi
https://pypi.org/project/RPi.GPIO/
Here is the method to set the angle of the servo, which activates using the GPIO
library.
pwm = GPIO.PWM(3, 50)
pwm.start(0)
def set_angle(angle):
duty = angle / 18 + 2
GPIO.output(3, True)
pwm.ChangeDutyCycle(duty)
sleep(1)
GPIO.output(3, False)
pwm.ChangeDutyCycle(0)
Once you have played/installed with your servo, you can attach it to the bottom of the soup can and either use a combination of zipties or super glue to attach it there.
Then you can install the raspberry pi after you have connected the camera/motion detector/servo.
Watch this introduction to the MQTT broker (pub/sub) platform.
https://www.youtube.com/watch?v=EIxdz-2rhLs
https://www.youtube.com/watch?v=dqr-AT5HvyM
MIT License
Copyright (c) 2021 Albert Yeh
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.