Automatic Pet Feeder using Arduino

Arduino

ByPankaj Khatri 30

Automatic Pet Feeder using Arduino

Today we are building an Arduino based Automatic Pet Feeder which can automatically serve food to your pet timely. It has a DS3231 RTC (Real Time Clock) Module, which used to set time and date on which your pet should be given food. So, by setting up the time according to your pet’s eating schedule, the device drop or fill the food bowl automatically.

In this circuit, we are using a 16*2 LCD to display the time using DS3231 RTC Module with Arduino UNO. Also, a servo motor is used to rotate the containers to provide the food and 4*4 matrix keypad to manually set up the time for feeding the Pet. You can set the rotation angle and container opening duration according to the quantity of food you want to serve to your pet. The quantity of food may also depend upon your pet whether it’s a dog, cat or bird.

 

Material Required

  • Arduino UNO
  • 4*4 Matrix Keypad
  • 16*2 LCD
  • Push Button
  • Servo Motor
  • Resistor
  • Connecting Wires
  • Breadboard

 

Circuit Diagram

In this Arduino based Cat Feeder, for Getting Time and Date, we have used RTC (Real Time Clock) Module. We have used the 4*4 Matrix Keypad to set the Pet’s eating time manually with the help of 16x2 LCD. The Servo motor rotates the container and drop the food on the time set by the user. The LCD is used for displaying the Date and Time. Complete working can be found in the Video given at the end.

Automatic Pet Feeder circuit diagram using Arduino

Automatic Pet Feeder using Arduino circuit hardware

 

3D-Printed Pet Feeder Model

We have designed this Arduino Pet Feeder container using the 3D-printer. You can also print the same design by downloading the files from here. The material used for printing this model is PLA. It has four Parts as shown in the image below:

3D-printed Pet Feeder model Parts

Assemble the four parts and connect the Servo Motor as shown in the picture below:

3D-printed Pet Feeder model

If you are new to 3D printing here is the starting guide. You can download the STL files for this pet feeder here.

 

DS3231 RTC Module

DS3231 is a RTC (Real Time Clock) module. It is used to maintain the date and time for most of the Electronics projects. This module has its own coin cell power supply using which it maintains the date and time even when the main power is removed or the MCU has gone through a hard reset. So once we set the date and time in this module it will keep track of it always. In our circuit, we are using DS3231 to feed the pet according to the time, set up by the Pet’s owner, like an alarm. As, clock reaches to the set time, it operates the servo motor to open the container gate and the food drops in the Pet’s food bowl.

DS3231 RTC Module

Note: When using this module for the first time you have to set the date and time. You can also use RTC IC DS1307 for reading the time with Arduino.

 

Code and Explanation

Automatics Pet Feeder’s Complete Arduino Code is given at the end.

Arduino have default libraries for using the Servo motor and LCD 16*2 with it. But for using DS3231 RTC Module and 4*4 Matrix Keypad with the Arduino, you have to download and install the libraries. The download link for both the libraries is given below:

 

In the below code, we are defining libraries, “#include <DS3231.h>” for RTC module, “#include <Servo.h>” for Servo Motor, “#include <LiquidCrystal.h>” for 16*2 LCD, and “#include <Keypad.h>” for 4*4 Matrix Keypad.

#include <DS3231.h>
#include <Servo.h>
#include <LiquidCrystal.h>
#include <Keypad.h>

 

In the below code, we are defining the keymap for the 4*4 matrix keypad and assigning the Arduino pins for the Row and Columns of keypad.

char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte rowPins[ROWS] = { 2, 3, 4, 5 };
byte colPins[COLS] = { 6, 7, 8, 9 };

 

Here, we are creating the keypad by using the command below in the code.

Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

 

Assigning A4 and A5 Arduino pins to connect with SCL and SDA pins of DS3231. Also, assigning pins to the LCD and initializing the Servo motor.

