AUTOMATIC CAT FEEDER
In this project, we will build an automated cat feeder that uses AI to detect a specific cat and dispense food. The motivation behind choosing this project is twofold. Firstly, I am concerned that when I am away from home for long periods of time, my cat may not have access to food. Secondly, I want to address the issues of anxiety and stress in my cat. Cats can become anxious or stressed when their feeding schedule is interrupted or when they perceive uncertainty about their next meal. By ensuring a consistent feeding routine, this project aims to alleviate such stress.
This automated cat feeder integrates a motion sensor to activate the camera, and if the correct cat is detected, the servo motor will release the food. This guide will walk you through the setup using a Raspberry Pi, camera module, motion sensor, and servo motor.
Things What You Need
Components
- Raspberry Pi (with Raspbian OS installed)
- Camera
- PIR Motion Sensor
- Servo Motor
- Power Supply for Raspberry Pi
- Breadboard and Jumper Wires
- Cat Food Dispenser
Materials
- Cardboard
- Modeling Knife
- Silicone
- Silicone gun
- Tape measure
- Compass
The total cost of this project is around 150€ - 200€. Depending on where you buy the components. I've made a bom of materials where u can find a link to all web stores.
Downloads
Set Up Raspberry Pi
- Install Raspbian OS: Ensure your Raspberry Pi has the latest version of Raspbian OS installed.
- Connect the Camera Module: Attach the camera module to the Raspberry Pi using the CSI port.
- Enable Camera Interface: Open the terminal and type sudo raspi-config. Navigate to Interfacing Options and enable the camera.
Connect Motion Sensor
1. Connect the PIR Sensor: Use jumper wires to connect the PIR sensor to the GPIO pins on the Raspberry Pi.
- VCC to 5V
- GND to GND
- OUT to GPIO 4
2. Test the Sensor: Write a simple Python script to test if the motion sensor is working.
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
PIR_PIN = 4
GPIO.setup(PIR_PIN, GPIO.IN)
try:
print("PIR Module Test (CTRL+C to exit)")
time.sleep(2)
print("Ready")
while True:
if GPIO.input(PIR_PIN):
print("Motion Detected!")
time.sleep(1)
except KeyboardInterrupt:
print("Quit")
GPIO.cleanup()
Integrate the Servo Motor
Connect the Servo Motor: Connect the servo motor to the GPIO pins.
- Red wire to 5V
- Brown wire to GND
- Orange wire to GPIO 17
Test the Servo Motor: Write a Python script to test the servo motor.
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)
pwm = GPIO.PWM(17, 50)
pwm.start(0)
def SetAngle(angle):
duty = angle / 18 + 2
GPIO.output(17, True)
pwm.ChangeDutyCycle(duty)
time.sleep(1)
GPIO.output(17, False)
pwm.ChangeDutyCycle(0)
try:
while True:
SetAngle(90) # Adjust angle as needed
time.sleep(2)
SetAngle(0)
time.sleep(2)
except KeyboardInterrupt:
pwm.stop()
GPIO.cleanup()
Set Up the AI Model
To set up the AI model for detecting your specific cat, you'll need to follow these steps: data collection, data labeling and annotation, and finally, setting up the AI model with YOLOv8.
1. Collect Data
First, gather images of your cat in various positions and lighting conditions. Ensure you also collect some images of other cats and objects to help the AI model distinguish your cat from others. Aim to collect at least 100-200 images for better accuracy.
2. Label and Annotate Data
Use Roboflow to label and annotate the collected images. This process involves marking the images to identify and distinguish your cat.
- Upload Images to Roboflow: Create a new project in Roboflow and upload your collected images.
- Annotate Images: Use the annotation tool to draw bounding boxes around your cat in each image. Make sure to label each box correctly (e.g., 'my_cat').
- Export Dataset: Once all images are annotated, export the dataset in a format compatible with YOLOv8 (e.g., YOLOv5 Pytorch format).
3. Set Up YOLOv8
YOLOv8 (You Only Look Once version 8) is a state-of-the-art object detection model. Follow these steps to set up YOLOv8 for your project.
- Install YOLOv8: Ensure you have the necessary libraries installed. You can use the Ultralytics YOLOv8 repository for installation.
pip install ultralytics
- Prepare Your Dataset: Place the annotated dataset in the appropriate directory structure required by YOLOv8. Typically, this involves organizing images and labels into train and val directories.
- Train the Model: Use the following script to train your YOLOv8 model with the annotated dataset.
from ultralytics import YOLO
# Load the model
model = YOLO('yolov8n.yaml') # use 'yolov8n.pt' for a pre-trained model
# Train the model
model.train(data='path_to_your_dataset.yaml', epochs=50)
# Save the trained model
model.save('best_model.pt')
- Test the Model: Write a script to test the model and ensure it correctly identifies your cat.
import cv2
from ultralytics import YOLO
# Load the trained model
model = YOLO('best_model.pt')
# Function to detect cat
def detect_cat(image):
results = model(image)
for result in results:
if 'my_cat' in result.names:
return True
return False
# Test with a sample image
image = cv2.imread('path_to_test_image.jpg')
if detect_cat(image):
print("Cat detected!")
else:
print("Cat not detected!")
Integrate Everything
Combine All Scripts: Write a Python script that integrates the motion sensor, camera and servo motor with the AI model. You can take the final.py file in my Github repository as an example.
Final Assembly
- Mount Components: Secure the camera module, motion sensor, and servo motor in appropriate positions on the cat feeder.
- Test the System: Run the integrated script and test the system to ensure it works as expected.
Conclusion
By following these steps, you will have created an automatic cat feeder that uses AI to detect your cat and dispense food. This project involves various skills including programming, electronics, and machine learning, making it an excellent learning experience.
For more detailed instructions and source code, please visit the GitHub repository for this project.