Gokul K B
Published © CC BY-NC

LumiTrap -3DP Solar UV Trap for Eco-Friendly Pest Control

Seeking a non-toxic and sustainable method to rіd your space of insects? This solar-powered bug trap presents an eco-conscious alternative

IntermediateFull instructions provided4 hours796
LumiTrap -3DP Solar UV Trap for Eco-Friendly Pest Control

Things used in this project

Hardware components

Seeed Studio XIAO SAMD21 (Pre-Soldered) - Seeeduino XIAO
Seeed Studio XIAO SAMD21 (Pre-Soldered) - Seeeduino XIAO
×1

Software apps and online services

Fusion
Autodesk Fusion

Story

Read more

Custom parts and enclosures

CAD

Schematics

circuit_Twpj5bkSF6.jpg

Code

Untitled file

C/C++
// Pin definitions
const int ldrPin = D0;       // D0 (LDR input)
const int uvLedPin = D1;      // D1 (UV LED)
const int fanPin   = D2;      // D2 (Fan)

// LDR setup
const float vRef = 3.3;
const int adcMax = 1023;
const float dayThreshold = 2.9;  // ≤ 2V = Bright (Day)

// Timing
const unsigned long uvOnlyDuration = 60000;  // 1 minute
const unsigned long fanOnDuration  = 30000;  // 30 sec

void setup() {
  pinMode(uvLedPin, OUTPUT);
  pinMode(fanPin, OUTPUT);
  digitalWrite(uvLedPin, LOW);
  digitalWrite(fanPin, LOW);

  analogReadResolution(10);  // 10-bit ADC
  Serial.begin(9600);
}

void loop() {
  float voltage = readLdrVoltage();
  Serial.print("LDR Voltage: ");
  Serial.print(voltage, 2);
  Serial.print(" V - ");

  if (voltage > dayThreshold) {
    Serial.println("Dark - Start cycle");

    // Turn on UV LED only
    digitalWrite(uvLedPin, HIGH);
    digitalWrite(fanPin, LOW);

    if (!waitWhileDark(uvOnlyDuration)) return;

    // Now turn on fan
    digitalWrite(fanPin, HIGH);
    if (!waitWhileDark(fanOnDuration)) return;

    // Turn off fan (UV LED stays ON)
    digitalWrite(fanPin, LOW);
  } else {
    Serial.println("Bright - Powering off");
    digitalWrite(uvLedPin, LOW);
    digitalWrite(fanPin, LOW);
    delay(500);
  }
}

// Helper: read voltage from LDR
float readLdrVoltage() {
  int raw = analogRead(ldrPin);
  return (raw / (float)adcMax) * vRef;
}

// Helper: wait during dark period, exit early if it gets bright
bool waitWhileDark(unsigned long duration) {
  unsigned long start = millis();
  while (millis() - start < duration) {
    float voltage = readLdrVoltage();
    if (voltage <= dayThreshold) {
      Serial.println("Daylight detected - exiting early");
      digitalWrite(uvLedPin, LOW);
      digitalWrite(fanPin, LOW);
      return false;
    }
    delay(100);
  }
  return true;
}  

CODE

C/C++
// Pin definitions
const int ldrPin = D0;       // D0 (LDR input)
const int uvLedPin = D1;      // D1 (UV LED)
const int fanPin   = D2;      // D2 (Fan)

// LDR setup
const float vRef = 3.3;
const int adcMax = 1023;
const float dayThreshold = 2.9;  // ≤ 2V = Bright (Day)

// Timing
const unsigned long uvOnlyDuration = 60000;  // 1 minute
const unsigned long fanOnDuration  = 30000;  // 30 sec

void setup() {
  pinMode(uvLedPin, OUTPUT);
  pinMode(fanPin, OUTPUT);
  digitalWrite(uvLedPin, LOW);
  digitalWrite(fanPin, LOW);

  analogReadResolution(10);  // 10-bit ADC
  Serial.begin(9600);
}

void loop() {
  float voltage = readLdrVoltage();
  Serial.print("LDR Voltage: ");
  Serial.print(voltage, 2);
  Serial.print(" V - ");

  if (voltage > dayThreshold) {
    Serial.println("Dark - Start cycle");

    // Turn on UV LED only
    digitalWrite(uvLedPin, HIGH);
    digitalWrite(fanPin, LOW);

    if (!waitWhileDark(uvOnlyDuration)) return;

    // Now turn on fan
    digitalWrite(fanPin, HIGH);
    if (!waitWhileDark(fanOnDuration)) return;

    // Turn off fan (UV LED stays ON)
    digitalWrite(fanPin, LOW);
  } else {
    Serial.println("Bright - Powering off");
    digitalWrite(uvLedPin, LOW);
    digitalWrite(fanPin, LOW);
    delay(500);
  }
}

// Helper: read voltage from LDR
float readLdrVoltage() {
  int raw = analogRead(ldrPin);
  return (raw / (float)adcMax) * vRef;
}

// Helper: wait during dark period, exit early if it gets bright
bool waitWhileDark(unsigned long duration) {
  unsigned long start = millis();
  while (millis() - start < duration) {
    float voltage = readLdrVoltage();
    if (voltage <= dayThreshold) {
      Serial.println("Daylight detected - exiting early");
      digitalWrite(uvLedPin, LOW);
      digitalWrite(fanPin, LOW);
      return false;
    }
    delay(100);
  }
  return true;
}  

Credits

Gokul K B
34 projects • 29 followers
⚡Electronics Engineer |🛠️Maker & Open-Source Enthusiast | 🚀 3D Printing & CAD Expert | 🏗️ Product Design |🔌Embedded Systems & PCB Design

Comments