DS3231  rtc(A4, A5);
Servo servo_test;      //initialize a servo object for the connected servo 
LiquidCrystal lcd(A0, A1, A2, 11, 12, 13); // Creates an LC object. Parameters: (rs, enable, d4, d5, d6, d7)

In the below code, we are declaring the t1 to t6, key, and array r[6], and the feed.

int t1, t2, t3, t4, t5, t6;
boolean feed = true;
 char key;
 int r[6];

 

In the below code, we are setting up all the components for the start. Like in this code “servo_test.attach(10);” Servo is attached to the  10th pin of the Arduino. Defining A0, A1 and A2 as the Output Pin and initializing LCD and RTC module.

void setup()
 {
  servo_test.attach(10);   // attach the signal pin of servo to pin9 of arduino
  rtc.begin();
  lcd.begin(16,2);
  servo_test.write(55);
  Serial.begin(9600);
  pinMode(A0, OUTPUT);
  pinMode(A1, OUTPUT);
  pinMode(A2, OUTPUT);
 }

 

Now, how the loop is working is the important part to understand. Whenever the Pushbutton is pressed, it goes high means 1, which can be read by “buttonPress = digitalRead(A3)”. Now it goes inside ‘if’ statement and calles the ‘setFeedingTime’ function. Then it compares the real time and the entered time by the user. If the condition is true which means the real time and the entered time is same, then the Servo motor rotates to and angle of 100 degree and after 0.4seconds of delay it comes back to its initial position.

void loop() {
lcd.setCursor(0,0);
int buttonPress;
buttonPress = digitalRead(A3);

if (buttonPress==1)
 setFeedingTime();
 lcd.print("Time:  ");
 String t = "";

 t = rtc.getTimeStr();
 t1 = t.charAt(0)-48;
 t2 = t.charAt(1)-48;
 t3 = t.charAt(3)-48;
 t4 = t.charAt(4)-48;
 t5 = t.charAt(6)-48;
 t6 = t.charAt(7)-48;

 lcd.print(rtc.getTimeStr());
 lcd.setCursor(0,1);
 lcd.print("Date: ");
 lcd.print(rtc.getDateStr());

 if (t1==r[0] && t2==r[1] && t3==r[2] && t4==r[3]&& t5<1 && t6<3 && feed==true)
 {
  servo_test.write(100);                   //command to rotate the servo to the specified angle
   delay(400);  
  servo_test.write(55);
  feed=false;
 }
 }      

 

In the void setFeedingTime() function code, After pressing the pushbutton we are able to enter the pet feeding time, then we have to Press ‘D’ to save that time. When the saved time matches with real time then servo start rotating.

void setFeedingTime()
{
  feed = true;
   int i=0;

  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Set feeding Time");
  lcd.clear();
  lcd.print("HH:MM");
  lcd.setCursor(0,1);
  while(1){
    key = kpd.getKey();
    char j;

  if(key!=NO_KEY){
    lcd.setCursor(j,1);
    lcd.print(key);
    r[i] = key-48;
    i++;
    j++;

    if (j==2)
    {
      lcd.print(":"); j++;
    }
    delay(500);
  }

  if (key == 'D')
  {key=0; break; }
  }
}

 

Working of the Automatic Pet Feeder

Automatic Pet Feeder using Arduino in action

After uploading the code to the Arduino Uno, the time and date will be displayed on the 16*2 LCD. When you pressed the pushbutton it asks for Pet’s feeding time and you have to enter the time using the 4*4 matrix Keypad. Display will show the entered time and as you press ‘D’ it saves the time. When the real time and the Entered time matches, it rotates the servo motor from its initial position 55⁰ to 100⁰ and after a delay again return to its initial position. Therefore, Servo motor is connected to the Food Container gate, so as it moves, the gate will open and some amount of food falls in the bowl or plate. After a delay 0.4 seconds Servo motor rotates again and close the gate. The whole process completes within a few seconds. This is how your pet get the food automatically on the time you entered.

 

