IoT-Based Smart Dustbin nodemcu

 

 IoT-Based Smart Dustbin 







Components:

  • ESP8266 (NodeMCU)

  • Ultrasonic Sensor (HC-SR04)

  • Servo Motor

  • Jumper Wires

  • Power Supply


Circuit


Ultrasonic Sensor (HC-SR04):
    • TrigD2

    • EchoD3

    • VCC5V

    • GNDGND

  1. Servo Motor:

    • SignalD4 (PWM Pin)

    • VCC5V

    • GNDGND

  2. ESP8266:

    • GNDGND

    • VCC3.3V (or 5V if required)


Code for IoT-Based Smart Dustbin:


#include <ESP8266WiFi.h> #include <Servo.h> const char *ssid = "your-SSID"; // Replace with your WiFi SSID const char *password = "your-PASSWORD"; // Replace with your WiFi Password // Ultrasonic sensor pins const int trigPin = D2; const int echoPin = D3; // Servo motor pin const int servoPin = D4; // Create a Servo object Servo myServo; // Threshold distance (in cm) to consider the dustbin full const int thresholdDistance = 10; void setup() { // Initialize serial monitor Serial.begin(115200); // Initialize the ultrasonic sensor pins pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); // Initialize the servo motor myServo.attach(servoPin); myServo.write(0); // Close the dustbin lid initially // Connect to Wi-Fi WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Connecting to WiFi..."); } Serial.println("Connected to WiFi!"); } void loop() { long duration, distance; // Send a pulse to trigger the ultrasonic sensor digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Read the time it took for the pulse to return duration = pulseIn(echoPin, HIGH); // Calculate distance (in cm) distance = (duration / 2) / 29.1; // Print distance to the serial monitor Serial.print("Distance: "); Serial.println(distance); // If the dustbin is full (distance is less than the threshold) if (distance < thresholdDistance) { Serial.println("Dustbin is Full!"); myServo.write(90); // Open the dustbin lid (90 degrees) // Send a notification (This can be expanded with email/notification functionality) // Example: Send an alert via a web server or mobile app here. } else { myServo.write(0); // Close the dustbin lid } delay(1000); // Wait for 1 second before checking again }

Post a Comment

0 Comments