Robotic Cat Door: Sensor-Actuator example

CatWithFood.pngDescription: Activity to show how Ultrasonic Distance Sensor and Servo Work together in an invention.

For use prior to building the __YourDuino Basic Robot__ or as a Sensor-Actuator example.

Student Questions:
  • Does anyone here have a cat?
  • What type of food do you feed the cat? (Dried or canned)
  • Have you noticed that canned food dries out after a few hours and can get pretty yucky?


Our friend Wyatt has a cat named Rocky who doesn’t like his food to get all dried out. If the food is dry, he won’t eat it! Wyatt asked us to try to help him come up with an invention to keep his cat’s food fresh so he will eat it. He doesn’t like to waste cat food!

Hmmm, So, How does this need to work?

Maybe, we can make a container that has a door that closes when the cat’s not eating so the food doesn’t dry out.

SO, We need to:
  • Sense When the cat is nearby so the door will open
  • Sense When the cat goes away so the door will close

What device in the robot kit could help us sense if something is nearby?
  • To sense when the cat is nearby we can use the Ultrasonic Sensor

How will the door open and close? We need something that can turn. What device is in the Robot Kit that can turn?
  • The Servo can turn 180 degrees

Now we need to make design decisions. Can we create this using an Arduino? Definitely!

We used:
  • RoboRed Arduino Microcomputer
  • Servo
  • Ultrasonic Distance Sensor
  • Some wires
  • 3x5 Notecard for door
  • Software sketch: (You can copy and past it from the section below)
  • Hot glue
  • Base: we used a small square of plywood but cardboard would work fine
  • Shoe box (optional)
external image bPxHd5uJ07sZcN2lkoHFxx1OljaN7EM_VKf5nYcJLPk8Hp8A7fdAVXaPfFpK4yjkTfS0FYNibDZ-vX-2XZoPmy9PArba5UwmH0OTLKfLdwcxIe9yPORSav4GeZ7g2FQeIJgji5ty
  1. Connect wires from RoborRed to Ultrasonic and Servo according directions in the Sketch (see photos for information)
  2. Glue Servo to base using Hot Glue
  3. Attach one of the long white arms to the Servo
  4. Glue Ultrasonic to base using Hot Glue
  5. Glue door to Servo Arm using Hot Glue
  6. UPload Software Sketch to RoboRed Arduino
  7. Test your invention! Put your hand near the Ultrasonic: the door should open, when you move away, the door should close!
  8. To really create this Cat feeder, you could attach this automated door to a shoe box. (Optional)

Wire connection detail:
external image xa8Cg6OKcPd6LFZnelFa2kmcBFyJ2TmJeUSxAZefwnHaAGXstKHoO9dVYABmC2V2aZ8Zfz1bd6aX8y27B2G8m2EVCrYlTW0yhj56dAhvvEmWXuDVmnOgph067YklFl-SX7IBH926

A short example: part of the Arduino Software Sketch "Code" below:


Arduino Software Sketch (Copy/Paste into blank Arduino IDE window)

NOTE! This uses a library called NewPing. See it / get it HERE:
/* YourDuino Cat Feeder Example...
 - WHAT IT DOES: Looks for a Cat or something using Ultrasonic Sensor
                 Opens the catfood door if cat is closer than 15Cm
                 Closes the catfood door is the cat is gone for a short time
 - SEE the comments after "//" on each line below
 - CONNECTIONS: (suggest cable with 4 wires: Red,Orange,Yellow,Green )
 - Sensor Vcc to RoboRED     Vcc     (Red)
 - Sensor Ground to RoboRED  Gnd     (Green)
 - Sensor Trig to RoboRed    pin 2   (Orange)
 - Sensor Echo to RoboRED    pin 3   (Yellow
   - SERVO to Pin 11
 - V1.20 05/15/2016
   Questions: */

/*-----( Import needed libraries )-----*/
#include <Servo.h>
#include <NewPing.h>

/*-----( Declare Constants and Pin Numbers )-----*/
#define TRIGGER_PIN  2   // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     3   // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
#define DOOR_OPEN    90  // Angle for Open Door

#define SERVO_PIN 11 // Servo plugs into Pin 11

/*-----( Declare objects )-----*/
Servo myservo;  // create servo object to control a servo
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

/*-----( Declare Variables )-----*/
int pos;    // variable to store the servo position

unsigned int uS;
unsigned int DistanceCm;

int DoorIsOpen;

void setup()   /****** SETUP: RUNS ONCE ******/
  Serial.begin(9600); // Open serial monitor at 9600 baud to see ping results.
  myservo.attach(SERVO_PIN);  // attaches the servo on pin 11 to the servo object
  DoorIsOpen = 0;
  myservo.write(10);  //Door Start Position
}//--(end setup )---

void loop()   /****** LOOP: RUNS CONSTANTLY ******/
  delay(500);                      // Wait 500ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  uS =; // Send ping, get ping time in microseconds (uS).
  Serial.print("Ping: ");
  DistanceCm =  (uS / US_ROUNDTRIP_CM);
  if (DistanceCm == 0 ) DistanceCm = 88; // Fix if bad reading

  if (DistanceCm <= 15 and !DoorIsOpen)

  if ((DistanceCm >= 25) && (DoorIsOpen))

}//--(end main loop )---

/*-----( Declare User-written Functions )-----*/

int OpenDoor()
  for (pos = 10; pos <= DOOR_OPEN; pos += 5) // goes from 10 degrees to 160 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(100);                      // Wait 500ms between pings
  DoorIsOpen = 1;

int CloseDoor()
  for (pos = DOOR_OPEN; pos >= 10; pos -= 5)  // goes from 160 degrees to 10 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
  DoorIsOpen = 0;
/*********( THE END )***********/