Introduction: DIY Smart Litter Box Sensor
If you have an indoor cat, you’re probably all too familiar with the litter box-changing process: You peek and sniff at the litter box, and if you encounter a grim sight or bad smell—it’s time to change the litter. Once you build your own smart litter box sensor, your cue becomes a blinking light and a text message. Much nicer, right?
Step 1: Parts, Tools & Supplies
For this project, you’ll need the following:
- Soldering iron & solder
- Wire strippers
- Heat shrink gun
- Laser cutter (Don’t have one? Try a vendor like this one, or look for one near you.)
- 3D printer, filament (Don’t have one? Try a vendor like this one, or look for one near you.
- Adafruit Huzzah Feather ESP8266 micro-controller
- Adafruit MiCS5524 Gas Sensor
- Micro USB cable (data-capable)
- Resistors <— These are so small and inexpensive you'll probably find the best deal at your local Radioshack (if it still exists!)
- 1 x 1K Ohm
- 1 x 330 Ohm
- 2 x 220 Ohm
- 2 x red LEDs
Step 2: Circuits & Code
Here's a diagram of the wiring that will live within your sensor's body, as well as the code that turns the gas sensor input into text message and LED outputs.
You'll need to download some key libraries and drivers to get your Huzzah Feather working with Arduino IDE and to ensure that you can connect to the cloud:
- Install the CP210x USB to UART Bridge VCP Drivers (so your board can talk to your USB port)
- Install the ESP8266 Board Package - Enter this URL into Additional Board Manager URLs field in the Arduino v1.6.4+ preferences
- Find and install the ESP8266 package in the 'Boards Manager' section of Arduino
- Also find and install the Adafruit IO Arduino and Adafruit MQTT Library packages in the 'Library Manager' too (they're what enable your Huzzah to connect to WiFi)
These pages are required reading:
- Adafruit's Huzzah Feather Overview - This page introduces you to the ins and outs of your board, explains how and why to install most of the components listed above, and features "blink" and "WiFi test" sketches that allow you to make sure you've got your board properly set up.
- Adafruit IO Basics Demo - While the example code at the bottom half of this post didn't work for me (appears to be based on outdated protocols and not work with this newer Huzzah board), the upfront provides helpful background on how your board connects to WiFi and also tells you how to set up an Adafruit.io account and feed, which is critical to getting your board to send messages. These are the two most important links from this helpful article:
- Adafruit IO join/setup page - You'll need to join the "beta" to create feeds and dashboards that can then trigger your IFTTT applets.
- IFTTT - This is where you set up the command protocol, or "applet," that will send a text, email--or a million other things--based on the output of your feed. My applet was "If data on feed is greater than 149, then send me an email at [my email address]"--but I'll leave you to create your own.
Now for the fun part--my code, and how to tweak it for yourself. This code is Frankesteined together from Arduino and Adafruit's examples (especially reliant on the adafruit_00_publish example that comes with one of the libraries mentioned above), as well as some help from my savvier friends. I doff my cap to you all!
First, here are the sections of code on the 'Config' tab that you'll need to update to match your personal Adafruit.IO and WiFi information:
#define IO_USERNAME "your IO username here"
#define IO_KEY "your IO key here"
#define WIFI_SSID "your SSID here"
#define WIFI_PASS "your WiFi password here"
You'll also find notes in that section that explain how you can connect to the cloud via other means, if you're so inclined. I just stuck with WiFi, myself!
Now, let's make sure that the sensor is set up to send a message (and light up) when you think it's too stinky. Below is an excerpt that includes the one part of the 'SmartLitterBox_example' code that you'll need to adjust, based on the smell thresholds in your litter box environment: the number 150.
// set up the 'gasSensor' feed
AdafruitIO_Feed *gasSensor = io.feed("litterbox-monitor-serial");
#define LED 13
const int analogPin = A0; // pin that the sensor is attached to
const int ledPin = 13; // pin that the LED is attached to
const int threshold = 150; // an arbitrary threshold level that's in the range of the analog input.
Basically, once your array is fully functioning, your board will be spitting out a numerical reading to its serial monitor every second. This number will change based on the concentration of stinky gas (in the instance of the litter box, that's ammonia) in the area. You may find that 150 is too high (or low) based on the atmosphere in your home, the smell preferences of you and your cat, etc.
NOTE: You'll also need to name your Adafruit.IO tab litterbox-monitor-serial. If you name it something else, be sure to make the corresponding change in the code (at the very first line under the '***example***' header).
Step 3: From Prototype to Soldered Masterpiece
If this is your first foray into the wild world of soldering, I highly recommend this soldering beginner's guide from Adafruit. It includes additional background on equipment, process and techniques that will be vital in creating a functioning product!
Before semi-permanently fusing all my components together with molten metal, I like to make sure they work in concert first! I accomplished this by first soldering simple breadboard wires to the 3V, Ground (GND), Analog (ADC), and GPIO #13 ports on my board. (NOTE: Pin #13 was an arbitrary choice. You could use any one of the numbered ports if you choose, but know that you'll have to update the corresponding spot in my code!)
Then, I recreated the circuit diagram above using the breadboard's solderless connections. I personally found this helpful when troubleshooting some of my trickier resistor levels, but the levels I found work for your LEDs too!
NOTE: You'll need to break in, or "calibrate," your MiCS5524 gas sensor before it will work for you. According to the instructions on the MiCS5524 overview page here (which I highly recommend you read through), that means running current through it for 24 hours.
Once you're satisfied with your set-up, it's time to get down to business. Treat the soldering guide linked above like your bible as you create lasting connections and cover key intersections with heat shrink tubing. And be conservative with your wire lengths, as they'll ultimately all need to sit within your external form.
Step 4: Form and Material
Following this step should be pretty easy if you have access to a high-end digital fabrication shop! If you don't, that's too bad—but if you google around for the most cost-effective shops in your regions, this shouldn't break the bank! Examples are linked up in step 1.
And remember, the star of this Instructable is really the circuit and code! My form is really just a suggestion, and I would love to see people's own designs for what a smart litter box sensor could look like! The main reason I chose 3D printing was so that I could create a shape that easily clasped together and snugly housed the gas sensor, Feather and LEDs. But to each his or her own! Below I have linked Adobe Illustrator and 3D model files that should work pretty seamlessly with most common 3D printing software and laser-cutters. Of course, you may need to modify (e.g. artboard size) on the idiosyncrasies of your own system—but I'll leave that up to you and your shop! Here's a look at my initial prototype, which was not designed at a scale that fit the Feather board or wiring. Oops!
Once you're all printed, cut and soldered, it's assembly time! I recommend approaching it in this order:
- Run the micro USB through the hole on the back first (NOTE: I had to cut off some of the plastic casing to do this, but you could buy a slenderer micro USB to skip this step)
- Push in the eye-screens (it should be a pressure fit, based on the 0.8" and 0.81" diameters of the hole and plexy circle, respectively--but this vary for you, based on laser cutter and 3D printer idiosyncrasies. Good thing you bought a whole sheet of plexy to mess around with, right?)
- Board (should be a good fit!)
- Sensor (slightly loose for me, but the wires can help hold it in place)
- LEDs and their wires
...then the last of the wires.
Step 5: We Made It, Baby!
With any luck, you're now the proud owner of a homemade cat-poop sniffer robot. I tip my cap to you!
I invite all of you to help take this project further. You could explore integrating a battery and some power-saving "sleep" functionality into the code, similar to this door sensor example. You could try different IFTTT applets to send messages to multiple parties. (Does your roommate or domestic partner always ignore the smell longer than you? Now you can both get the same email at the same time, and actually take turns!) Creating your own design for the exterior is great, especially if you figure out how to integrate it more elegantly with your litter box. And, hey, if you've got stories about how this dramatically improved your quality of life, I'm all ears!