From ArduinoInfo
Jump to navigation Jump to search

Robotic Cat Door: Sensor-Actuator example


Description: 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.

OK, but 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

Making Design Decisions

  • Can we create this using an Arduino? Definitely!

For this example, we used:

  • RoboRed Arduino Microcomputer
  • Servo
  • Ultrasonic Distance Sensor
  • Some wires
  • 3x5 Notecard for door
  • Software sketch: (You can copy and paste it from the section below)
  • Hot glue
  • Base: we used a small square of plywood but cardboard would work fine
  • Shoe box (optional)

Let's see what that would look like All Together!


Construction Steps

  1. Connect wires from RoboRed 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 (Use masking tape if you will build the Yourduino Robot with these same parts!)
  5. Glue door to Servo Arm using Hot Glue (Use masking tape if you will build the Yourduino Robot with these same parts!)
  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:


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); Serial.print(DistanceCm); Serial.println("cm"); if (DistanceCm == 0 ) DistanceCm = 88; // Fix if bad reading if (DistanceCm <= 15 and !DoorIsOpen) { Serial.println("CAT!!"); OpenDoor(); delay(5000); } if ((DistanceCm >= 25) && (DoorIsOpen)) { Serial.println("BYE"); CloseDoor(); delay(5000); } }//--(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' delay(100); } DoorIsOpen = 0; } /*********( THE END )***********/