Build it step by step
0 of 7 doneBuild the chassis
Motors, driver and battery pack.
Test driving
Forward, back and turns.
Add the obstacle turn
Ultrasonic sensor and random turns.
Add the tilt stop
Motors off when lifted.
Build the treat flap
Servo and a tiny hopper.
Add the timing
One treat every 20 minutes.
Play test
With your cat and supervision.
How do you build a roaming pet toy robot?
A small robot wanders and drops a treat, avoiding walls, to entertain a cat. You need Arduino Nano, HC-SR04 Ultrasonic Sensor, TB6612FNG Motor Driver, BO motor straight 100 RPM × 2, Robot car chassis kit 2WD (acrylic), SG90 Micro Servo, Battery holder 4 × AA with switch, AA alkaline battery × 4, Tilt sensor (ball switch, SW-520D) and Active buzzer 5 V. It takes 7 steps and about 150 minutes.
- Level
- Intermediate
- Time
- About 150 min
- Parts
- 10
Wiring
| From | To | Why |
|---|---|---|
| HC-SR04 TRIG and ECHO | Nano D12 and D13 | Obstacle sensor |
| HC-SR04 VCC and GND | Nano 5V and GND | Power |
| TB6612 PWMA, AIN1, AIN2 | Nano D5, D7, D8 | Left motor |
| TB6612 PWMB, BIN1, BIN2 | Nano D6, D9, D10 | Right motor |
| TB6612 STBY | Nano D11 | Driver enable |
| Servo signal | Nano D3 | Treat flap |
| Tilt switch | Nano D4 and GND | Pick-up detector |
| Buzzer | Nano D2 and GND | Sound |
| Battery pack plus | TB6612 VM, Nano VIN and servo plus | 6 V supply |
Code
The robot drives forward with a small random wobble, and when the ultrasonic sensor sees something within 20 cm it backs up and turns a random way for a random time, so it never follows the same path. Every 20 minutes it stops, chirps and tips a treat out of a hopper through a servo flap; keep the hopper very small, so a stuck mechanism can never overfeed your pet. If the tilt switch opens because the robot was lifted or has tipped over, the motors stop at once. Use only pet-safe materials, with no small parts a cat can pull off, cover wires, and keep the speed low; the cat should never be able to reach the wheels or batteries. Supervise play and put the robot away afterwards. Cats may find the sound or movement stressful, so introduce it slowly and stop if your pet seems upset. Arduino IDE, board "Arduino Nano" (try the Old Bootloader option if the upload fails).
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).
// Roaming pet robot: wanders, turns away from obstacles, drops a treat now and then, stops when lifted. Board: Arduino Nano.
#include <Servo.h>
Servo flap;
const int TRIG = 12, ECHO = 13, TILT = 4, BUZ = 2;
const int PWMA = 5, AIN1 = 7, AIN2 = 8, PWMB = 6, BIN1 = 9, BIN2 = 10, STBY = 11;
const unsigned long TREAT_EVERY = 20UL * 60UL * 1000UL; // one treat every 20 minutes
unsigned long lastTreat = 0;
void drive(int l, int r) {
digitalWrite(AIN1, l > 0); digitalWrite(AIN2, l < 0); analogWrite(PWMA, abs(l));
digitalWrite(BIN1, r > 0); digitalWrite(BIN2, r < 0); analogWrite(PWMB, abs(r));
}
long distanceCm() {
digitalWrite(TRIG, LOW); delayMicroseconds(3);
digitalWrite(TRIG, HIGH); delayMicroseconds(10); digitalWrite(TRIG, LOW);
long us = pulseIn(ECHO, HIGH, 20000);
return us ? us / 58 : 200;
}
void setup() {
int pins[] = {PWMA, AIN1, AIN2, PWMB, BIN1, BIN2, STBY, TRIG, BUZ};
for (int i = 0; i < 9; i++) pinMode(pins[i], OUTPUT);
pinMode(ECHO, INPUT); pinMode(TILT, INPUT_PULLUP);
digitalWrite(STBY, HIGH);
flap.attach(3); flap.write(0);
randomSeed(analogRead(A0));
}
void loop() {
if (digitalRead(TILT) == HIGH) { drive(0, 0); return; } // lifted or tipped: stop at once
if (millis() - lastTreat > TREAT_EVERY) {
drive(0, 0); lastTreat = millis();
tone(BUZ, 1200, 200); delay(300);
flap.write(70); delay(600); flap.write(0); // flap opens and a treat rolls out
}
long d = distanceCm();
if (d < 20) { // wall ahead: back up and turn a random way
drive(-130, -130); delay(400);
if (random(2)) drive(140, -140); else drive(-140, 140);
delay(random(300, 700));
} else {
drive(130 + random(-20, 20), 130 + random(-20, 20)); // gentle wobble so the path varies
delay(150);
}
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Open floor | Wanders with a varying path |
| Wall ahead | Backs up and turns |
| Picked up | Motors stop |
| Treat time | Chirp and one treat drops |
If it does not work
Spins on the spot
Check the motor wiring direction.
Hits walls
Check the sensor aim and raise the 20 cm distance.
Flap stuck
Adjust the servo angle.
Does not stop when lifted
Check the tilt switch orientation.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the roaming pet toy robot?
Arduino Nano, HC-SR04 Ultrasonic Sensor, TB6612FNG Motor Driver, BO motor straight 100 RPM × 2, Robot car chassis kit 2WD (acrylic), SG90 Micro Servo, Battery holder 4 × AA with switch, AA alkaline battery × 4, Tilt sensor (ball switch, SW-520D) and Active buzzer 5 V.
How long does the roaming pet toy robot take to build?
About 150 minutes for a intermediate maker, in 7 steps.
How do I know the roaming pet toy robot works?
Open floor: Wanders with a varying path. Wall ahead: Backs up and turns. Picked up: Motors stop.
Spins on the spot: what should I check?
Check the motor wiring direction.
Hits walls: what should I check?
Check the sensor aim and raise the 20 cm distance.
Flap stuck: what should I check?
Adjust the servo angle.
Does not stop when lifted: what should I check?
Check the tilt switch orientation.
