Insect trap counter
An IR sensor across the funnel of a light trap counts the insects that fall through each night, and a Nano with a clock and a card writes the hourly totals, so a farmer can watch pest numbers rise and fall.
Build it step by step
0 of 7 doneWire the sensor
VCC to 5V, GND to GND, OUT to D2.
Wire the clock
SDA to A4, SCL to A5, power and ground.
Wire the card module
CS D10, MOSI D11, MISO D12, SCK D13, 5V and GND.
Wire the light
Red LED through 220 ohms to D6.
Install the library
RTClib.
Upload the sketch
The light stays off.
Wave a small object in front of the sensor
The light blinks and the Serial Monitor counts.
How do you build an insect trap counter?
An IR sensor across the funnel of a light trap counts the insects that fall through each night, and a Nano with a clock and a card writes the hourly totals, so a farmer can watch pest numbers rise and fall. You need Arduino Nano, IR obstacle avoidance sensor module, DS3231 RTC module, MicroSD Card Module, LED 5 mm red, Resistor 220 Ω, Jumper wires male to male (40 pcs, 20 cm) and Breadboard 830 Tie Points. It takes 7 steps and about 150 minutes.
- Level
- Intermediate
- Time
- About 150 min
- Parts
- 8
Parts list
- Development BoardsBuyArduino Nano × 1
- SensorsBuy
- SensorsBuy
- InterfacesBuy
- SemiconductorsBuyLED 5 mm red × 1
- PassivesBuyResistor 220 Ω × 1
- PrototypingBuy
- PrototypingBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| IR sensor VCC | Nano 5V | Power at 5 V |
| IR sensor GND | Nano GND | Common ground |
| IR sensor OUT | Nano D2 | Low when something passes in front |
| DS3231 SDA and SCL | Nano A4 and A5 | I2C, address 0x68 (VCC to 5V, GND to GND) |
| SD module CS | Nano D10 | Chip select |
| SD module MOSI, MISO, SCK | Nano D11, D12, D13 | SPI (VCC to 5V, GND to GND, use a 5 V module) |
| Red LED long leg | 220 ohm resistor to Nano D6 | Blinks on each count and stays on for a card problem |
| Red LED short leg | Nano GND | Return |
Code
A light trap uses a lamp to draw night-flying insects, which fall through a funnel into a container. Counting them each night shows whether a pest is increasing, and this helps decide when to treat the crop, which is the idea behind integrated pest management. An IR obstacle sensor sends out infrared light and sees it bounce back from anything in front, so each insect passing in front of it makes the output go low for a moment. The program counts each change from high to low, with a pause of 60 milliseconds so that one insect is not counted twice, and writes the total for each hour, with the date, to a file on the card when the hour changes. The clock chip keeps good time, and the sketch sets it to the time of upload the first time. It will not tell the kinds of insects apart, so count a sample by eye now and then to see how well it matches, and expect it to miss clusters and tiny insects. Bright sun or the trap lamp can confuse an IR module, so shade the sensor from the lamp and test it at night. Keep the electronics in a dry, shaded box. Do not touch a mains lamp or its wiring, and use a lamp made for the purpose, with its own safe supply.
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).
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <RTClib.h>
const int IR = 2, LED = 6, CS_PIN = 10;
RTC_DS3231 rtc;
unsigned int hourCount = 0;
int currentHour = -1;
bool lastState = HIGH;
unsigned long lastCount = 0;
void writeHour(DateTime t, unsigned int n) {
File f = SD.open("TRAP.CSV", FILE_WRITE);
if (!f) { digitalWrite(LED, HIGH); return; }
f.print(t.year()); f.print('-'); f.print(t.month()); f.print('-'); f.print(t.day()); f.print(' ');
f.print(t.hour()); f.print(F(":00,"));
f.println(n);
f.close(); // saved, even if the power goes after this
}
void setup() {
pinMode(IR, INPUT);
pinMode(LED, OUTPUT);
Serial.begin(9600);
if (!rtc.begin()) { digitalWrite(LED, HIGH); while (true) {} }
if (rtc.lostPower()) rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
if (!SD.begin(CS_PIN)) { digitalWrite(LED, HIGH); while (true) {} }
File f = SD.open("TRAP.CSV", FILE_WRITE);
if (f) {
if (f.size() == 0) f.println(F("hour,count"));
f.close();
}
currentHour = rtc.now().hour();
}
void loop() {
bool state = digitalRead(IR); // the sensor output goes low when something is in front
if (state == LOW && lastState == HIGH && millis() - lastCount > 60) {
hourCount++;
lastCount = millis();
digitalWrite(LED, HIGH);
delay(15);
digitalWrite(LED, LOW);
Serial.println(hourCount);
}
lastState = state;
static unsigned long lastCheck = 0;
if (millis() - lastCheck > 1000) { // check the clock once a second
lastCheck = millis();
DateTime now = rtc.now();
if (now.hour() != currentHour) {
writeHour(now - TimeSpan(0, 1, 0, 0), hourCount); // save the hour that has just ended
hourCount = 0;
currentHour = now.hour();
}
}
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Start | No light, no errors |
| Pass a finger in front of the sensor | Blink and count 1 |
| Pass it twice quickly | Two counts, not more |
| Wait for the next hour | A line in TRAP.CSV |
| Remove the card | Red light stays on |
If it does not work
Counts by itself
Shade the sensor from the lamp and from sun, and turn down its sensitivity.
Never counts
Check OUT on D2 and turn up the sensitivity screw.
Red light on
Check the card is FAT32 and wired to D10 to D13.
Wrong hour
Set the clock again, or re-upload.
File is empty
Wait for the hour to change, then remove the card.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the insect trap counter?
Arduino Nano, IR obstacle avoidance sensor module, DS3231 RTC module, MicroSD Card Module, LED 5 mm red, Resistor 220 Ω, Jumper wires male to male (40 pcs, 20 cm) and Breadboard 830 Tie Points.
How long does the insect trap counter take to build?
About 150 minutes for a intermediate maker, in 7 steps.
How do I know the insect trap counter works?
Start: No light, no errors. Pass a finger in front of the sensor: Blink and count 1. Pass it twice quickly: Two counts, not more.
Counts by itself: what should I check?
Shade the sensor from the lamp and from sun, and turn down its sensitivity.
Never counts: what should I check?
Check OUT on D2 and turn up the sensitivity screw.
Red light on: what should I check?
Check the card is FAT32 and wired to D10 to D13.
Wrong hour: what should I check?
Set the clock again, or re-upload.
