Introduction: Build a Cat Litter Box Fan With Arduino

A hideaway cat litter box is a great way to make your apartment look like a residence for adults instead of crazy cat ladies. A hidden litter box in a cabinet, under a sink, or built in to some other furniture does a lot for appearances, but what about the smell? Here's how you can exploit the power of baking soda and embedded computers to keep your apartment fresh-smelling without disturbing your cat during his or her "quiet time."

What you'll need:
An Arduino-compatible embedded microcontroller (I used the Kameduino and Chibiduino from Brown Dog Gadgets: http://www.browndoggadgets.com/store/arduinocompatible/) and the Arduino IDE from http://arduino.cc/en/Main/Software
A 6-12 volt wall wart, or a 9 volt battery
A DC case fan, 80mm or so, 5-12 volts
A TIP120 "Darlington" NPN transistor (available at your local components drawer)
Three resistors - 220 ohm and two 1000 ohm
A "matched set" of an IR emitter and IR phototransistor (I got mine from Sparkfun: http://www.sparkfun.com/products/241)
A box of baking soda, ideally the "fridge and freezer" kind with the ventilation panels on the side
Some "poster tack" sticky mounting putty
Some rubber bands

You'll need a soldering iron and some solder. A solderless breadboard is a great tool for prototyping circuits without making permanent connections. Let's get started.

Step 1: Prototype the Circuit

The theory of operation is simple - when your cat enters his or her litter box hideaway, he'll trip a simple breakbeam sensor and a timer will start, giving your cat enough time to do his "business." After the timer counts down three minutes, a fan will start and run for another three minutes, pushing air through the ventilated sides of the baking soda box and trapping les mal odeurs.

It helps to breadboard the circuit, first. A Fritzing mockup of the circuit is provided.  If you bought your IR emitter and phototransistor from Sparkfun, then it may not be obvious which is which; try plugging them into 5 volt DC power (from your Arduino, perhaps) in series with the 1k resistor and viewing them through a digital camera (such as your cell phone.) While IR is invisible to the unaided human eye, digital cameras can see it quite handily particularly if they are poorly filtered (as most are.) The IR LED is the one that lights up with a purple color. The phototransistor will not light up at all. Remember that an LED works only in one direction; with the anode connected to power and the cathode to ground. Typically, the long lead is the anode.

In this setup, three pins of the Arduino are used. Pin 3 provides power to the LED. Pin 2, as an input, detectes the presence or absence of the IR beam. Pin 5 supplies current to the "base" of the TIP120, which allows current to flow from the "collector" to the "emitter" and activating the fan's motor. Resistors in series with the LED and TIP120 prevent too much current from flowing through the delicate microprocessor. The resistor and the phototransistor form a voltage divider which will input to the Arduino in the presence of IR light.

Step 2: Program the Microcontroller

The Arduino team has made it easy to program your microcontroller from the Arduino IDE, which you'll need to download and install. Once you have, create a new "sketch" and copy the code from below, or download the CatTimer.ino Arduino sketch:

//-=-=-=-=-=-=-=-=-=-
#define SENSE_PIN 2
#define PULSE_PIN 3
#define FAN_PIN 5
#define FAN_DELAY 180000
#define FAN_RUN_TIME 180000

volatile boolean detectState;

void setup() {               
  pinMode(13, OUTPUT);
  pinMode(PULSE_PIN, OUTPUT);
  pinMode(SENSE_PIN, INPUT);
  pinMode(FAN_PIN, OUTPUT);
  digitalWrite(SENSE_PIN, LOW); 
}

void loop() {
  testBeam();
  delay(1000);

}


void testBeam(){
  /*
  On wake-up, pulse the IR emitter while listening for a LOW interrupt on the phototransistor.
  If it was LOW during the pulse, something blocked the beam, so activate the fan.
  */
  detectState = true;
  attachInterrupt(0, &beamInterrupt, CHANGE);
  digitalWrite(PULSE_PIN, HIGH);

  delay(200); //ms
  detachInterrupt(0);
  digitalWrite(PULSE_PIN, LOW);
  if (detectState){
    digitalWrite(13, HIGH);
    delay(FAN_DELAY);
    digitalWrite(FAN_PIN, HIGH);
    delay(FAN_RUN_TIME);
    digitalWrite(FAN_PIN, LOW);
    digitalWrite(13, LOW);
  }
  delay(20);
}

void beamInterrupt(){
  detectState = false;
}
//-=-=-=-=-=-=-=-=-=-=-

If you're using an Arduino Uno, Mini, or other USB-compatible Arduino, you need merely to plug it in, select the appropriate board in the Tools->Board menu, and click the "Upload" button. It should only take a few seconds. If your board has serial-style programming headers, you'll need to use an FTDI serial adapter instead. You're probably using a board based on the Duemillanove. If you're not sure what to select in Tools->Board, the maker of your board can probably tell you.

For prototyping purposes, you'll want to change the values for FAN_DELAY and FAN_RUN_TIME above to something much smaller. Remember that these values are in milliseconds (one-thousandth of a second). When you can break the beam and start the fan, change the program back to the above values or whatever best suits you (and your cat.)

Step 3: Build the Circuit

Once you've got the circuit up and running, it's time to transfer it to something more permanent than your solderless breadboard. There's a lot of options. Adafruit Industries makes protoboard with holes wired up just like solderless breadboards so you can transfer the circuit just as you made it, with the addition of some wires so that you can place the IR emitter and detector at the entrance of the litter box hideaway. Or you can use the simple perfboard available at Radio Shack and make the connections yourself by smearing solder between adjacent pads ("solder bridging"), using little runs of wire, or by bending the leads of the components themselves.

This is where your soldering skills will really come into play, since it'll be a challenge to tie all these parts together in the right way and in a small package. If you've never soldered before, this might not be the project for you to learn - start with any one of the Instructables on soldering things.

I decided to move my circuit to a Chibiduino, an inexpensive and tiny Arduino-compatible board from Brown Dog Gadgets. What I like about it is that there's just enough prototyping space on the board to solder in the circuit without having doodads hanging off the board. Now, that space comes with a disadvantage - there's no programming header on the board, so the only way to program it is to physically move a programmed Atmega328 to the Chibiduino's socket from another board. So I programmed a Kameduino, then carefully pried out the microprocessor and inserted it into the Chibiduino. (Remember to insert the IC the right way; line up the notch in the chip and in the socket.)

Step 4: Get Fresh

Use some rubber bands to hold it all together - you'll need to change the baking soda after a while. Use some poster tack to stick the sensor and emitter somewhere where the cat will trip it (but hopefully not trip ON it) on his way in.

That's all there is to it!

Step 5: Further Steps

In principle, this breakbeam sensor could be adapted to a lot of other things - an alarm or bell when someone steps through a doorway, turning the lights on when someone steps in a room, and so on. I've provided a simple sketch, but there's a lot of room for improvement - using the AVR "watchdog" library to automatically reset the processor if it hangs, or using the low-power modes so that the whole thing can run off of a battery instead of the wall. The great thing about the Arduino platform is that you've got a lot of options and it's easy to explore them. There's a lot of tools to help. If this is your first electronics project, congratulations! You've made something useful. Check out Fritzing for examples of other circuits you can build.