# Scheduled pet feeder with a DS3231 clock and servo gate on Arduino: design worksheet and code

> A ₹1,360 design worksheet for a timed pet feeder with a DS3231 real-time clock and a servo gate, with the portion-from-time calibration, clock accuracy maths and an honest note on animal welfare.

Canonical: https://roboticsengineering.in/projects/scheduled-pet-feeder-ds3231-servo-arduino/ | Published: 2026-11-04 | Updated: 2026-11-04

**A ₹1,360 timed feeder that opens a gate at set times and lets dry food fall into a bowl. A DS3231 real-time clock keeps the time, the sketch opens the servo gate for a calibrated number of seconds, and the portion is worked out from how fast your food flows. The page covers the maths and, importantly, the failure cases, because an animal depends on this.**

**Scope:** intermediate. This is a design worksheet with illustrative costs. It has not been bench-tested by this site. The accuracy and portion figures below were computed by script. A hobby feeder can jam, run out of food or lose power, so it must never be the only way an animal is fed. Check on the animal and the feeder every day. The flow rate must be measured for your food and your gate; the figure used here is only an example.

## How it works

The DS3231 is a temperature-compensated clock commonly listed with an accuracy of about ±2 parts per million, which works out to roughly 63 seconds of error in a year (2 × 10⁻⁶ × 31,557,600 seconds). That is far better than the Arduino’s own timer and is why a separate clock chip is used. It has a coin cell, so the time survives a power cut.

Portion size is flow rate times open time. If your food flows at 12 grams a second through your gate, an open time of 2 seconds gives about 24 grams and 5 seconds about 60 grams. Measure the real figure by opening the gate for 5 seconds into a bowl on kitchen scales, several times, and use the average. Different foods flow differently, and a nearly empty hopper flows more slowly than a full one.

## Bill of materials

| Part | Qty | Unit | Total |
| --- | --- | --- | --- |
| Arduino Uno R3 (or compatible) | 1 | ₹550 | ₹550 |
| DS3231 real-time clock module with coin cell | 1 | ₹150 | ₹150 |
| Standard servo for the gate (MG995 class) | 1 | ₹250 | ₹250 |
| 5 V 2 A power supply | 1 | ₹150 | ₹150 |
| Food hopper, tube and bowl (bottle or container, hand-built) | 1 | ₹150 | ₹150 |
| Breadboard | 1 | ₹60 | ₹60 |
| Jumper wires | 1 | ₹50 | ₹50 |
| Parts total | ₹1,360 |  |  |

Scroll the table sideways to see every column.

Prices checked Nov 2026. Shipping and tools are not included.

Prices are illustrative figures for Indian sellers in November 2026, before shipping, and have not been checked against current listings.

## The numbers

| Gate open time | Portion at an example 12 g/s |
| --- | --- |
| 1 s | 12 g |
| 2 s | 24 g |
| 3 s | 36 g |
| 5 s | 60 g |
| 10 s | 120 g |

*Portion = flow rate × time. The 12 g/s flow is an example only; measure your own with scales.*

A clock error of about 63 seconds a year is not a problem for a feeding schedule. A bigger risk is the coin cell going flat, so the sketch refuses to feed if the clock reports it lost power, and the cell should be replaced every year or two.

## Power and wiring

| Part | Connects to | Note |
| --- | --- | --- |
| DS3231 SDA / SCL | A4 / A5 | I2C |
| DS3231 VCC and GND | 5V and GND |  |
| Servo signal | D9 |  |
| Servo power | Separate 5 V 2 A supply, common GND | The servo draws current spikes that can reset the Uno |

*Build the gate so a jam stalls without damage: a flap with a rubber stop is kinder to the servo than a rigid slider.*

## The sketch

```
// Scheduled pet feeder: Uno + DS3231 + servo gate
// Design worksheet: calibrate OPEN_MS with scales. Never rely on this alone.

#include <Wire.h>
#include <RTClib.h>
#include <Servo.h>

RTC_DS3231 rtc;
Servo gate;

const int GATE_PIN = 9;
const int CLOSED = 0, OPENED = 80;               // degrees; check your gate
const unsigned long OPEN_MS = 2000UL;            // set from your calibration

// Feeding times (24 h clock)
const int HOURS[]   = {7, 18};
const int MINUTES[] = {30, 30};
const int N_TIMES = 2;

long lastKey = -1;                               // minute of the last feed

void feed() {
  gate.write(OPENED);
  delay(OPEN_MS);
  gate.write(CLOSED);
}

void setup() {
  Serial.begin(9600);
  gate.attach(GATE_PIN);
  gate.write(CLOSED);
  if (!rtc.begin()) { Serial.println("No RTC"); while (true) {} }
  if (rtc.lostPower()) {
    Serial.println("RTC lost power: set the time and restart");
    // rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));   // run once to set the time
    while (true) {}                                       // refuse to run unattended
  }
}

void loop() {
  DateTime now = rtc.now();
  long key = now.day() * 1440L + now.hour() * 60L + now.minute();
  for (int i = 0; i < N_TIMES; i++) {
    if (now.hour() == HOURS[i] && now.minute() == MINUTES[i] && key != lastKey) {
      lastKey = key;
      feed();
    }
  }
  delay(1000);
}
```

The key prevents a second feed in the same minute. The sketch stops completely if the clock lost power, which fails safe to no food rather than random times; a person must then check the animal. To set the time the first time, uncomment the adjust line, upload, run once, then comment it out again and upload. The sketch has been proofread but not compiled on this site.

## Test plan

1. **Set and check the clock:** compare the printed time with your phone over a day.
2. **Gate alone:** test the gate without food and check it opens and closes fully.
3. **Calibrate:** open for 5 seconds into a bowl on scales three times and compute grams per second.
4. **Hopper level:** repeat with a full and a nearly empty hopper to see how the flow changes.
5. **Jam test:** deliberately block the gate with a few pellets and see how the servo behaves.
6. **Power cut:** unplug it for a minute, plug it back in and check the time is still right.

## What usually goes wrong

| Symptom | Likely cause | Fix |
| --- | --- | --- |
| Feeds at the wrong time | Clock not set or wrong time zone | Set the time, check against your phone |
| Gate jams | Pellets wedge in the gap | Widen the gap or round the flap edge |
| Portion too small | Hopper nearly empty or food changed | Recalibrate and keep the hopper topped up |
| Uno resets | Servo on the Uno supply | Use a separate 5 V supply |
| Does nothing at all | RTC reported lost power | Replace the coin cell and set the time |

## Upgrades

- **Weight feedback:** add a load cell to weigh the bowl after feeding.
- **Low-hopper sensor:** add an ultrasonic sensor to warn when food is low.
- **Alert:** use an ESP32 to message you if a feed fails.
- **Auger:** use a screw feeder for more consistent portions than a gate.

## Cost and parts notes

The ₹1,360 total is an illustration and does not include shipping. Use the [cost calculator](https://roboticsengineering.in/robotics-project-cost-calculator/) to compare sellers.

## Common questions

**How accurate is the DS3231?**

It is commonly listed at about ±2 ppm, which is about a minute of drift a year.

**Can I rely on it to feed my pet?**

No. It can jam or lose power. Check the animal and the feeder every day.

**How do I choose the portion?**

Ask a vet for the daily amount, split it across feeds, and calibrate the open time to match.

**Why refuse to run after a power loss?**

If the clock lost its time the schedule would be wrong, so the sketch stops and needs a person to fix it.

Last reviewed: November 2026. This is a design worksheet with illustrative costs. It has not been bench-tested by this site; library names, pins and limits must be checked against your board and the current library versions, and measurements from your build are welcome.
