Build a Ventilated Cat Litter Box with Pi Zero
It’s hard for one to tie cats, those cute fluffy creatures, with the pungent smell from the waste they produce everyday. These two are my cats. While it means double the fun, the poop also doubles as well. To add insult to injury, our cats’ litter box is placed not far from our dining room, with open air circulation. Imagine while you are enjoying your lunch, there comes a surprise in the air… emmm.
So I thought to build a ventilation system on the litter box, a system with a fan that can be motion activated. When the cats are done with their businesses, the fan would start automatically, sucking up the surrounding air and pushing it out of the window.
The Raspberry Pi family quickly came to mind —the Pi Pico is a $5 micro controller board that is able to output signals based on inputs and logic. But I wanted to log the time my cats do businesses as well — who knows, the data may end up being interesting (a histogram of poop time?). So I decided to pick the slightly more expensive Pi Zero Wireless instead. The wireless version allows me to log into the Pi remotely and check the data, while still being cheap enough ($10) so if I happen to brick one, I can just get another.
A 80mm fan sounds like enough sucking power. I also bought a 12v power supply, a hvac air duct to connect to the nearby window, some PIR motion sensors, and a bunch of 1k resistors.
The fan requires a 12v input, but the Pi runs on only 5v. Thus the first challenge is how to power the Pi with the higher 12v. I found this video of building a voltage step down circuit. Maybe I’ll give it a try later, but I have some old USB car chargers laying around (they are also only $3 on ebay), so I’ll use one for the voltage step down instead.
The Pi also only emits 3.3v output voltage, that’s not compatible with the 12v circuit either. Through some research, I found out NPN transistors are often used to control the flow of a high voltage circuit with lower volts. The idea is that, the Pi outputs a 3.3v signal to the transistor, who then closes the main 12v circuit.
It also helps if you have a hot glue gun laying around — I used one to make the duct pipe air-tight, but duct tape also works (duh).
First is to install the Pi OS your Pi.
I mentioned earlier that I wanted to log the events, so I installed a database and its python client:
sudo apt install mariadb-server-10.0 python3-pippip3 install mariadb
I also installed the
gpiozero library to control my hardware, it encapsulates the hardware functionalities pretty well:
sudo apt install python3-gpiozero
Main Python Script
The main block of code for the python script is quite short. For each triggered event from the motion sensor, the output pin is kept ON for 10 min so the fan will stay on for that duration.
FAN_ON_DURATION = 10 * 60 # 10 min
outpin = LED(22)
pir = MotionSensor(27, sample_rate=1) # I used the GPIO pin 27while True:
Execute On Start Up
I also want my python script to run automatically when system starts up. For this, I created a custom systemd file. Since the script depends on mysqld daemon, make sure it’s executed after mysqld is started.
Description=Motion detection service for cat litter
ExecStart=/usr/bin/python3 -u motion_detection.py
This file needs to be renamed as
<your_service>.service and placed into the
/etc/systemd/system folder. Then enable the service at system start up with
sudo systemctl enable <your_service>.service
The entire code can be found on my github repo.
I created a simple wiring diagram to keep myself honest when connecting all the wires.
Tl;dr is that all ground wires need to be connected to the common GND. The fan and the transistor are connected in series, so the transistor can open or close the circuit for the fan. The output GPIO on the Pi is connected to the base of the transistor with a 333 ohm resistor (three 1k resistors connected in parallel), so the current flow (3.3 / 333 = ~10mA) will activate the transistor, and turn on the fan.
I took apart the USB car adapter. And with some soldering, I replaced the metal terminals with wires, and connected them into the main circuit. The 5v USB output is used to power the Pi.
Luckily, my cat litter box already came with a vent on top, and I made a cardboard box to cover it, then duct taped it onto the litter box to make it air tight. The motion sensor is aiming directly downwards.
Here’s how everything looks together.
It ended up working quite well. Even if I place my nose right outside the litter box, I can hardly smell anything bad when the fan is running .
Now I’m one step closer to fully enjoy my lunch — next is to figure out how to stop the cats from jumping on the tables trying to snatch a piece.