Skip to content

Latest commit

 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 

Repository files navigation

Smart Dustbin Using IoT (ESP8266)

This project is aimed at building a Smart Dustbin using IoT. It integrates an ESP8266 board with several sensors and actuators to automatically open the lid when a hand is detected, measure the dustbin fullness using an ultrasonic sensor, and trigger a buzzer when the bin is full. The project is controlled remotely via the Blynk app.

Overview

The system performs the following functions:

  • IR Sensor: Detects the presence of a hand in front of the sensor.
  • Servo Motor: When a hand is detected, the servo motor rotates the lid to 90° (to open it) and then returns to 0° (to close) after a 3-second interval.
  • Ultrasonic Sensor: Measures the distance to the trash inside the dustbin and maps this value to a fullness percentage (0-100%).
  • Buzzer: Activates when the dustbin is 100% full.
  • Blynk Integration:
    • V0: Displays IR sensor status ("Hand Detected" / "Not Detected").
    • V1: Displays the current servo angle (0° or 90°).
    • V2: Displays the dustbin fullness percentage.
    • V3: Indicates the full dustbin alert (1 for alert, 0 for normal).

Hardware Requirements

  • ESP8266 Board (e.g., NodeMCU or any ESP8266-based board)
  • IR Sensor
  • Servo Motor (compatible with ESP8266; it is recommended to use the ESP8266Servo library)
  • Ultrasonic Sensor (with TRIG and ECHO pins)
  • Buzzer
  • Jumper Wires & Breadboard

Software Requirements

  • Arduino IDE (Ensure you have the ESP8266 Board Package installed via the Board Manager)
  • ESP8266 Board Package (available in Arduino Board Manager)
  • Blynk Library (install via the Arduino Library Manager)
  • Servo Library (or ESP8266Servo for better compatibility with ESP8266)

Circuit Diagram

The hardware connections are as follows:

  • IR Sensor: Connect to ESP8266 GPIO D5.
  • Servo Motor: Connect to ESP8266 GPIO D6.
  • Ultrasonic Sensor:
    • TRIG pin to ESP8266 GPIO D7
    • ECHO pin to ESP8266 GPIO D8
  • Buzzer: Connect to ESP8266 GPIO D2

*Circuit Diagram circuit_image * Circuit diagram link: (https://app.cirkitdesigner.com/project/a288e986-22ab-4bde-b39b-221b9fdbabf5)

Code Description

The project code uses a non-blocking approach (using millis()) to control the servo motor. When the IR sensor detects a hand, the servo opens to 90° and the detection time is recorded. If the hand is removed and 3 seconds have elapsed, the servo automatically closes (returns to 0°). The current servo angle is sent to the Blynk app so that you can monitor the lid position in real time. Additionally, the ultrasonic sensor measures the fullness of the dustbin and triggers the buzzer when it reaches 100%.

Blynk App Configuration

The following virtual pins are used in the Blynk app:

  • V0 (Label Widget):
    • Name: IR Sensor Status
    • Data Type: String
    • Display: "Hand Detected" or "Not Detected"
  • V1 (Gauge Widget):
    • Name: Servo Angle
    • Data Type: Integer
    • Units: degrees
    • Range: 0 to 90
  • V2 (Gauge Widget):
    • Name: Dustbin Fullness
    • Data Type: Integer
    • Units: percentage
    • Range: 0 to 100
  • V3 (Alarm/Sound Widget):
    • Name: Full Dustbin Alert
    • Data Type: Integer
    • Display: 1 (Alert On) or 0 (Alert Off)

Code (src/main.ino)

#define BLYNK_TEMPLATE_ID   "TMPL6hgZg8Yiy"
#define BLYNK_TEMPLATE_NAME "Smart Dustbin Using IoT Using ESP8266"
#define BLYNK_AUTH_TOKEN    "iQ1PgQ-Yf7MEx8Qa_nSNcPN6_6QRpvi2"

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Servo.h>

char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Creative House";
char pass[] = "Creative2025";

#define IR_SENSOR_PIN D5
#define SERVO_PIN     D6
#define TRIG_PIN      D7
#define ECHO_PIN      D8
#define BUZZER_PIN    D2

Servo myservo;
int servoAngle = 0;

long duration;
int distance;
int fullness;
bool isFull = false;

BlynkTimer timer;

// Servo/Lid control variables
bool lidIsOpen = false;
unsigned long lidLastDetectedTime = 0;
const unsigned long lidOpenDuration = 3000; // 3 seconds

//--------------------------------------
// Servo/Lid control functions
//--------------------------------------
void openLid() {
  myservo.write(90);      // Open lid to 90°
  servoAngle = 90;
  lidIsOpen = true;
  Blynk.virtualWrite(V1, servoAngle); // Update servo angle on Blynk
}

void closeLid() {
  myservo.write(0);       // Close lid (0°)
  servoAngle = 0;
  lidIsOpen = false;
  Blynk.virtualWrite(V1, servoAngle); // Update servo angle on Blynk
}

//--------------------------------------
// IR Sensor check with non-blocking delay
//--------------------------------------
void checkIrSensor() {
  if (digitalRead(IR_SENSOR_PIN) == LOW) {
    Blynk.virtualWrite(V0, "Hand Detected");
    // Update detection time when hand is detected
    lidLastDetectedTime = millis();
    if (!lidIsOpen) {
      openLid();
    }
  } else {
    Blynk.virtualWrite(V0, "Not Detected");
    // If hand is removed and 3 seconds have elapsed, close the lid
    if (lidIsOpen && (millis() - lidLastDetectedTime >= lidOpenDuration)) {
      closeLid();
    }
  }
  // Always update the servo angle on Blynk
  Blynk.virtualWrite(V1, servoAngle);
}

//--------------------------------------
// Ultrasonic sensor check for dustbin fullness
//--------------------------------------
void checkDustbinFullness() {
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);
  
  duration = pulseIn(ECHO_PIN, HIGH);
  distance = duration * 0.034 / 2; // Distance in centimeters
  
  // Assume 30cm means empty and 1cm means full
  fullness = map(distance, 30, 1, 0, 100);
  fullness = constrain(fullness, 0, 100);
  Blynk.virtualWrite(V2, fullness);
  
  if (fullness >= 100 && !isFull) {
    digitalWrite(BUZZER_PIN, HIGH);
    Blynk.virtualWrite(V3, 1);
    isFull = true;
  } else if (fullness < 100 && isFull) {
    digitalWrite(BUZZER_PIN, LOW);
    Blynk.virtualWrite(V3, 0);
    isFull = false;
  }
}

//--------------------------------------
// Sensor update function
//--------------------------------------
void updateSensors() {
  checkIrSensor();
  checkDustbinFullness();
}

void setup() {
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);

  pinMode(IR_SENSOR_PIN, INPUT);
  myservo.attach(SERVO_PIN);
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  pinMode(BUZZER_PIN, OUTPUT);

  // Initialize the servo to closed (0°)
  myservo.write(0);
  servoAngle = 0;
  Blynk.virtualWrite(V1, servoAngle);
  
  // Update sensors every 100 milliseconds to maintain responsiveness
  timer.setInterval(100L, updateSensors);
}

void loop() {
  Blynk.run();
  timer.run();
}

About

This project is aimed at building a Smart Dustbin using IoT.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Contributors

Languages