Build it step by step
0 of 7 doneWire the sensor
TRIG to D9, ECHO to D10, 5V and GND.
Wire the OLED and button
OLED to A4, A5, 5V and GND, button to D2 and GND.
Upload with the entrance clear
The screen shows the empty distance.
Hold your hand in front
For a second, and the count goes to 1.
Wait a minute
And try again for a second visit.
Mount the sensor
Across the entrance, out of reach.
Hold the button
For two seconds to reset each morning.
How do you build a litter tray visit counter?
A distance sensor above the edge of a cat's litter tray counts visits and shows how many there were today, with no camera and no picture taken. A change in the usual number can be a reason to mention it to a vet. It is a counter only and not a medical device. You need Arduino Nano, HC-SR04 Ultrasonic Sensor, SSD1306 OLED Display, Tactile push button 6 mm, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm). It takes 7 steps and about 70 minutes.
- Level
- Beginner
- Time
- About 70 min
- Parts
- 6
Parts list
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| HC-SR04 VCC and GND | Nano 5V and GND | Sensor power |
| HC-SR04 TRIG | Nano D9 | Trigger pulse |
| HC-SR04 ECHO | Nano D10 | Echo pulse width |
| OLED SDA and SCL | Nano A4 and A5 | I2C data and clock |
| OLED VCC and GND | Nano 5V and GND | Power for a 0.96 inch module |
| Button | Nano D2 and GND | Hold for two seconds to reset the count |
Code
The distance sensor looks across the tray's entrance. When the sketch starts it measures the empty distance ten times and averages it as the baseline, so start it with the entrance clear. After that, anything more than 10 centimetres closer than the baseline counts as present. To avoid counting twitches, something must stay for one second to count as a visit, and visits within a minute of the last are ignored, so a cat that steps in and out several times is one visit. The screen shows the day's count and the baseline, and holding the button for two seconds resets the count and measures the baseline again, which is what you do each morning. The count is lost if the power is cut. This is a counter only, with no camera and no recording. It is not a medical device: a sudden change in the usual number of visits is only something to mention to your vet, who will decide whether it matters. Keep the electronics out of reach of the cat and away from any litter dust or moisture, and fix the sensor so it cannot be knocked into the tray. Install "Adafruit SSD1306" and "Adafruit GFX" from the Library Manager.
Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
// Libraries: Adafruit SSD1306 and Adafruit GFX.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 oled(128, 64, &Wire, -1);
const uint8_t TRIG = 9;
const uint8_t ECHO = 10;
const uint8_t BTN = 2;
const int MARGIN_CM = 10; // something this much closer than the empty reading counts
const unsigned long PRESENT_MS = 1000; // must be there this long to count as a visit
const unsigned long LOCKOUT_MS = 60000UL; // ignore repeats for a minute after a visit
int baseline = 0;
unsigned int visits = 0;
bool present = false;
unsigned long presentSince = 0;
unsigned long lastVisit = 0;
bool counted = false;
unsigned long holdStart = 0;
unsigned long lastDraw = 0;
int readCm() {
digitalWrite(TRIG, LOW);
delayMicroseconds(2);
digitalWrite(TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG, LOW);
unsigned long us = pulseIn(ECHO, HIGH, 30000UL);
if (us == 0) return 400; // no echo, treat as far away
return us / 58;
}
int calibrate() {
long sum = 0;
for (uint8_t i = 0; i < 10; i++) { sum += readCm(); delay(60); }
return sum / 10;
}
void draw() {
oled.clearDisplay();
oled.setTextColor(SSD1306_WHITE);
oled.setTextSize(1);
oled.setCursor(0, 0);
oled.print(F("Visits today"));
oled.setTextSize(3);
oled.setCursor(0, 16);
oled.print(visits);
oled.setTextSize(1);
oled.setCursor(0, 50);
oled.print(F("Empty: "));
oled.print(baseline);
oled.print(F(" cm"));
oled.display();
}
void setup() {
pinMode(TRIG, OUTPUT);
pinMode(ECHO, INPUT);
pinMode(BTN, INPUT_PULLUP);
Serial.begin(9600);
oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
oled.clearDisplay();
oled.setTextColor(SSD1306_WHITE);
oled.setCursor(0, 0);
oled.print(F("Keep the entrance empty"));
oled.display();
delay(1500);
baseline = calibrate();
Serial.print(F("Empty reading: "));
Serial.println(baseline);
draw();
}
void loop() {
int d = readCm();
bool seen = (d < baseline - MARGIN_CM);
unsigned long now = millis();
if (seen && !present) { present = true; presentSince = now; counted = false; }
if (!seen && present) { present = false; }
if (present && !counted && now - presentSince >= PRESENT_MS) {
if (lastVisit == 0 || now - lastVisit >= LOCKOUT_MS) {
visits++;
lastVisit = now;
Serial.print(F("Visit "));
Serial.println(visits);
draw();
}
counted = true; // one count for each time something stays
}
if (digitalRead(BTN) == LOW) { // hold for two seconds to reset
if (holdStart == 0) holdStart = now;
if (now - holdStart > 2000) { visits = 0; holdStart = 0; baseline = calibrate(); draw(); }
} else {
holdStart = 0;
}
if (now - lastDraw > 2000) { lastDraw = now; draw(); }
delay(60);
}What the serial monitor should show
Open the serial monitor at the baud rate in the code. This is what our simulator prints for a virtual Arduino Nano with its sensors at their default values. Your numbers will follow your real readings, but the format should match.
Empty reading: 80
Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Start-up | Shows an empty distance in centimetres |
| Hand held for a second | One visit |
| Another visit within a minute | Not counted |
| Hold the button two seconds | Count resets |
| Serial monitor | Prints each visit |
If it does not work
Counts nothing
Check TRIG and ECHO and that the entrance is wider than the sensor's beam.
Counts without a visit
Check nothing is near the baseline path and raise MARGIN_CM.
Empty distance shows 400
The sensor sees no echo, check wiring and aim.
Blank screen
Check the OLED wires and try address 0x3D.
Count wrong after a power cut
The count is not saved, reset it.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the litter tray visit counter?
Arduino Nano, HC-SR04 Ultrasonic Sensor, SSD1306 OLED Display, Tactile push button 6 mm, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm).
How long does the litter tray visit counter take to build?
About 70 minutes for a beginner maker, in 7 steps.
How do I know the litter tray visit counter works?
Start-up: Shows an empty distance in centimetres. Hand held for a second: One visit. Another visit within a minute: Not counted.
Counts nothing: what should I check?
Check TRIG and ECHO and that the entrance is wider than the sensor's beam.
Counts without a visit: what should I check?
Check nothing is near the baseline path and raise MARGIN_CM.
Empty distance shows 400: what should I check?
The sensor sees no echo, check wiring and aim.
Blank screen: what should I check?
Check the OLED wires and try address 0x3D.
