Arduino Auto Open Close Trash Bin
by melodylilac in Workshop > Home Improvement
413 Views, 0 Favorites, 0 Comments
Arduino Auto Open Close Trash Bin
Intro:
Basically, a trash can that can automatically open and close as you and your trash approach.
youtube video link: https://youtu.be/KT5-5HLUzh0
What you will need:
1.Arduino
2.Servo motor
3.ultrasound sensor
4. Servo arms
5. Trash bin or box (I am using a box)
Built the Circuit
Connect the Servo Arm
Add Servo & Sonar Sensor to the Trash Bin
Create the Code
#include int _distance ; int UltrasonicSensorCM(int trigPin, int echoPin) //Ultrasonic Sensor Code Auto Generated Return CM max distance 200 { long duration; pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(20); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); duration = duration / 59; if ((duration < 2) || (duration > 200)) return false; return duration; } Servo servo_pin_3; // create servo object to control a servo void setup(){ // put your setup code here, to run once: Serial.begin(9600); // opens serial port, sets data rate to 9600 bps digitalWrite( 6 , LOW ); //set ultrasonic sensor trigPin servo_pin_3.attach(3); // attaches the servo on pin to the servo object } void loop(){ // put your main code here, to run repeatedly: Serial.print(_distance); //print message Serial.print(" "); //print a blank Serial.println(); _distance = UltrasonicSensorCM( 6 , 7 ) ; delay( 1 ); // waits a few milliseconds if ( _distance < 50 && _distance > 1 ) { servo_pin_3.write( 180 ); // tell servo to go to position delay( 3000 ); // waits a few milliseconds } else { servo_pin_3.write( 60 ); // tell servo to go to position delay( 60 ); // waits a few milliseconds } }