Change time and degree according to food

Code

#include <DS3231.h>
#include <Servo.h>
#include <LiquidCrystal.h>
#include <Keypad.h>

const byte ROWS = 4; // Four rows
const byte COLS = 4; // Three columns

// Define the Keymap

char keys[ROWS][COLS] = {

  {'1','2','3','A'},

  {'4','5','6','B'},

  {'7','8','9','C'},

  {'*','0','#','D'}

};

// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.

byte rowPins[ROWS] = { 2, 3, 4, 5 };

// Connect keypad COL0, COL1 and COL2 to these Arduino pins.

byte colPins[COLS] = { 6, 7, 8, 9 };

//  Create the Keypad
  Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

 

DS3231  rtc(A4, A5);
Servo servo_test;      //initialize a servo object for the connected servo  
LiquidCrystal lcd(A0, A1, A2, 11, 12, 13); // Creates an LC object. Parameters: (rs, enable, d4, d5, d6, d7)

 //int angle = 0;    
// int potentio = A0;      // initialize the A0analog pin for potentiometer
 int t1, t2, t3, t4, t5, t6;

 
boolean feed = true; // condition for alarm

 char key;
 int r[6];
 
 void setup() 
 { 
  servo_test.attach(10);   // attach the signal pin of servo to pin9 of arduino
  rtc.begin();
  lcd.begin(16,2);
  servo_test.write(55); 
  Serial.begin(9600);
  pinMode(A0, OUTPUT);
  pinMode(A1, OUTPUT);
  pinMode(A2, OUTPUT);
  
 } 
 
 void loop() 
 { 

lcd.setCursor(0,0);
int buttonPress;
buttonPress = digitalRead(A3);

if (buttonPress==1)
 setFeedingTime();
 

//Serial.println(buttonPress);

 lcd.print("Time:  ");
 String t = "";
 t = rtc.getTimeStr(); 
 t1 = t.charAt(0)-48;
 t2 = t.charAt(1)-48;
 t3 = t.charAt(3)-48;
 t4 = t.charAt(4)-48;
 t5 = t.charAt(6)-48;
 t6 = t.charAt(7)-48;
 
 lcd.print(rtc.getTimeStr());
 lcd.setCursor(0,1);
 lcd.print("Date: ");
 lcd.print(rtc.getDateStr());
 
 if (t1==r[0] && t2==r[1] && t3==r[2] && t4==r[3]&& t5<1 && t6<3 && feed==true)
 { 
  servo_test.write(100);                   //command to rotate the servo to the specified angle 
   delay(400);   
  servo_test.write(55); 
  feed=false;
 } 
 }       

void setFeedingTime()
{
  feed = true;
   int i=0;

  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Set feeding Time");
  lcd.clear();
  lcd.print("HH:MM");
  lcd.setCursor(0,1);

  
  while(1){
    key = kpd.getKey();

    char j;
    
  if(key!=NO_KEY){
    
    lcd.setCursor(j,1);
    
    lcd.print(key);
    
    r[i] = key-48;
    i++;
    j++;

    if (j==2)
    {
      lcd.print(":"); j++;
    }
    delay(500);
  }

  if (key == 'D')
  {key=0; break; }
  }
}

Video

