Automatic pet feeder using Arduino

Original text: https://circuitdigest.com/microcontroller-projects/automatic-pet-feeder-using-arduino

Automatic pet feeder using Arduino

ARDUINO

Pass by** Pankaj Khatri **April 3, 2018 56

Today, we are building an automatic pet feeder based on Arduino, which can automatically provide food for your pet in time. It has a DS3231 RTC (real-time clock) module, which is used to set the time and date when the pet should be fed. Therefore, by setting the time according to the pet's eating time, the device can automatically put down or fill the food bowl.

In this circuit, we use DS3231 RTC module with Arduino UNO 16 * 2 LCD To display the time. In addition, use servo motor Rotate the container to provide food and manually set the time for feeding the pet using the 4 * 4 matrix keyboard. You can set the rotation angle and container opening time according to the amount of food to be provided to your pet. The amount of food may also depend on your pet, whether it's a dog, cat or bird.

Materials required

  • Arduino UNO
  • 4 * 4 matrix keyboard
  • 16 * 2 LCD
  • Button
  • Servo motor
  • resistor
  • Connecting line
  • Bread board

Circuit diagram

In this Arduino based Cat Feeder, in order to obtain the time and date, we use the RTC (real-time clock) module. We use the 4 * 4 matrix keyboard to manually set the pet's meal time through the 16x2 LCD. The servo motor rotates the container and puts down the food at the time set by the user. The LCD is used to display the date and time. You can find the complete job in the video at the end.

3D printed pet feeder model

We designed the Arduino Pet Feeder container using a 3D printer. You can also Download files from here Print the same design. The material used to print this model is PLA. It is divided into four parts, as shown in the figure below:

Assemble the four parts, and then connect the servo motor as shown in the figure below:

If you are not familiar with 3D printing Here is the getting started guide . You can here download The pet feeder of STL file.

DS3231 RTC module

DS3231 is an RTC (real time clock) module. It is used to maintain the date and time of most electronic projects. The module has its own button battery power supply, which can be used to maintain the date and time even if the main power supply is disconnected or the MCU is hard reset. Therefore, once we set the date and time in this module, it will always track it. In our circuit, we are using DS3231 to feed pets according to the time set by the Pet owner (such as alarm). When the clock reaches the set time, it will operate the servo motor to open the container door, and the food will fall into Pet's food bowl.

**Note: * * when using this module for the first time, you must set the date and time. You can also use RTC IC DS1307 and Arduino read time.

Code and interpretation

Finally, the complete Arduino code of automatic pet feeder is given.

Arduino has a default library for using servo motors and LCD 16 * 2. However, to use the DS3231 RTC module and 4 * 4 matrix keyboard on Arduino, you must download and install the library. The download links of these two libraries are as follows:

In the following code, we define the library, "including < ds3231. H >" is used for RTC module "," including < servo. H > "is servo motor", "including < liquidcrystal. H >" 16 * 2 LCD, and * "#include < keypad. H >" * represents 4 * 4 matrix keyboard.

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

In the following code, we define the keyboard mapping for the 4 * 4 matrix keyboard and assign Arduino pins to the rows and columns of the keyboard.

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 create the keyboard by using the commands in the following code.

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

Assign A4 and A5 Arduino pins to connect with SCL and SDA pins of DS3231. In addition, assign the pins to the LCD and initialize 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 following code, we declare t1 to t6, key and array r [6], and feed.

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

In the following code, we will start setting up all components. Like the * "servo_test.attach (10);" in this code* The servo is connected to the 10th pin of Arduino. Define A0, A1 and A2 as output pins and initialize LCD and RTC modules.

void setup()
 {
  Servo_test.attach(10); //Connect the servo signal pin 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 loops work is an important part of understanding. Whenever the "push button" is pressed, it becomes higher, meaning that "1 *" can be read through "buttonPress = digitalRead (A3)". Now it enters the "if" statement and calls the "setFeedingTime" * function. It then compares the real-time time with the time entered by the user. If the condition is true, which means that the real-time time time is the same as the input time, the servo motor rotates to an angle of 100 degrees and returns to its initial position after a delay of 0.4 seconds.

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 button, we can enter the feeding time of the pet, and then we must press the "D" key to save the time. When the saved time is consistent with the real-time, the servo starts to rotate.

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; }
  }
}

Operation of automatic pet feeder


After uploading the code to Arduino Uno, the time and date will be displayed on the 16 * 2 LCD. When you press the button, it will ask the feeding time of the pet. You must enter the time using the 4 * 4 matrix keyboard. The display will show the entered time, which will save time when you press "D". When the real-time time and input time match, it will rotate the servo motor from its initial position 55 ⁰ to 100 °, and return to its initial position again after delay. Therefore, the servo motor is connected to the door of the food container, so as it moves, the door will open and some food will fall into the bowl or plate. After a delay of 0.4 seconds, the servo motor rotates again and closes the gate. The whole process will be completed in a few seconds. This is how your pet automatically gets food when you enter it.

Change the time and degree according to the 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; }
  }
}

Added by runnee on Sun, 20 Feb 2022 06:07:57 +0200