AutomaticTrashcan

by 1041987 in Circuits > Arduino

31 Views, 1 Favorites, 0 Comments

AutomaticTrashcan

arduino.png

My name is Udaivir and today,I am going to show you how to make a automatic trashcan that opens and closes with the use of a distance sensor,a RGB led that switches between red and green so you can know when the trashcan is open and when it is not and lastly,a buzzer that makes a sound to indicate that the trashcan is open.

Downloads

Supplies

Downloads

Wiring the Distance Sensor and Servo Motor

arduino.png

Wiring the distance sensor and sending power and ground in the breadboard

We would start with sending 5v and ground to the breadboard from the arduino.Then we can place the distance sensor on the arduino.The VCC of the distance sensor would get connected to power and the GND would be connected to ground.After that is done,We would connect Trig and Echo to the arduino as shown in the tinkercad image given above.

Connecting the Servo Motor ,the RGB Led and the Buzzer

1.Wiring the Servo motor

The servo motor has 3 wires that we have to connect in order for it to work.The middle wire of the servo motor(the red one) is the one we would give power to by connecting it to power on the breadboard.The brown wire on the servo motor is the one that we would give ground by connecting it to ground on the breadboard.The orange wire of the servo mototr is the one that we would connect to a pin on the arduino.It can be any number pin that you like but I chose pin number 3.Just like that,your servo motor is fully wired.

2.Wiring the RGB LED

I had a common anode RGB LED so I have it connected to power but if you have a common cathode RGB LED,all you need to do is switch the connection from power to ground.After connecting it to power/ground.We will wire the red,green and blue legs of LED to one of the PWM pins on the arduino each.I chose pins 9,10 and 11 to wire my red,green and blue legs of the LED.Just like that,your led is also fully wired now.

3.Wiring the buzzer

The buzzer is fairly easy to wire.Firstly,you would place the buzzer on the bradboard.The shorter leg of the buzzer is negative and the longer leg of the buzzer is positive.You would connect the negative leg of the buzzer to ground from the breadboard and ou would connect the positive leg to any number pin you like on the arduino,I chose pin number 12.Just like that,your buzzer is fully wired.

Code

Screenshot 2024-06-21 114007.png
Screenshot 2024-06-21 114021.png
Screenshot 2024-06-21 114035.png

Here is a download of the code if you want to use it and images given above,I will also go over the main factors of the code that help the circuit function how it is supposed to.

1.Include Servo Library(line 1)

The line of code just helps the servo motor to function and would not function without this line,so in a way,this is just a servo library.

2.Declaring variables,values and pins

  • Trig - This is the digital pin of the trig pin of the distance sensor
  • Echo - This is the digital pin of the echo pin of the distance sensor
  • Servo - This allows the servo motor to rotate from 0 to 180.
  • Red,Green,Blue - These three are the PWM pins of the RGB LED
  • Buzzer - This is the pin of the positive leg of the buzzer

3.Setup

  • Making the trig pin an output and the echo pin an input(can change if you used a different pin)
  • Makes the buzzer pin an output(can change if you used a different pin)
  • Makes the red,green and blue pins output.(can change if you used different PWM pins)
  • Attaches the servi motor to pin 3(can change if you used a different pin)

4.setColor

  • The first 3 lines of the setColor void make the starting color of the LED 0 each.
  • analogWrite all the values of the colors to the LED

5.Loop Functions

  • Tells the trig pin to initiate and start the distance sensor by sending a pulse
  • Measures the duration of the pulse on the echo pin
  • It calculates the distance based on how long the pulse lasted.
  • If the distance is closer than 50 and is in the range which is 50,it will rotate the servo motor 50 degrees,turn the led green and have the buzzer make a sound and then will continute to open the trashcan
  • If it is not in the range or goes out of range,the servo motor would rotate 160 degrees and close the trashcan,the led would also switch back to red to indicate that the trashcan is closed.
  • Adds a short delay before the loop is started again.