Introduction: Garbage Can

In this project, we took 2 components and made them work together. This project will use an Ultrasonic sensor(distance sensor) and a servo motor. The Ultrasonic sensor will be used alongside with a servo motor to create an automatic opening and closing garbage can. The Ultrasonic sensor calculated the distance and if there is an object within 50 cm of the sensor then it will trigger the servo sensor function. Afterwards, the servo will turn 180 degrees and since it is tied to the cardboard by the string it will lift the lid open. The servo has a 2-second delay on it and then it will close afterwards.

Supplies

Arduino Uno

Ultrasonic Sensor

Servo Motor

Dustbin

String

Tape

Hot Glue Stick

Hot Glue Gun

Jumper Wires (Both Male to Male and Female to Female)

Cardboard

Step 1: Code

#include //servo library

Servo servo;

int trigPin = 5;

int echoPin = 6;

int servoPin = 7;

int led= 10;

long duration, dist, average;

long aver[3]; //array for average

void setup()

{

Serial.begin(9600);

servo.attach(servoPin);

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

servo.write(0); //close cap on power on

delay(100);

servo.detach();

}

void measure()

{

digitalWrite(10,HIGH);

digitalWrite(trigPin, LOW);

delayMicroseconds(5);

digitalWrite(trigPin, HIGH);

delayMicroseconds(15);

digitalWrite(trigPin, LOW);

pinMode(echoPin, INPUT);

duration = pulseIn(echoPin, HIGH);

dist = (duration/2) / 29.1; //obtain distance

}

void loop()

{

for (int i=0;i<=2;i++) //average distance

{

measure();

aver[i]=dist;

delay(10); //delay between measurements

}

dist=(aver[0]+aver[1]+aver[2])/3;

if ( dist<50 ) //Change distance as per your need

{

servo.attach(servoPin);

servo.write(10);

delay(3000);

servo.write(170);

delay(1000);

servo.detach();

}

Serial.print(dist);

}

Step 2: Making the Can

First, you need to create the lid of the garbage can. You can do this with cardboard. Firstly you need to take a piece of cardboard bigger than the top of the bin. Next, place the bin upside down to trace a circle on the cardboard. Cut the Cardboard out and then cut it in half. After cutting the cardboard in half tape it back together.

Step 3: Wiring and the Placing the Sensors

Now Wire the Servo Motor according to the given Diagram and you have complete the project.