Arduino projects

Automatic pet feeding machine using Arduino

Today, we are going to make an automatic pet feeder based on Arduino development board, which can automatically provide food to your pet in time. This project uses a DS3231 RTC (Real Time Clock) module to set the time and date when the pet needs to be fed. So, according to your pet’s eating schedule, the device will automatically put down or fill the food.

 

In this project, we use a 1602 LCD module to display the time, which is obtained by using a DS3231 RTC module connected to an Arduino UNO. In addition, it uses a servo motor to rotate the container to provide food, and also provides a 4*4 matrix keypad to manually set the time to feed the pet. You can set the rotation angle and container opening time according to the amount of food you want to feed your pet. The amount of food may also vary slightly depending on whether your pet is a puppy, kitten or bird.

Required Materials

●    Arduino UNO development board

● 4 * 4 matrix keyboard

● LCD display module 1602

● One button

● Servo motor

● Resistance

● Connecting wire

● Breadboard

Circuit Schematic

In this Arduino based pet feeder project, to get the time and date, we have used RTC (Real Time Clock) module. We have used 4*4 matrix keypad along with LCD module 1602 to manually set the feeding time of the pet. The servo motor rotates the container and drops the food as per the time set by the user. LCD is used to display the date and time.

Automatic pet feeding machine using   Arduino

 

Automatic pet feeding machine using   Arduino

3D printed pet feeder model

We designed the container of this Arduino pet feeder using a 3D printer. You can also print the same design by downloading the file from here . The material used to print this model is PLA. It consists of four parts as shown in the following figure:

Automatic pet feeding machine using   Arduino

Assemble the four components and connect the servo motor as shown in the following diagram:

Automatic pet feeding machine using   Arduino

Introduction to DS3231 RTC Module

DS3231 is a RTC (Real Time Clock) module. In most of the electronic projects, it is used to maintain the date and time. This module has its own button battery which can be used to maintain the date and time even if the main power is removed or the MCU hardware is reset. So once we set the date and time in this module, it will always maintain it. In our circuit, we use DS3231 to feed the pet according to the time set by the pet owner, just like an alarm clock. When the clock reaches the set time, it operates the servo motor to open the container door and the food falls into the pet food bowl.

Automatic pet feeding machine using   Arduino

NOTE: When you use this module for the first time, you must set the date and time.

Code and Description

The complete Arduino code for the Automatic Pet Feeder is given at the end of this chapter.

Arduino has default library files for using servo motors and LCD module 1602. But for using RTC module DS3231 and 4*4 matrix keyboard with Arduino, you have to download and install these libraries. The download links of these two libraries are as follows:

● DS3231 RTC (Real Time Clock) module library

● 4 * 4 matrix keyboard library

 

In the following code, we defined the libraries, “#include <DS3231.h>” for the RTC module, “#include <Servo.h>” for the servo motor, “#include <LiquidCrystal.h>” for the 16*2 LCD, and “#include <Keypad.h>” for the 4*4 matrix keypad.

  1. #include <DS3231.h>
  2. #include <Servo.h>
  3. #include <LiquidCrystal.h>
  4. #include <Keypad.h>

Copy code

In the following code, we have defined the keymap for the 4*4 matrix keypad and assigned the Arduino pins to the rows and columns of the keypad.

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

Copy code

Here, we create the keyboard by using the following command in the code.

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

Copy code

We assign A4 and A5 of Arduino board to connect SCL and SDA pins of DS3231. Also, assign pins to LCD and initialize servo motor.

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

Copy code

In the following code, we declare t1 to t6, key and array r[6], and feed.

  1. int t1, t2, t3, t4, t5, t6;
  2. boolean feed = true;
  3. char key;
  4. int r[6];

Copy code

