PIR Sensor for Automatic Trash Can Lid

Project Overview

This project creates a touchless automatic trash can lid that opens when you wave your hand or approach with trash. A PIR sensor detects motion, and a servo motor lifts the lid. After a few seconds, the lid closes automatically. It’s perfect for kitchens where you often have messy hands or want to avoid touching the lid.

Difficulty: Beginner
Estimated time: 2-3 hours
Estimated cost: $20-30

How It Works

A PIR sensor mounted on the trash can lid or front detects motion. When motion is detected, a servo motor rotates to lift the lid (or a linear actuator pushes it open). After a set delay (e.g., 3 seconds), the servo returns to its closed position. An optional button can be added for manual control.

Materials Needed

  • Arduino Nano or Arduino Uno (1)
  • HC-SR501 PIR sensor (1)
  • Servo motor (MG995 or SG90, size depends on lid weight)
  • Trash can (with a hinged lid)
  • Mounting hardware (brackets, screws)
  • Jumper wires
  • Power supply (5V 2A, or battery pack)
  • Project enclosure (small box for electronics)
  • Hot glue or epoxy (for mounting)

Mechanical Design

Servo Mounting

Two common mounting approaches:

  1. Direct lift: Mount servo on the back of the trash can. Attach a rigid arm to the servo horn that pushes the lid open. Works well for lightweight lids.
  2. Lever mechanism: Use a longer lever arm to reduce the force required. Mount servo at the base and use a connecting rod to lift the lid.

Lid Weight Considerations

If your lid is heavy, consider:

  • Using a high-torque servo (MG996R or similar, 10-15 kg/cm torque).
  • Adding a counterweight to the lid.
  • Using a linear actuator instead of a servo.
  • Using a gear reduction mechanism.

Circuit Diagram

Connection Table

Component Pin Arduino Pin
PIR Sensor VCC 5V
PIR Sensor GND GND
PIR Sensor OUT Digital Pin 2
Servo Motor VCC (red) 5V (or external power)
Servo Motor GND (brown) GND
Servo Motor Signal (orange) Digital Pin 9

Note: For powerful servos, use an external 5V power supply (2-3A) connected to the servo’s VCC and GND, and share GND with the Arduino.

Arduino Code

// Automatic Trash Can Lid Opener
// Opens lid when motion detected

#include <Servo.h>

Servo lidServo;

const int pirPin = 2;
const int servoPin = 9;

// Servo positions (adjust for your lid)
const int closedPos = 0;    // Lid closed position
const int openPos = 90;      // Lid open position

unsigned long lastTriggerTime = 0;
const unsigned long openDuration = 3000;  // Lid stays open for 3 seconds

bool lidOpen = false;
bool motionDetected = false;

void setup() {
  Serial.begin(9600);
  
  pinMode(pirPin, INPUT);
  lidServo.attach(servoPin);
  
  // Start with lid closed
  lidServo.write(closedPos);
  
  Serial.println("Automatic Trash Can Ready");
  Serial.println("Waiting 60 seconds for PIR warm-up...");
  delay(60000);
}

void openLid() {
  Serial.println("Opening lid");
  lidServo.write(openPos);
  lidOpen = true;
  lastTriggerTime = millis();
}

void closeLid() {
  Serial.println("Closing lid");
  lidServo.write(closedPos);
  lidOpen = false;
}

void loop() {
  bool motion = digitalRead(pirPin) == HIGH;
  
  if (motion && !motionDetected) {
    motionDetected = true;
    if (!lidOpen) {
      openLid();
    }
  }
  
  if (!motion && motionDetected) {
    motionDetected = false;
  }
  
  if (lidOpen && (millis() - lastTriggerTime > openDuration)) {
    closeLid();
  }
  
  delay(100);
}

Enhanced Version with Manual Button and Status LED

#include <Servo.h>

Servo lidServo;

