Overview

How smart is your cat?

This treat dispenser is an easy way to feed and entertain your cat when you're not at home. The box is triggered by a pad that detects a change in capacitance when touched. This makes it very sensitive to little paws, and can be easily repositioned it to make it easier or more challenging for your cat to trigger. 

This project uses readily accessible household materials and a handful of electronics to create an interactive toy that your cat (or dog) will enjoy. This treat dispenser can be a good way to exercise your kitty's brain or as a proxy measure of their feline IQ

Recommended electronics

1 x Circuit Playground Express
Circuit Playground Express is the perfect introduction to electronics and programming
1 x 3xAA holder with DC jack
Battery holder 3xAA batteries with 2.1mm DC jack
1 x USB cable - A/MicroB - 3ft
Standard A to micro-B USB cable
1 x CRICKIT for Circuit Playground Express
Creative Robotics and Interactive Construction Kit is an add-on to our popular Circuit Playground Express
The project can be done without a Crickit, by connecting the servo directly, but the crickit makes it a lot easier!
1 x Alkaline AA batteries - 3 pack
These batteries are good quality at a good price, and work fantastic with any of the kits or projects that use AAs
1 x Micro servo
This little servo can rotate approximately 180 degrees (90 in each direction). Works just like standard servos you're used to, but smaller.
1 x Flex PCB Material - Pyralux - 6" by 6" Square
1 oz of copper bonded to a sheet of polyamide for strength and flexibility. This material can be creased and bent and won't crack as easily as copper tape.
You can use tin foil instead of pyralux if you only have that, but it wont last as long!

Household materials

In addition to these electronics, you should also grab:

Read on to learn how to build your own!

Build the Chute

The first step is to insert your tube into your box. This will require cutting two ellipsoid-shaped holes in the box so that the tube is held at an angle.

Squeeze your tube slightly into an oval, trace this outline on the top of your box.

 

Cut out this hole in the top.

 

Insert tube at an angle and trace out a much longer oval in the bottom of the box.

 

Cut out the oval.

Widen and lengthen holes as necessary. It may take a couple revisions to get the length and width of the second hole correct. 

Push tube through both holes. The fit should be tight, but not so tight that it bends the cardboard tube or distorts the box dramatically.

If you don't have an X-acto knife or similar, the 3D printed handle pictured in this guide can be found on Thingiverse and pairs well with standard utility knife blades to make an excellent hobby knife.

Add a Gate

Create an opening

 

Mark a slit where the gate will go. This should be parallel to the face of the box and extend only halfway through the tube.

 

Cut out area for gate using a box cutter.

 

Test the fit of the gate material, should be loose. 

Create the Gate

 

On a scrap piece of cardboard, trace the top edge of the gate. This is a mirror image of the curvature on the inside of the tube! We will use this as a guide to create a door that sits flush inside the tube.

 

Cut out the "U" shape you traced. This is your guide.

 

Trace shape onto a long piece of cardboard. Extend the lines further down the piece of cardboard. This is the outline of your gate.

 

Cut out the gate and poke a small hole in the top edge.  

Attach the Arm

Find a paper clip and unbend it so it's a straight(ish) piece of wire.

 

Use pliers to bend one end over and hook this into the hole in the top of the gate.

 

Crimp paper clip in place so it hold onto the cardboard tight. 

 

Insert gate into opening in tube. Test that it slides easily up and down.

Check that when the gate is closed it leaves no gaps between itself and the tube that treats might slip through.

Add Electronics

Now for the fun stuff. Let's add some electronics to this contraption!

Mount Servo Motor

 

Mark placement of motor with a pencil.

 

Use hot glue to stick the motor in place.

 

Bend paper clip at point where it will connect to servo arm.

 

Trim excess off paper clip.

Poke the end of the paper clip through the servo arm. Our gate is now motorized!

Connect to CRICKIT

Connect servo to Servo port 1 and alligator clip to Touch pad 1. The other end of the alligator clip will connect to your capacitive touch pad which can be placed anywhere outside the box.

Now is also a good time to connect your battery pack which will power the servo motor.

Capacitive trigger

Cut out a small section from the sheet of Pyralux PCB material (or other conductive substance). This will be the trigger for the servo motor.

You can use anything metallic, even tin foil, but it wont last as long

Clip the other end of the alligator clip to the copper Pyralux. This trigger can be placed anywhere on the outside of the box.

When the cat touches this pad, it will change the capacitance of the pad. As it is connected to the Crickit via a wire with alligator clips, the change in capacitance will register on the Crickit capacitive touch input.

Add a tray (Optional)

If you'd like to add a tray to catch treats as they're dispensed, a large shipping tube or something similar works well.

 

Cut tube down the middle.

 

Using hot glue, stick one half of tube to bottom of box.

MakeCode

Now it's time to upload some code! For this we will be using Microsoft MakeCode for Adafruit, a web-based code editor. It provides a block editor, similar to Scratch or Code.org, and also a JavaScript editor for more advanced users.

If you haven't used MakeCode before, this guide is a good place to start.

Getting into Bootloader Mode

To make your board work with MakeCode we need to put it into "bootloader mode". All that's required to do this is to connect the board to your computer with a micro USB cable and click the small reset button in the center of the board. 

