Devices & Components
Arduino Uno Rev3
SG90 Micro-servo motor
Jumper wires (generic)
Ultrasonic Sensor - HC-SR04 (Generic)
Cardboard
Hardware & Tools
Scissor, Electrician
Barbecue stick
Hot glue gun (generic)
Project description
Code
Trash bin
arduino
This is the code I wrote for this project. You need to install the Servo library if you don't have it on your Arduino IDE. Modify the openCloseDelay value to make the lid open and close faster or slower.
1#include <Servo.h> 2 3int servo = 7 ; 4int trigger = 8; 5int echo = 9; 6int angle = 90; 7int servoDelay = 2; 8int sensorDelay = 10; 9int openCloseDelay = 2000; 10long duration = 0; 11float distance = 0; 12 13Servo myServo; 14 15void setup() { 16 Serial.begin(9600); 17 pinMode(trigger, OUTPUT); 18 pinMode(echo, INPUT); 19 20 digitalWrite(servo, LOW); 21 digitalWrite(trigger, LOW); 22 23 myServo.attach(servo); 24 myServo.write(angle); 25} 26 27void loop() { 28 delay(sensorDelay); 29 digitalWrite(trigger, HIGH); 30 delay(sensorDelay); 31 digitalWrite(trigger, LOW); 32 duration = pulseIn(echo, HIGH); 33 distance = duration * 0.034 / 2; 34 Serial.println(distance); 35 36 if (distance <= 50.0) { 37 angle = 0; 38 myServo.write(angle); 39 delay(openCloseDelay); 40 } else { 41 angle = 90; 42 myServo.write(angle); 43 delay(openCloseDelay); 44 } 45} 46
Downloadable files
Trash bin schematics
The sensor is supplied by 5V and the servo is connected to 3.3V pin.
Trash bin schematics

Trash bin schematics
The sensor is supplied by 5V and the servo is connected to 3.3V pin.
Trash bin schematics

Comments
Only logged in users can leave comments