Smart dusbin using arduino



Materials:-

Diagram:-




Connections:-

Ultrasonic sensor  to arduino
  • Ultrasonic GND = Arduino GND
  • Ultrasonic Trig = Arduino D6
  • Ultrasonic Echo = Arduino D7
  • Ultrasonic VCC = Arduino 3.3V

    Servo motor  to arduino  
    • Servo Black/brown = Arduino GND
    • Servo Red = Arduino 5V
    • Servo yellow = Arduino D8

    Downloads:-


    Code

    #include <Servo.h>   //servo library
    #define trigpin 6
    #define echopin 7
    int servoPin = 8;
    Servo servo;
    void setup()
    {

      pinMode(trigpin, OUTPUT);
      pinMode(echopin, INPUT);
      servo.attach(servoPin);
      servo.write(0);
      delay(100);
     servo.detach(); 
    }

    void loop()
    {
      int duration, distance;
      digitalWrite(trigpin, HIGH);

      delayMicroseconds(1000);
      digitalWrite(trigpin, LOW);


      duration = pulseIn(echopin, HIGH);

      distance = ( duration / 2) / 29.1;
      if ( distance<50 ) {
     servo.attach(servoPin);
      delay(1);
     servo.write(150);  
     delay(4000);       
     servo.write(0);    
     delay(2000);
     servo.detach();      
    }

    }