Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
Will Su
Published © CC BY

Touchless Automatic Motion Sensor Trash Can

A touchless automatic motion sensor trash can.

BeginnerShowcase (no instructions)1 hour122,459
Touchless Automatic Motion Sensor Trash Can

Things used in this project

Hardware components

Arduino Micro
Arduino Micro
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
SG90 Micro-servo motor
SG90 Micro-servo motor
×1
Mini Trash Can
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Hot glue gun (generic)
Hot glue gun (generic)

Story

Read more

Schematics

Circuits.io

Code

AutoTrashCan.ino

Arduino
#include <Servo.h> 

Servo myservo;
const int servo_pin = 2;
const int trig_pin = 3;
const int echo_pin = 4;
const int inter_time = 200;
int time = 0;

void setup() 
{
  Serial.begin(9600);
  myservo.attach(servo_pin, 500, 2400);
  myservo.write(90);
  pinMode (trig_pin, OUTPUT);
  pinMode (echo_pin, INPUT);
  delay(3000);
} 

void loop() 
{
  float duration, distance;
  digitalWrite(trig_pin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trig_pin, LOW);
  duration = pulseIn (echo_pin, HIGH);
  distance = (duration/2)/29;
  Serial.print(distance);
  Serial.println(" cm");
  time = time + inter_time;
  delay(inter_time);
  if (distance < 10)
  {
    for(int i = 1500; i >= 1100; i-=25){
      myservo.writeMicroseconds(i);
      Serial.println("2");
      delay(100);
    }
    delay(1000);
    for(int i = 1100; i <= 1500; i+=25){
      myservo.writeMicroseconds(i);
      Serial.println("1");
      delay(100);
    }
  }
}

Credits

Will Su

Will Su

0 projects • 34 followers

Comments