Automatic Trashcan

by 757401 in Circuits > Arduino

1564 Views, 22 Favorites, 0 Comments

Automatic Trashcan

PXL_20240123_174225725.jpg

Hello, my name is Gurvishvash Baath but I like to be called Gv because that's what my friends call me. On this instructible I will present my final project aka the automatic trashcan. What this project does is when your hand is near the trashcan, it opens up and when you are away from the trashcan it closes, pretty simple. It's a very simple but still fun project to make, but now that you know what the project is lets move onto the materials in order to build this fun and simple project.

Supplies

PXL_20240112_181228237.jpg

In order to make this project, you are going to need a few materials

  1. Ultra sonic distance sensor
  2. Servo motor
  3. Jumper wires
  4. Arduino
  5. Cardboard box
  6. 9V battery
  7. 9V battery cap

Now that you know what the materials are we can move onto how to set everything up, so lets go to the first step.

Connecting Servo, Distance Sensor and Arduino

download.png
PXL_20240116_181138160.jpg

For connecting the servo and distance sensor you can use the Tinker cad image to help you if you are struggling with wiring them to the Arduino.

1.Connecting jumper wires to the distance sensors

This is the only component you need jumper wires for because the pins on the distance sensor are used for breadboards, the first thing you want to do is connect the jumper wires on the distance sensor and connect the first ones to VCC and GND. Then the second jumper wires you connect them to Trig and Echo.

2.Adding wires to the servo motor

Here you will need wires for the servo motor because it has 3 open holes where you can put 3 wires in (I recommend getting colour coded wires so that you know which one goes into which in case you want to take it out).

3.Connecting both components to the Arduino

So now that both of our components have wires, we will plug them into the Arduino. For the distance sensor we plug the VCC into the 5V and the GND into the GND pin, after that you can plug trig and echo into any number pins you would like, but just remember to change it in the code same goes with the motor. For our servo motor there are 3 wires red, orange and brown. The red wire goes into 3.3V, the brown wire goes into GND and the orange wire goes into any number pin you like. Now you should have everything connected with the Arduino, so let’s move onto to our next step.

Attaching Everything in the Box

PXL_20240123_174247356.jpg

1.Cutting holes through the box

The first thing you should do is cut holes that fit the size of the distance sensor because that is where you will be placing your sensor (Make sure they fit the distance sensor, sensors).

2.Placing the distance sensor through the holes

For this step all you need to do is place the distance sensor through the holes you make in the box.

3.Taping the motor to any side of the box

For this you just need to figure out where you want to tape the servo motor, obviously tape it where it will push the lid up and close the lid. If you can try to get a servo extender which makes the motor on the top more longer meaning it can push up the lid more easier than without the extender.

4.Taping the Arduino and Battery to the side of the box (optional)

This is completely optional and I did not do it, but if you want a proper trash can with space then you should probably do this. All you need to do is tape the Arduino and battery to one side, I also recommend using a bigger cardboard box or make one your own because the one I have is small and is simply just a small project, but a lot of people can make this project into a real trash can somehow. So now we move onto the next step which is the code.


Code

download.png

This is the code and I am going to explain how it works.

1.Include Servo Library (line 1)

What this basically does is it controls the servo motor so its basically the servo library

2.Declaring Variables and Pin Numbers (lines 3, 4 and 5)

  • servo: this allows the servo to rotate from 0 to 180
  • trig Pin: this is a digital pin of the trig pin from the ultrasonic distance sensor
  • echo Pin: this is a digital pin of the echo pin from the ultrasonic distance sensor

3.Setup Function (lines 7, 8, 9 and 10)

  • Makes the trig Pin into an output and the echo Pin into an input
  • Attaches the servo motor to pin 3 (change it if you are using a different pin)

4.Loop Function (lines 13-35)

  • Sends a pulse to the trig pin to startup the ultrasonic distance sensor
  • Measures the duration of the pulse on the echo pin
  • Calculates the distance based on the duration of the pulse
  • If the distance is within range which is 50 it moves the servo to 50 degrees otherwise is moves the servo to position 160 degrees
  • adds a short delay before the loop starts again

How It's Supposed to Work

Here is video to show you how it is supposed to function.