Gal Ofel
Published

Eco-Friendly Automated Mosquito Trap - Arduino Nano

Garden pool attracts mosquitoes to lay eggs, then auto-flushes before they hatch. No chemicals - just an Arduino, a pump, and a timer.

BeginnerFull instructions provided1 hour53
Eco-Friendly Automated Mosquito Trap - Arduino Nano

Things used in this project

Hardware components

Arduino Nano Every
Arduino Nano Every
×1
DS3231MPMB1 Peripheral Module
Maxim Integrated DS3231MPMB1 Peripheral Module
×1
Solar Power Manager 5V
DFRobot Solar Power Manager 5V
×1
Battery, 3.7 V
Battery, 3.7 V
×1
Seeed Studio 5V Relay module (digital) Electronic Brick
×2

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
Wire Stripper & Cutter, 18-10 AWG / 0.75-4mm² Capacity Wires
Wire Stripper & Cutter, 18-10 AWG / 0.75-4mm² Capacity Wires
Mastech MS8217 Autorange Digital Multimeter
Digilent Mastech MS8217 Autorange Digital Multimeter
Multitool, Screwdriver
Multitool, Screwdriver

Story

Read more

Schematics

Wire Diagram

Code

mosquito_trap.ino

C/C++
// Mosquito Trap Automation by Make-It
// This code uses a DS3231 Real-Time Clock (RTC) to trigger a water change every 4 days.

#include <Wire.h>
#include "RTClib.h"

// Create an RTC object
RTC_DS3231 rtc;

// --- Pin Definitions ---
#define EMPTY_PUMP_PIN 7  // Digital pin connected to IN1 on the relay module
#define REFILL_PUMP_PIN 8 // Digital pin connected to IN2 on the relay module

// --- Configuration ---
const long CYCLE_INTERVAL_SECONDS = 345600; // 4 days (4 * 24 * 60 * 60 seconds)
const int EMPTY_PUMP_DURATION_MS = 60000;   // Run empty pump for 60 seconds
const int REFILL_PUMP_DURATION_MS = 60000;  // Run refill pump for 60 seconds

// We need to store the time of the last cycle. We'll use a variable for this.
// For a real-world device, this should be stored in EEPROM to survive power loss.
long lastCycleTime = 0;

void setup() {
  Serial.begin(9600);
  Wire.begin();

  pinMode(EMPTY_PUMP_PIN, OUTPUT);
  pinMode(REFILL_PUMP_PIN, OUTPUT);

  // Make sure pumps are off at startup
  digitalWrite(EMPTY_PUMP_PIN, HIGH); // Relays are often LOW-triggered, so HIGH is OFF
  digitalWrite(REFILL_PUMP_PIN, HIGH);

  if (!rtc.begin()) {
    Serial.println("Couldn't find RTC");
    Serial.flush();
    abort();
  }

  // UNCOMMENT THE FOLLOWING LINE THE *FIRST* TIME YOU UPLOAD
  // This sets the RTC to the date and time this sketch was compiled.
  // After setting the time, comment it out and re-upload to prevent resetting the time on every reboot.
  // rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));

  Serial.println("Mosquito Trap Initialized.");
  
  // Get the current time to start our cycle timer
  DateTime now = rtc.now();
  lastCycleTime = now.unixtime();
  Serial.print("Current Unix time: ");
  Serial.println(lastCycleTime);
}

void loop() {
  // Get the current time from the RTC
  DateTime now = rtc.now();
  long currentTime = now.unixtime();

  Serial.print("Current Time: ");
  Serial.print(now.timestamp(DateTime::TIMESTAMP_FULL));
  Serial.print(", Seconds since last cycle: ");
  Serial.println(currentTime - lastCycleTime);

  // Check if 4 days (CYCLE_INTERVAL_SECONDS) have passed since the last cycle
  if (currentTime - lastCycleTime >= CYCLE_INTERVAL_SECONDS) {
    Serial.println("--- Cycle time reached! Starting water change. ---");
    runWaterChangeCycle();
    // Update the last cycle time to the current time
    lastCycleTime = currentTime;
    Serial.println("--- Water change complete. Timer reset. ---");
  } else {
    Serial.println("Not time yet. Waiting...");
  }

  // Wait for a while before checking again to conserve power.
  // For ultra-low power, you would use deep sleep modes here.
  delay(3600000); // Check once per hour (3600 * 1000 ms)
}

void runWaterChangeCycle() {
  // Step 1: Turn on the emptying pump
  Serial.println("Starting empty pump...");
  digitalWrite(EMPTY_PUMP_PIN, LOW); // LOW turns the relay ON
  delay(EMPTY_PUMP_DURATION_MS);

  // Step 2: Turn off the emptying pump
  digitalWrite(EMPTY_PUMP_PIN, HIGH); // HIGH turns the relay OFF
  Serial.println("Empty pump finished.");
  delay(5000); // Wait 5 seconds before refilling

  // Step 3: Turn on the refilling pump
  Serial.println("Starting refill pump...");
  digitalWrite(REFILL_PUMP_PIN, LOW); // LOW turns the relay ON
  delay(REFILL_PUMP_DURATION_MS);

  // Step 4: Turn off the refilling pump
  digitalWrite(REFILL_PUMP_PIN, HIGH); // HIGH turns the relay OFF
  Serial.println("Refill pump finished.");
}

Credits

Gal Ofel
1 project • 0 followers

Comments