Build it step by step
0 of 7 doneSet the clock
Run RTClib's DS3231 example once to set the time.
Wire the sensor, clock and screen
OUT to D2, and the two I2C devices on A4 and A5.
Upload the sketch
The screen shows Visits today: 0.
Wave a hand in front of the sensor
It counts once after 200 milliseconds.
Keep the hand there
It does not count again until you move away for two seconds.
Mount the sensor at the perch
Pointing along it, shaded from the sun.
Keep the electronics dry
Inside a box with only the sensor lead out.
How do you build a bird feeder visit counter?
An infrared sensor at the perch of a garden bird feeder counts landings and shows how busy the feeder is through the day. No camera is used. It is a simple way to start looking at when birds feed, and keeping the electronics out of the rain is the main job. You need Arduino Nano, IR obstacle avoidance sensor module, SSD1306 OLED Display, DS3231 RTC module, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm). It takes 7 steps and about 80 minutes.
- Level
- Beginner
- Time
- About 80 min
- Parts
- 6
Parts list
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| IR sensor OUT | Nano D2 | Goes low when something is in front of it |
| IR sensor VCC and GND | Nano 5V and GND | Sensor power |
| RTC SDA and SCL | Nano A4 and A5 | I2C clock chip |
| OLED SDA and SCL | Nano A4 and A5 | Shares the same two I2C wires |
| RTC and OLED VCC and GND | Nano 5V and GND | Power |
| Power | USB power bank or lead from indoors | Keep the Nano, RTC and OLED in a dry box |
Code
The sensor looks along the perch, and when a bird sits there it pulls its output low. To avoid counting one visit many times, the sketch counts a visit only after something has been in front of the sensor for 200 milliseconds, and counts the next one only after the perch has been empty for two seconds. Each visit is added to the total and to the count for the current hour, taken from the DS3231 clock, and the screen shows the total, the count for this hour and the busiest hour. At midnight the counts reset. Install "RTClib", "Adafruit SSD1306" and "Adafruit GFX" from the Library Manager, and set the clock once with RTClib's example sketch. The screen and the clock share the same two I2C wires. The sensor module is not weatherproof either, so shade it from direct sun, which can set it off, and shelter it from rain. Keep the Nano, clock and screen in a dry box indoors or under a cover, with only the short sensor lead outside, and use a USB lead from indoors or a power bank. The sensor does not photograph or record the birds, only counts them. Tune the sensor's knob so it sees the perch but not the sky, and compare the count with a few minutes of watching to check it.
Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
// Libraries: RTClib, Adafruit SSD1306 and Adafruit GFX.
#include <Wire.h>
#include <RTClib.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
RTC_DS3231 rtc;
Adafruit_SSD1306 oled(128, 64, &Wire, -1);
const uint8_t IR_PIN = 2;
const unsigned long CONFIRM_MS = 200; // something must sit in front of the sensor this long
const unsigned long CLEAR_MS = 2000; // and the perch must be empty this long before the next count
uint16_t perHour[24];
uint16_t total = 0;
uint8_t today = 0;
bool occupied = false;
bool counted = false;
unsigned long since = 0;
unsigned long lastDraw = 0;
void draw(const DateTime &t) {
uint8_t busiest = 0;
for (uint8_t h = 1; h < 24; h++) if (perHour[h] > perHour[busiest]) busiest = h;
oled.clearDisplay();
oled.setTextSize(1);
oled.setTextColor(SSD1306_WHITE);
oled.setCursor(0, 0);
oled.print(F("Visits today: "));
oled.print(total);
oled.setCursor(0, 14);
oled.print(F("This hour: "));
oled.print(perHour[t.hour()]);
oled.setCursor(0, 28);
oled.print(F("Busiest hour: "));
if (total == 0) oled.print(F("-")); else oled.print(busiest);
oled.setCursor(0, 42);
if (t.hour() < 10) oled.print('0');
oled.print(t.hour());
oled.print(':');
if (t.minute() < 10) oled.print('0');
oled.print(t.minute());
oled.display();
}
void setup() {
pinMode(IR_PIN, INPUT);
Serial.begin(9600);
if (!rtc.begin()) Serial.println(F("RTC not found"));
oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
DateTime t = rtc.now();
today = t.day();
draw(t);
}
void loop() {
DateTime t = rtc.now();
if (t.day() != today) { // new day: start the counts again
today = t.day();
total = 0;
for (uint8_t h = 0; h < 24; h++) perHour[h] = 0;
}
bool seen = (digitalRead(IR_PIN) == LOW);
unsigned long now = millis();
if (seen != occupied) { // state changed, start timing it
occupied = seen;
since = now;
}
if (occupied && !counted && now - since >= CONFIRM_MS) {
counted = true;
total++;
perHour[t.hour()]++;
Serial.print(F("Visit at "));
Serial.print(t.hour());
Serial.print(':');
Serial.println(t.minute());
draw(t);
}
if (!occupied && counted && now - since >= CLEAR_MS) counted = false; // ready for the next bird
if (now - lastDraw > 1000) {
lastDraw = now;
draw(t);
}
}What the serial monitor should show
Open the serial monitor at the baud rate in the code. This is what our simulator prints for a virtual Arduino Nano with its sensors at their default values. Your numbers will follow your real readings, but the format should match.
RTC not found
Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Hand in front of the sensor | One visit counted |
| Hand held still | Still one visit |
| Hand away two seconds then back | A second visit |
| Screen | Total, this hour and the busiest hour |
| Midnight | Counts start again |
If it does not work
Counts every few seconds without a bird
Turn the sensor knob to reduce its range or shade it from the sun.
Never counts
Turn the knob up and check the OUT wire goes to D2.
RTC not found message
Check the I2C wires and the module's coin cell.
Blank screen
Check the OLED wires and try address 0x3D.
Wrong time
Set the clock again with the RTClib example.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the bird feeder visit counter?
Arduino Nano, IR obstacle avoidance sensor module, SSD1306 OLED Display, DS3231 RTC module, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm).
How long does the bird feeder visit counter take to build?
About 80 minutes for a beginner maker, in 7 steps.
How do I know the bird feeder visit counter works?
Hand in front of the sensor: One visit counted. Hand held still: Still one visit. Hand away two seconds then back: A second visit.
Counts every few seconds without a bird: what should I check?
Turn the sensor knob to reduce its range or shade it from the sun.
Never counts: what should I check?
Turn the knob up and check the OUT wire goes to D2.
RTC not found message: what should I check?
Check the I2C wires and the module's coin cell.
Blank screen: what should I check?
Check the OLED wires and try address 0x3D.
