Automatic pet feeder with an Arduino Nano, an IR sensor and a servo flap

A dog, then a cat, walk up to the bowl. An infrared sensor sees the head, and the Arduino Nano swings a servo flap open to drop one portion of food, then closes it. One portion per visit.

  • 5 parts
  • 6 wiring steps
  • Arduino code
  • 5 V supply

Quick answer

The short version. Every pin is in the full wiring below.

What you need

  • Arduino Nano
  • FC-51 infrared obstacle sensor module
  • SG90 micro servo
  • Regulated 5 V 2 A supply
  • A hopper with a flap on the servo horn

How to wire it

  1. Supply + to the Nano 5V pin, the sensor VCC and the servo red lead.
  2. Supply minus to the Nano GND pin, the sensor GND and the servo brown lead.
  3. Sensor OUT to Nano D2.
  4. Nano VIN and USB are not used while the supply is connected.
  5. Set the sensor's trimmer so it sees the pet's head but not the bowl below it.
  6. If the Nano resets when the servo moves, add a 470 uF capacitor across the servo supply.

Before you power it: Everything runs on 5 V from a regulated supply. Keep the flap and servo out of reach of paws and noses while testing.

A yellow Pet Food Dispenser title above an Arduino Nano. Below it a clear hopper of brown kibble with a Wirecora label sits above a steel bowl, with a blue SG90 servo and a flap at the hopper outlet. A red and green FC-51 infrared sensor looks down beside the hopper, and a golden dog eats from the bowl. A 5 V 2 A supply and coloured wires join the Nano, sensor and servo
The circuit as wired in the Short. Every wire end is a real pin; the netlist was checked before the video went out.

Parts list

  • Arduino Nano (5 V)
  • FC-51 infrared obstacle sensor module (2 to 30 cm, output LOW while something is in front of it)
  • SG90 micro servo (4.8 to 6 V; brown = GND, red = +, orange = signal)
  • Regulated 5 V 2 A supply
  • A hopper with a flap on the servo horn, and a bowl

Wiring, step by step

  1. Supply + to the Nano 5V pin, the sensor VCC and the servo red lead.
  2. Supply minus to the Nano GND pin, the sensor GND and the servo brown lead.
  3. Sensor OUT to Nano D2. Servo orange lead (signal) to Nano D9.
  4. Nano VIN and USB are not used while the supply is connected.
  5. Set the sensor's trimmer so it sees the pet's head but not the bowl below it. Dark fur is seen from a shorter distance, and direct sunlight disturbs the sensor.
  6. If the Nano resets when the servo moves, add a 470 uF capacitor across the servo supply.

How it works

  1. The FC-51 pulls OUT LOW while something is within its range. The sketch reads D2 every 20 ms; when it sees LOW and has not fed this visit, it opens the flap from 0 to 80 degrees in 20 steps of 10 ms (0.20 s), waits 550 ms and closes it again.
  2. The flag fed stays true until OUT goes HIGH again, so a pet that stays gets only one portion, and the next pet gets its own.
  3. The flap sweep of 80 degrees in 0.20 s is 400 degrees per second, against the SG90's 600 degrees per second.
  4. The sensor's output is HIGH (5 V, through the module's 10 k pull-up) when clear and about 0.2 V when triggered. The Nano reads above 3.0 V as high and below 1.5 V as low, so both are clear.
  5. Supply current in the worst case, with the servo stalled, is about 0.71 A (servo about 0.65 A, Nano about 19 mA, sensor about 43 mA), against 2 A.

The code: pet_feeder.ino

The Arduino code for this circuit is in the Wirecora Discord, in the channel #arduino-code (owner’s choice, 14 October 2026: the code lives where you can ask about it). It is a plain .ino file you can download there.

The sketch the Short was animated from, run step by step in a simulation; it has not been run on a real board. It uses the standard Servo library.

Get the code in #arduino-code

What the video simplifies

  • The pets eat in about a second and take turns every 3 seconds.
  • The kibble starts and stops cleanly, and the stream ends a moment before the flap swings back. A real flap cuts through the kibble and gives rough portions; real feeders use an auger or a drum.
  • The hopper level does not fall.
  • The electronics are drawn much larger than the pets, the bowl and the hopper, and the sensor is drawn face-on although it looks down.
  • The sensor also has the bowl in its range, so it only works with the trimmer set between the head and the bowl.
  • Nothing limits how often a pet comes back.
  • This circuit was checked by datasheets and calculation, not built.

You found this by searching. Here's the whole path.

  1. Read how it works
  2. See it work
  3. Try it on the board
  4. Build it
  5. Show it
An Arduino Nano, an IR obstacle sensor and an SG90 servo on a 5 V supply
Next: see it work

Thirty seconds of video shows the circuit doing its job. Then this whole circuit is on the wiring board. Wire it there first and watch it work before you touch a real part.

Then build it

Everything runs on 5 V from a regulated supply. Keep the flap and servo out of reach of paws and noses while testing.

Then show it

Built it, or stuck halfway? Post a photo in the Wirecora Discord. Questions get an answer every day.