Comments

  • Nicole's picture
    Nicole
    Apr 03, 2018

    What an innovation. Salute your creativity :)

  • Gangadaran's picture
    Gangadaran
    Apr 22, 2018

    gave all the connections properly and display still shows black box without any letters

  • Hiro_Hamada's picture
    Hiro_Hamada
    Apr 24, 2018

    Adjust the contrast of the LCD. Also check if connections are made properly 

  • Havers's picture
    Havers
    Jul 19, 2018

    is that code is right?? i try to install it in arduino but its fail.

  • samj's picture
    samj
    Apr 25, 2018

    some of the codes i'm trying to upload but i get errors!!Pls help!!

  • Mark's picture
    Mark
    May 09, 2018

    You probably typed it in wrong. Check again.

  • Mark's picture
    Mark
    May 02, 2018

    Are there any more pictures of the set up that show the wiring in more detail? We tried to follow it exactly but our LCD is not working. Words are not showing up...

  • AISHA's picture
    AISHA
    May 03, 2018

    Check if the contrast levels are all fine

  • Hiro's picture
    Hiro
    May 04, 2018

    How do we do that?

  • Hiro's picture
    Hiro
    May 04, 2018

    It keeps displaying the stuff after the button is pressed without even pressing the button and then sometimes it just doesnt work at all.

  • Raphael's picture
    Raphael
    May 04, 2018

    What do I have to change in the code if I am using a I2C LCD Display instead of the normal 16 pin one like in this example?

  • Stalin's picture
    Stalin
    May 07, 2018

    Excuse me what meaning the number 48 has in setting the time.

  • mark's picture
    mark
    May 08, 2018

    can anyone upload a picture of the board and the file to their code?
    I've tried to wire everything according to the video. However, it is not working.

  • Morgan's picture
    Morgan
    May 08, 2018

    I'm sorry. We added a potentiometer to adjust the contrast of the LCD, but the words are still very light/cannot be seen. In addition, we cannot change the date. How do we fix this problem? Our project is due soon so any help is appreciated!! Thank you! :)

  • Anika's picture
    Anika
    May 08, 2018

    does anyone have a picture of their board and arduino with their wiring?

  • Morgan's picture
    Morgan
    May 08, 2018

    does anyone have a modified code?

  • Mark's picture
    Mark
    May 09, 2018

    Mr. Pankaj Khatri,
    Do you think you could upload a picture of the setup where we can clearly see where everything is going? The wiring diagram is different from the setup in the video. We aren't sure which to follow.

  • B.Aswinth Raj's picture
    B.Aswinth Raj
    May 11, 2018

    Follow the circuit digram guys, its the best way to get it done. Dont relay on wiring pictures. 

  • lincuna jemalyn's picture
    lincuna jemalyn
    Jun 18, 2018

    Some of the codes I'm trying to upload but i get errors...Pls help!!

  • mohd iqbal hakim's picture
    mohd iqbal hakim
    Jul 20, 2018

    why your data can't verify on arduino uno. have problem on your data please help me i need your help. i want to do this projek for my final year project

  • B.Aswinth Raj's picture
    B.Aswinth Raj
    Jul 23, 2018

    What problem are you having? only if you brief that people here might be able to help.

  • Carlos's picture
    Carlos
    Jul 28, 2018

    How could I put more "times"? like 2 meals

  • Martin's picture
    Martin
    Jul 28, 2018

    I need your help because when i write this code it has errors throught

  • Hiro_Hamada's picture
    Hiro_Hamada
    Aug 01, 2018

    People here can help you only if you brief them with your problems 

  • Emil's picture
    Emil
    Aug 08, 2018

    Can I program several feeding times a day, or only once?

  • AISHA's picture
    AISHA
    Aug 09, 2018

    You can easily tweak the Arduino code to feed several times a day

  • Kyaw Zin's picture
    Kyaw Zin
    Aug 29, 2018

    Please guide me how can I tweak the Arduino code to feed several times. I made a tweak and it's not working. That's why I need the guide line. Please help me to feed my cat with this. Please.

  • tom's picture
    tom
    Aug 31, 2018

    what are the values of required resistors

  • tom's picture
    tom
    Sep 09, 2018

    i can not see the colors clearly. what is the value of the resistors?
    thanks

  • B.Aswinth Raj's picture
    B.Aswinth Raj
    Sep 12, 2018

    These are just pull-up and pull-down resistors use 4.7K for both and will work fine 

Log in or register to post Comment