const int pirPin = 2;
const int servoPin = 9;
const int buttonPin = 3;
const int ledPin = 13;

const int closedPos = 0;
const int openPos = 90;

unsigned long lastTriggerTime = 0;
const unsigned long openDuration = 3000;
bool lidOpen = false;
bool motionDetected = false;
bool buttonHeld = false;

void setup() {
  Serial.begin(9600);
  
  pinMode(pirPin, INPUT);
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
  
  lidServo.attach(servoPin);
  lidServo.write(closedPos);
  digitalWrite(ledPin, LOW);
  
  delay(60000);
}

void openLid() {
  lidServo.write(openPos);
  lidOpen = true;
  lastTriggerTime = millis();
  digitalWrite(ledPin, HIGH);
  Serial.println("Lid OPEN");
}

void closeLid() {
  lidServo.write(closedPos);
  lidOpen = false;
  digitalWrite(ledPin, LOW);
  Serial.println("Lid CLOSED");
}

void loop() {
  bool motion = digitalRead(pirPin) == HIGH;
  bool button = digitalRead(buttonPin) == LOW;
  
  // Manual button control
  if (button && !buttonHeld) {
    buttonHeld = true;
    if (lidOpen) {
      closeLid();
    } else {
      openLid();
    }
  }
  if (!button) {
    buttonHeld = false;
  }
  
  // Automatic motion control
  if (motion && !motionDetected && !lidOpen) {
    motionDetected = true;
    openLid();
  }
  
  if (!motion && motionDetected) {
    motionDetected = false;
  }
  
  // Auto-close after delay
  if (lidOpen && !buttonHeld && (millis() - lastTriggerTime > openDuration)) {
    closeLid();
  }
  
  delay(100);
}

Installation Steps

  1. Mount servo: Attach servo to trash can using brackets or epoxy. Ensure the arm can move freely.
  2. Connect linkage: Attach servo horn to lid with a rigid arm or flexible wire. Test movement manually.
  3. Position PIR sensor: Mount sensor on front of trash can at waist height, angled to detect hand approach.
  4. Mount electronics: Place Arduino in small enclosure attached to the back or side of the trash can.
  5. Power: Use USB power adapter or battery pack (4xAA for 6V).
  6. Test: Wave hand in front of sensor, verify lid opens and closes.
  7. Adjust positions: Modify openPos and closedPos values in code to match your lid.

Calibration Tips

  • Servo position: Find the correct angle for open and closed positions by testing different values.
  • PIR sensitivity: Adjust potentiometer so sensor detects hand but not people walking past.
  • Detection area: Use masking tape to narrow the field of view if needed.
  • Open duration: Adjust openDuration based on how long you typically need the lid open.

Power Options

  • USB power: Simplest, if near an outlet.
  • Battery pack: 4xAA batteries (6V) or 18650 lithium battery (3.7V with boost converter).
  • Rechargeable: Use TP4056 charging module with LiPo battery.

Project Extensions

  • Battery level indicator: Add voltage divider and monitor battery level.
  • Voice activation: Add voice recognition module.
  • Fragrance dispenser: Add a small fan to spray air freshener when lid opens.
  • LED lighting: Add LED strip inside lid to illuminate when open.
  • Capacity sensor: Add ultrasonic sensor to detect when trash can is full.

Troubleshooting

  • Servo not moving: Check power supply. Servo may draw too much current from Arduino’s 5V pin; use external supply.
  • Lid not closing fully: Adjust closedPos value. Check for mechanical binding.
  • False triggers: Reduce PIR sensitivity or reposition sensor.
  • Lid closes too quickly: Increase openDuration.
  • Servo jitter: Add 100-1000µF capacitor across power lines near the servo.

Conclusion

This automatic trash can lid opener makes kitchen cleanup more hygienic and convenient. With simple materials and basic coding, you can upgrade any standard trash can to a touchless model.

Leave a Reply

Your email address will not be published. Required fields are marked *