Make a Smart Automatic Pet Feeder with Arduino Uno

automatic pet feeder

automatic pet feeder


Do you ever get lazy feeding your pets? Sure, we love our pets, but just sometimes we wish we could make it automatic. Today we’ll make a smart automatic pet feeder using Arduino Uno! The key components of the project include an RTC module to track time and manage feeding schedule, distance sensor to monitor food level, light sensor to distinguish day/night, and RFID to identify our pet. Let’s get started!


  • Arduino Uno
  • Light sensor TEMT6000
  • Distance Sensor Sharp GP2Y0A21YK
  • RFID MFRC522
  • Buzzer
  • Motor SG90
  • RTC DS1307



  • Bottle (or any food container)
  • Metal Plate 35×25 cm

Step 1: Connecting the light sensor

We’ll be using SparkFun Ambient Light Sensor TEMT6000 to detect if it’s Day or Night. It’s important to distinguish the two in order to determine when and how often we release the food. TEMT6000 light sensor has 3 pins: SIG; GND; VCC. Wiring this sensor to the Arduino Board is quite simple: the VCC to the 5V pin; GND to board’s GND pin and the SIG needs to be connected to an Analog Input. I’ve chosen the A0 pin. The output pin SIG works as a transistor, so the greater the light applied near the sensor, the higher the voltage is on the output of the pin.

The graph below shows a relationship between the current and illuminance perceived by the TEMT6000. Illuminance is a measure of total luminous flux (i.e. visible light emitted by a source, measured in lm) divided by an area in m². Generally, 1 illuminance (Ix) = 1 Im/m². The TEMT6000 recognizes typical human visible spectrum with light wavelengths in the range of 390–700 nm.

TEMT6000 Datasheet:

automatic pet feeder

Figure 1: Collector light current vs Illuminance / ©Sparkfun



automatic pet feeder

Figure 2: Wire diagram of TEMT6000 light sensor and Arduino Uno


automatic pet feeder

Figure 3: Connection between TEMT6000 and Arduino Uno

Because this sensor is connected on an analog pin, the maximum value is 1023 due to the 10 bit resolution of the analog-to-digital converter. So for example, when the sensor is subjected on the highest light from my phone flash the value read from the sensor was around 1023.

automatic pet feeder

Figure 4: Arduino serial monitor showing the maximum values of the sensor

The code for the light sensor:


Step 2: Adding the distance sensor

To measure the distance I’ve chosen an analog sensor (Sharp GP2Y0A21YK) because it has the best results when compared to other distance sensors. The working principle of reflection is as follows: a signal is emitted and when it finds an obstacle in the way, it sends back also a signal  (a voltage value that varies depending on how near/far is the obstacle); this voltage needs to be converted into distance.

The position of GP2Y0A21YK will be above the food container, measuring the emptiness of the bottle (i.e. food level) before initiating the automatic feeder function. With this distance sensor, the system will detect if in the food container (or bottle) is full or empty. The ways this program will work:

  1. For a small distance: the automatic system will add only a small portion of food;
  2. For a medium distance: your pet will receive a half of portion of food;
  3. For a large distance: the automatic system will release full portion of food.

Here the distance implies the value from the place you mounted the distance sensor to the bottom of the food container. Because the range of the sensor is 10-80 cm, the sensor needs to be above the top of the food container with 10cm in order to read the correct value of the distance.

How to determine the best-fit line for the sensor:


The relationship between the sensor’s output voltage and the inverse of the measured distance is approximately linear over the sensor’s usable range. You can use this plot to convert the sensor output voltage to an approximate distance by constructing a best-fit line that relates the inverse of the output voltage (V) to distance (cm). In its simplest form, the linearizing equation can be that the distance to the reflective object is approximately equal to a constant scale factor (~27 V*cm) divided by the sensor’s output voltage. Adding a constant distance offset and modifying the scale factor can improve the fit of this line.



automatic pet feeder

Figure 5: The characteristic of the distance sensor / ©



Based on “typical values” from Sharp, the formula to translate SensorValue into Distance (the formula is only valid for a SensorValue between 80 to 500) is:

Distance (cm) = 4800/(SensorValue – 20)

This sensor can find the distance to objects that present a very narrow edge such as a wall at a very sharp angle.

Note: The output of this sensor will vary from unit to unit, and based on the characteristics of the target (reflectance, size, direction of motion, object alignment).



automatic pet feeder

Figure 6: Wire diagram of Sharp GP2Y0A21YK to Uno


automatic pet feeder

Figure 7: Connection between Sharp distance sensor, TEMT6000, and Uno


The code:

Step 3: Determine the real time

The time will be determined using the RTC DS1307 model. A real time clock is a system whose function is to determine real time. This circuit works on the base of a crystal oscillator with a frequency of 32.768 kHz. The principle is similar to the watches. An electronic oscillator function based on mechanical resonance of crystal vibration creates a precise frequency. This frequency is used for tracking date and time from the computer.

It’s a practical module because it has a battery that provides continuity even when the system is shut down.


automatic pet feeder

Figure 8: Wire diagram for RTC module



automatic pet feeder

Figure 9: Connection between the RTC, Sharp distance sensor, TEMT6000, and Uno

For an optimal usage, it is necessary to add 2 libraries for the module to work.

These 2 libraries are found on my

  • DS1307RTC
  • Time
  • Wire (this library is already included in the Arduino IDE and can be easily added)

We will run a test code in to check the module. When we upload the program to Arduino board, the serial monitor will show the current date and hour. These 2 libraries have an example code for finding date and hour, called “SetTime”.

Finding SetTime:

Click on Arduino IDE → File → Examples → DS1307RTC → SetTime

automatic pet feeder

Figure 10: Finding SetTime on Arduino IDE


In Figure 11, you will see that the module works correctly as the current date and time are displayed.

automatic pet feeder

Figure 11: Current date and time displayed correctly

For our application, we will only measure hour. To do this we need to extract from RTC module the exact hour. This will be done with the simple function of the RTC called “setSyncProvider(RTC.get)”. After implementing this function, you will be able to see the hour in serial monitor along with values from distance sensor and light sensor.


The code:


Figure 12: Hour displayed

Figure 12: Hour displayed


Continue Reading >