In the following code, we set up all the components for initialization. For example, in the code “servo_test.attach(10);” , the servo motor is connected to the 10th pin of the Arduino. A0, A1, and A2 are defined as output pins, and then the LCD and RTC modules are initialized.

  1. void setup()
  2. {
  3.   servo_test.attach(10); // attach the signal pin of servo to pin9 of arduino
  4.   rtc.begin();
  5.   lcd.begin(16,2);
  6.   servo_test.write(55);
  7.   Serial.begin(9600);
  8.   pinMode(A0, OUTPUT);
  9.   pinMode(A1, OUTPUT);
  10.   pinMode(A2, OUTPUT);
  11. }

Copy code

Now, the important part to understand is how the loop function works. Whenever the button is pressed, it goes to level 1 which can be read by “buttonPress = digitalRead(A3)” . Now it goes into the ‘if’ statement and calls the ‘ setFeedingTime  function. It then compares the real time with the time entered by the user. If the condition is true, i.e. the real time and the entered time are the same, then the servo motor rotates to 100 degrees and returns to its initial position after a 0.4 second delay.

  1. void loop() {
  2. lcd.setCursor(0,0);
  3. int buttonPress;
  4. buttonPress = digitalRead(A3);
  5. if (buttonPress==1)
  6. setFeedingTime();
  7. lcd.print(“Time: “);
  8. String t = “”;
  9. t = rtc.getTimeStr();
  10. t1 = t.charAt(0)-48;
  11. t2 = t.charAt(1)-48;
  12. t3 = t.charAt(3)-48;
  13. t4 = t.charAt(4)-48;
  14. t5 = t.charAt(6)-48;
  15. t6 = t.charAt(7)-48;
  16. lcd.print(rtc.getTimeStr());
  17. lcd.setCursor(0,1);
  18. lcd.print(“Date: “);
  19. lcd.print(rtc.getDateStr());
  20. if (t1==r[0] && t2==r[1] && t3==r[2] && t4==r[3]&& t5<1 && t6<3 && feed==true)
  21. {
  22.   servo_test.write(100); //command to rotate the servo to the specified angle
  23.    delay(400);  
  24.   servo_test.write(55);
  25.   feed=false;
  26. }
  27. }      

Copy code

In the void setFeedingTime() function code, after pressing the button, we can enter the feeding time of the pet and then we have to press ‘D’ to save the time. When the saved time matches the real time, the servo starts rotating.

  1. void setFeedingTime()
  2. {
  3.   feed = true;
  4.    int i=0;
  5.   lcd.clear();
  6.   lcd.setCursor(0,0);
  7.   lcd.print(“Set feeding Time”);
  8.   lcd.clear();
  9.   lcd.print(“HH:MM”);
  10.   lcd.setCursor(0,1);
  11.   while(1){
  12.     key = kpd.getKey();
  13.     char j;
  14.   if(key!=NO_KEY){
  15.     lcd.setCursor(j,1);
  16.     lcd.print(key);
  17.     r[i] = key-48;
  18.     i++;
  19.     j++;
  20.     if (j==2)
  21.     {
  22.       lcd.print(“:”); j++;
  23.     }
  24.     delay(500);
  25.   }
  26.   if (key == ‘D’)
  27.   {key=0; break; }
  28.   }
  29. }

Copy code

How an automatic pet feeder works

Automatic pet feeding machine using   Arduino

After uploading the code to the Arduino Uno development board, the LCD module will display the corresponding time and date. When you press the button, it will ask the pet’s feeding time, and you have to enter the time using the 4*4 matrix keypad. The display will show the time entered, and pressing the ‘D’ key can save the time. When the real time and the input time are consistent, the servo motor rotates from the initial position 55⁰ to 100⁰ and returns to the initial position again after a delay. Since the servo motor is connected to the food container door, when it moves, the door opens and some food falls into the bowl or plate. After a delay of 0.4 seconds, the servo motor rotates again and closes the gate. The whole process is completed in a few seconds. This is how your pet automatically gets food at the time you enter.

You change the time and rotation angle according to the amount of food. If you have problems making similar projects, please reply under this post.

Code

The complete code used in this article is as follows:  main.rar (1.21 KB, downloaded 476 times)

Back to top button