Devices & Components
Arduino Uno Rev3
Ultrasonic Sensor - HC-SR04 (Generic)
Jumper wires (generic)
9V battery (generic)
SG90 Micro-servo motor
Project description
Code
Code of Smart Dustbin
h
1#include <Servo.h> 2 3Servo servoMain; // Define our Servo 4 5int trigpin = 10; 6 7int echopin = 11; 8 9int distance; 10 11float duration; 12 13float cm; 14 15void setup() 16 17{ 18 19servoMain.attach(9); // servo on digital pin 10 20 21pinMode(trigpin, OUTPUT); 22 23pinMode(echopin, INPUT); 24 25} 26 27void loop() 28 29{ 30 31digitalWrite(trigpin, LOW); 32 33delay(2); 34 35digitalWrite(trigpin, HIGH); 36 37delayMicroseconds(10); 38 39digitalWrite(trigpin, LOW); 40 41duration = pulseIn(echopin, HIGH); 42 43cm = (duration/58.82); 44 45distance = cm; 46 47if(distance<30) 48 49{ 50 51servoMain.write(180); // Turn Servo back to center position (90 degrees) 52 53delay(3000); 54 55} 56 57else{ 58 59servoMain.write(0); 60 61delay(50); 62 63} 64 65}
Code of Smart Dustbin
h
1#include <Servo.h> 2 3Servo servoMain; // Define our Servo 4 5int 6 trigpin = 10; 7 8int echopin = 11; 9 10int distance; 11 12float duration; 13 14float 15 cm; 16 17void setup() 18 19{ 20 21servoMain.attach(9); // servo on digital 22 pin 10 23 24pinMode(trigpin, OUTPUT); 25 26pinMode(echopin, INPUT); 27 28} 29 30void 31 loop() 32 33{ 34 35digitalWrite(trigpin, LOW); 36 37delay(2); 38 39digitalWrite(trigpin, 40 HIGH); 41 42delayMicroseconds(10); 43 44digitalWrite(trigpin, LOW); 45 46duration 47 = pulseIn(echopin, HIGH); 48 49cm = (duration/58.82); 50 51distance = cm; 52 53if(distance<30) 54 55{ 56 57servoMain.write(180); 58 // Turn Servo back to center position (90 degrees) 59 60delay(3000); 61 62} 63 64else{ 65 66servoMain.write(0); 67 68delay(50); 69 70} 71 72}
Downloadable files
Final Look
Final Look

Comments
Only logged in users can leave comments