Home
Pet feeder
Drops one portion of dry food when you press its button, tap a web page, or at the two meal times you set.
- Hard
- about 90 min
- ESP32-S3
- 9 parts
- 12 wires
- 116 lines of code
- Uses Wi-Fi
ESP32 packs are built on real parts: the simulator runs Arduino Uno circuits only.
Idea
A stepper motor turns by an exact amount, so a portion is always the same size. The same job can be started three ways, which is a good habit: one function, many triggers.
Before you start
Dry food only. A feeder you built yourself can jam: it is a helper, not a substitute for checking that your pet has eaten.
What you learn
Driving a stepper motor, running something on a daily schedule, and a status light.
How it works
input
Feed-now button
decides
ESP32-S3 · Wi-Fi
output
Stepper motor
RGB LED
Powered with 5 V through the board's USB-C port.
It joins your home Wi-Fi. You type the network name and password into the sketch before uploading; they stay on the board.
A reference design. The sketch is compiled with the real ESP32 toolchain, and its pins are checked against the wiring table automatically. It has not yet been built and tested on real parts by us, so wire it on the bench and run the checks before you build it into a case. Real modules vary: follow the labels printed on yours, not their position in the picture.
Parts
0 of 9 ready
No prices here: they change by shop and by week. Buy each part by its name and the note beside it.
You also need
A computer with Arduino IDE 2 installed (free)
3D wiring
One jumper per step, with the USB cable unplugged. Several wires may share 3V3, 5V or GND: the expansion board or breadboard gives each its own pin.
Step 0 of 12
Lay out the parts
Seat the board on its expansion board or breadboard and lay the modules out round it. Nothing is connected yet.
All connections
pet-feeder.ino
// Pet feeder
// Turns a feed screw to drop one portion: when you press the button, from a
// web page, or at the two times of day you set.
#include <WiFi.h>
#include <WebServer.h>
#include <time.h>
#include <Stepper.h>
const char* WIFI_SSID = "YOUR_WIFI_NAME";
const char* WIFI_PASS = "YOUR_WIFI_PASSWORD";
const long UTC_OFFSET_S = 7 * 3600; // Vietnam: UTC+7
// Meal times, 24-hour clock.
const int MEAL_HOURS[] = {7, 18};
const int MEAL_MINUTES[] = {0, 0};
const int MEAL_COUNT = 2;
// The 28BYJ-48 takes 2048 steps for one full turn of its shaft.
const int STEPS_PER_TURN = 2048;
const int PORTION_STEPS = STEPS_PER_TURN / 2; // half a turn is one portion: adjust for your screw
const int PIN_MOTOR_IN1 = 4;
const int PIN_MOTOR_IN2 = 5;
const int PIN_MOTOR_IN3 = 6;
const int PIN_MOTOR_IN4 = 7;
const int PIN_BUTTON_A = 15;
const int PIN_RGB_R = 16;
const int PIN_RGB_G = 17;
const int PIN_RGB_B = 18;
// This motor wants its coils driven in the order IN1, IN3, IN2, IN4.
Stepper motor(STEPS_PER_TURN, PIN_MOTOR_IN1, PIN_MOTOR_IN3, PIN_MOTOR_IN2, PIN_MOTOR_IN4);
WebServer server(80);
int fedToday = 0;
int lastMealMinute = -1;
void light(int r, int g, int b) {
analogWrite(PIN_RGB_R, r);
analogWrite(PIN_RGB_G, g);
analogWrite(PIN_RGB_B, b);
}
void motorOff() {
// Leave no coil powered, or the motor gets warm for nothing.
digitalWrite(PIN_MOTOR_IN1, LOW);
digitalWrite(PIN_MOTOR_IN2, LOW);
digitalWrite(PIN_MOTOR_IN3, LOW);
digitalWrite(PIN_MOTOR_IN4, LOW);
}
void feed() {
light(140, 90, 0);
motor.step(PORTION_STEPS);
motorOff();
fedToday++;
light(0, 120, 0);
}
void sendPage() {
String html = "<!doctype html><meta name=viewport content='width=device-width,initial-scale=1'><title>Pet feeder</title>"
"<body style='font-family:sans-serif;max-width:24rem;margin:2rem auto;padding:0 1rem'><h1>Pet feeder</h1>";
html += "<p>Portions today: <b>" + String(fedToday) + "</b></p>";
html += "<form action=/feed method=post><button style='font-size:1.3rem;padding:.6rem 1.2rem'>Feed one portion</button></form></body>";
server.send(200, "text/html", html);
}
void setup() {
Serial.begin(115200);
pinMode(PIN_BUTTON_A, INPUT_PULLUP);
pinMode(PIN_RGB_R, OUTPUT);
pinMode(PIN_RGB_G, OUTPUT);
pinMode(PIN_RGB_B, OUTPUT);
motor.setSpeed(10); // turns per minute: this motor stalls much above 15
motorOff();
light(0, 0, 60);
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASS);
unsigned long start = millis();
while (WiFi.status() != WL_CONNECTED && millis() - start < 20000) delay(250);
configTime(UTC_OFFSET_S, 0, "pool.ntp.org", "time.google.com");
Serial.print("Open this address in a browser: http://");
Serial.println(WiFi.localIP());
server.on("/", sendPage);
server.on("/feed", HTTP_POST, []() {
feed();
server.sendHeader("Location", "/");
server.send(303);
});
server.begin();
light(0, 120, 0);
}
void loop() {
server.handleClient();
if (digitalRead(PIN_BUTTON_A) == LOW) {
delay(30);
while (digitalRead(PIN_BUTTON_A) == LOW) delay(5);
feed();
}
struct tm now;
if (getLocalTime(&now, 10)) {
int minuteOfDay = now.tm_hour * 60 + now.tm_min;
if (minuteOfDay != lastMealMinute) {
lastMealMinute = minuteOfDay;
if (minuteOfDay == 0) fedToday = 0;
for (int i = 0; i < MEAL_COUNT; i++) {
if (now.tm_hour == MEAL_HOURS[i] && now.tm_min == MEAL_MINUTES[i]) feed();
}
}
}
delay(5);
}
Libraries
- Stepper: install it from Sketch > Include Library > Manage Libraries.
Uploading
First time with an ESP32: in Arduino IDE open Boards Manager, search “esp32” and install “esp32 by Espressif Systems”.
- Before uploading, type your Wi-Fi name and password into WIFI_SSID and WIFI_PASS at the top of the sketch.
- Open the sketch in Arduino IDE.
- Plug the board in with the USB-C cable.
- Tools > Board > ESP32S3 Dev Module, then Tools > Port and choose the board's port.
- Press Upload (the arrow). Wait for “Done uploading”.