Home/ Projects/ Servo gate or pet feeder
Project 9 of 16 Motion Intermediate

Servo gate or pet feeder

The SG90 holds a commanded angle rather than just spinning, which is what makes it useful for gates, dispensers and valves. You drive it with a PWM pulse whose width sets the position. Power it from the 5V rail rather than a logic pin — the stall current on a small servo is more than a GPIO can give, and a browning-out board that resets mid-move is the classic first failure here.

ESP32 & ESP8266 IoT Automation Kit
One of 16 projects in the ESP32 & ESP8266 IoT Automation Kit — the parts below are from that kit.

What this project uses

Every item is in the kit — nothing extra to buy.

Everything for this project

ESP32 & ESP8266 IoT Automation Kit

All 24 parts in one box, with this project and 15 more to build from them.

AED 180.00 See the kit
Need only these parts? Order them loose · from AED 107.58
Used in this project tick what you still need
Also needed untick if you already have them
Selected parts AED 107.58

One of each part. Packs (resistor assortment, jumper wires) already hold more than a single build needs. Backorder parts ship once they arrive — the kit above has every part boxed today. Delivery is free on orders AED 150.00+.

Sketch for this project

Written for the board in the kit. Copy it into the Arduino IDE; change the pin numbers if you wire it differently.

09 Servo gate or pet feeder Arduino C++
/*
  09 — Servo gate or pet feeder
  Board: ESP32.  Parts: SG90 servo, tactile switch (optional trigger).
  Library: "ESP32Servo" (the standard Servo library does not support the ESP32).

  Wiring:
    SG90:   brown -> GND, red -> VIN (5 V), orange (signal) -> GPIO 13
            Power it from the 5 V pin, never from a GPIO: a small servo can pull
            more than a pin can give, and a browning-out board that resets
            mid-move is the classic first failure here.
    Button: one leg -> GPIO 27, other leg -> GND (press = open the gate now)

  The SG90 HOLDS an angle rather than spinning: a 1–2 ms pulse every 20 ms
  sets the position. That is what makes it a gate, a dispenser or a valve.
*/

#include <ESP32Servo.h>

const int SERVO_PIN  = 13;
const int BUTTON_PIN = 27;

const int CLOSED_DEG = 10;
const int OPEN_DEG   = 100;
const unsigned long OPEN_FOR_MS   = 1500;    // how long the gate stays open
const unsigned long FEED_EVERY_MS = 6UL * 60UL * 60UL * 1000UL;   // 6 h timer (0 = button only)

Servo gate;
unsigned long lastFeed = 0;

void openGate() {
  Serial.println("open");
  for (int a = CLOSED_DEG; a <= OPEN_DEG; a += 2) { gate.write(a); delay(10); }   // slow = quiet
  delay(OPEN_FOR_MS);
  for (int a = OPEN_DEG; a >= CLOSED_DEG; a -= 2) { gate.write(a); delay(10); }
  Serial.println("closed");
}

void setup() {
  Serial.begin(115200);
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  gate.setPeriodHertz(50);              // standard 50 Hz servo signal
  gate.attach(SERVO_PIN, 500, 2400);    // SG90 pulse range in microseconds
  gate.write(CLOSED_DEG);
  lastFeed = millis();
}

void loop() {
  if (digitalRead(BUTTON_PIN) == LOW) {          // manual trigger
    openGate();
    lastFeed = millis();
    delay(300);                                  // crude debounce, fine here
  }
  if (FEED_EVERY_MS > 0 && millis() - lastFeed >= FEED_EVERY_MS) {
    openGate();
    lastFeed = millis();
  }
}
Install: ESP32Servo. Servo power from the 5 V pin, never from a GPIO.

More from this kit

All projects →

Root
Online — usually replies instantly
Catalog assistant: verify final stock/price on the product page before checkout.