# DIY Raspberry Pi Automatic Dog or Cat Feeder: Full Tutorial

Build your own pet feeder, from hardware setup to programming with a Raspberry Pi and Viam.

## Introduction
If your pet is as insatiable as mine, you are familiar with waking up to the sound of gentle whining and begging paws—two hours before the alarm.

## Summary
By following this tutorial, you'll:
- Build your own automatic pet feeder using Raspberry Pi.
- Train a custom machine learning (ML) model with Viam’s Data Manager to recognize your pet.
- Implement Viam’s Machine Learning and Vision Services to run the model on your robot.
- Integrate a stepper motor and 3D-printed dispenser to release treats when your pet is identified.

## Requirements

### Hardware
You will need the following hardware components:
1. A computer running macOS or Linux
2. [Raspberry Pi](https://www.raspberrypi.com/products/raspberry-pi-4-model-b/) with [microSD card](https://www.amazon.com/Amazon-Basics-microSDXC-Memory-Adapter/dp/B08TJTB8XS) (and [microSD card reader](https://www.amazon.com/Card-Reader-Beikell-Memory-Adapter/dp/B09Z6JCKL7))
3. [Raspberry Pi power supply](https://www.amazon.com/Raspberry-Model-Official-SC0218-Accessory/dp/B07W8XHMJZ/)
4. [Stepper motor and motor driver](https://makersportal.com/shop/nema-17-stepper-motor-kit-17hs4023-drv8825-bridge)
5. [12V power supply adaptor for motor driver](https://www.amazon.com/ABLEGRID-12-Volt-Power-Supply/dp/B009ZZKUPG/)
6. [Simple USB powered webcam](https://www.amazon.com/wansview-Microphone-Streaming-Conference-Teaching/dp/B08XQ3TWFX/)
7. Assorted jumper wires
8. [Four 16mm or 20mm M3 screws](https://www.amazon.com/Cicidorai-M3-0-5-Button-Machine-Quantity/dp/B09TKP6C6B/)

### Tools and Other Materials
You will also need the following tools and materials:
1. Wide mouth Mason Jar or [blender cup](https://www.amazon.com/Ninja-Single-16-Ounce-Professional-Blender/dp/B07Q23X5WP/) (if you want to avoid using glass!)
2. Small pet treats or dry kibble
3. Tools for assembly such as screwdrivers and allen keys
4. 3D printer (or somewhere you can order 3D printed parts from)
5. [3D printed STL models](https://github.com/viam-labs/smart-pet-feeder)

### Software
You will need the following software:
- [Python 3](https://www.python.org/download/releases/3.0/)
- [pip](https://pip.pypa.io/en/stable/#)
- [`viam-server`](https://docs.viam.com/get-started/installation/) installed to your board.

## 1. Assemble Your DIY Dog or Cat Feeder

1. Prepare your 3D prints. The front of the main body of your print is the side with the dog bone.
   
2. Mount your Raspberry Pi to the side of the main body of your pet feeder.
3. Connect your power source to the Pi.
4. Mount your webcam to the front of your pet feeder.
5. Insert the 3D printed stepper motor wheel into your pet feeder.
6. Place your stepper motor into the motor holder part.
7. Connect your stepper motor to the motor driver according to the wiring diagram:

## 2. Configure and Test Your Automatic Pet Feeder

After setting up your robot, proceed to configure and test it.
1. Set up the Raspberry Pi.
2. Configure components in the [Viam app](https://app.viam.com/).
3. Test your DIY smart pet feeder’s motor.

### Test Your Smart Pet Feeder’s Camera
Next, test the camera. Ensure you can see your pet.

## 3. Use ML to Recognize Your Cat or Dog
Make your pet feeder smart by capturing data using ML models.
### Configure Data Management

1. Under the **CONFIGURE** tab, add the **Data Management** service to sync data with the Viam app.
2. Ensure **Data Capture** and **Cloud Sync** are enabled.

### Capture Images of Your Pet
Collect images of your pet using the feeder. 
### Create a Dataset and Tag Images
1. Select images to tag with your pet's name.
2. Add each image to a dataset.

## 4. Control Your DIY Smart Pet Feeder Programmatically
Use the [Viam SDK](https://docs.viam.com/build/program/apis/) in Python.
### Set Up Your Python Environment
1. SSH into your Pi.
2. Install Python Package Manager:
   ```bash
   sudo apt install python3-pip
   ```
### Add the Logic
- Implement logic to control the motor based on ML classifications of the camera feed.

```python
async def main():
    # Initialize components
    ...
    while True:
        # Check camera stream for pet
        classifications = await puppyclassifier.get_classifications_from_camera(camera_name)
        if found:
            # Dispense treat
            await stepper.go_for(rpm=80, revolutions=2)
        else:
            await stepper.stop()
```  
### Upgrade Your DIY Cat or Dog Feeder
Consider adding features like sound alerts or multi-pet recognition.

## Full Code
The entire code for the project in Python:
```python
import asyncio
...
# Add your full code here
```

Viam is the fastest way to build a robot. Register for Viam 101 by July 31 for an opportunity to win a UFACTORY Lite 6 arm.