Now we're ready for MakeCode!

Click this link or the button below to enter the portal to interact with the code for this project.

How to upload code

To upload code, connect you Circuit Playground Express to your computer using the micro USB cable, click the Download button to download the .uf2 file to your computer, and drag 'n drop it onto the CPLAYBOOT drive. 

The drive will automatically eject itself. (Your computer may give you a "failed to eject drive correctly" error, you can ignore this.) The code is now on your Circuit Playground Express and ready to run!

What does this code do?

This sketch in MakeCode uses CRICKIT's capacitive touch sensing abilities to trigger the servo motor.

In the on start block we can see that there are two startup commands to set the volume level (not too loud) and lower the servo to ensure the gate starts in the closed position. 

In the forever block there is an if statement, which says:

  • If the value of crickit read touch 1 rises to between 700 and 1000
  • Then play a sound (in this case "ba ding") and raise the gate for 200ms before lowering it back down

If you'd like to play with this code, click "Edit" and a new window will open in which you can create your own version.

Capacitive touch is sensitive to humidity and may react differently depending on your environment.

Before creating your final assembly it's a good idea to test that your circuit works.

Capacitive touch is read as a value between 0 and 1023. You can increase or decrease the capacitive touch 'window' as necessary to get the sensitivity level you need (for example from between >700 and <1000 to between >850 and <950). 

If CRICKIT stops responding to touch altogether a single press of the little reset button will restart the whole thing and bring it back to life again.

Once you're satisfied that your box is working, place CRICKIT and battery pack inside and hold in place with double sided foam tape.

The touch pad can be attached anywhere on the outside of the box where your cat is likely to paw at it (the bottom of the chute is a good place to start). Attach using double sided tape.

CircuitPython

If you feel more comfortable coding with CircuitPython rather than MakeCode, this page is for you.

The benefits of using CircuitPython for capacitive touch in this case are that it's less likely to freeze (requiring restart), allows greater complexity in any reward scheme you'd like to set up, and allows you to play any sound you want in combination with a treat being dispensed.

Getting Familiar

CircuitPython is a programming language based on Python, one of the fastest growing programming languages in the world. It is specifically designed to simplify experimenting and learning to code on low-cost microcontroller boards.

CircuitPython is easiest to use within the Mu Editor. If you haven't previously used Mu, this guide will get you started.

If you haven't used Circuit Playground Express with CRICKIT before, make sure you've updated it with the latest special 'seesaw' version of the CPX firmware. This guide will show you how.

Copy that code!

To start, copy and paste the code below into your Mu editor.

import time
import board
import audioio
from adafruit_crickit import crickit

print("Cat Feeder")

feed_servo = crickit.servo_1

# audio output
cpx_audio = audioio.AudioOut(board.A0)
f = open("activate.wav", "rb")
wav = audioio.WaveFile(f)

while True:
    if crickit.touch_1.value:
        time.sleep(0.1)
        cpx_audio.play(wav)
        feed_servo.angle = 180
        time.sleep(0.2)
        feed_servo.angle = 0
        time.sleep(0.1)

Uploading

Make sure you've connected the Circuit Playground Express to your computer and have Mu open and connected to your board.

Once the code is copied into Mu, press the Save button - your code should be saved to the CIRCUITPY disk drive (which appears when the Circuit Playground Express is plugged into your computer) as code.py.

You may also include a WAV file to play when capacitive touch is triggered. Name this file "activate.wav" in your CIRCUITPY drive for it to be recognized by the code.

If you want to create and use your own wav files, see this guide to ensure they are formatted for playing on CRICKIT.

Troubleshooting

Problem: My Circuit Playground Express isn't recognized by Mu!

Solution: Make sure your board is set up with CircuitPython, which has the Circuit Playground Express show up as a flash drive named CIRCUITPY when you connect the CPX to your computer. If it is showing up as CPLAYBOOT on your computer, you can follow the steps in this guide to ensure CircuitPython is loaded and you see the CIRCUITPY drive.

 

Problem: My servo isn't moving!

Solution: Check that the switch on CRICKIT is set to "ON".

 

Problem: My servo still isn't moving! 

Solution: Make sure you've updated your Circuit Playground Express with the latest special 'seesaw' version of the CPX firmware. This guide will show you how.

Train your cat!

Now it's time to introduce your cat to this mysterious new box. The sound of the servo motor may be startling at first, but becomes normalized over time.

Many different designs for treat dispensers can be found on the internet, but all rely on the same principle of positive reinforcement, part of a larger branch of psychology known as Operant Conditioning

The science of Operant Conditioning can produce dramatic changes in animal behavior by leveraging different reinforcing stimuli. This project, more than simply a fun way to occupy your pet, can also be used to powerfully influence your animal's behavior.

For ideas on other ways to put your treat dispensing box to use, projects like Josh Klein's crow vending machine, or Karen Pryor's book Don’t Shoot The Dog: The New Art of Teaching and Training provide fascinating examples of the potential power of positive reinforcement. 

Exploring further

If you enjoy this project and want to continue exploring you can find lots more MakeCode projects and CircuitPython projects on the Adafruit Learning System.

This guide was first published on Sep 05, 2018. It was last updated on Sep 05, 2018.