Electronics

Step 1

Gather The Parts


Here’s what you will need:

  1. Ultrasonic Sensor – $2.59
  2. Arduino – $24.95
  3. Trash Can With Lid – $11.00

Other miscellaneous tools you might need:

  1. Dremel or Drill
  2. Various Wires
  3. Breadboard
  4. Soldering Equipment

Step 2

Download The Schematic

Screen Shot 2014-09-04 at 5.58.08 PM

If you don’t know what Fritzing is, click here to download it. Then click the image below to download the Fritzing schematic.

Download

Step 3

Writing The Code

If you don’t have the Arduino software downloaded already, you can grab a copy here. Feel free to copy and paste the code below into your Arduino software and then upload it to your Arduino.


#define pingTrig 6 
#define pingEcho 7 

#include 

Servo myservo;

void setup()
{
  pinMode(pingTrig, OUTPUT);
  pinMode(pingEcho, INPUT);
  myservo.attach(9);
}

void loop()
{
  long duration, inches, cm;
  
  digitalWrite(pingTrig, LOW);
  delayMicroseconds(2);
  digitalWrite(pingTrig, HIGH);
  delayMicroseconds(10);
  digitalWrite(pingTrig, LOW);
  
  duration = pulseIn(pingEcho, HIGH);
  
  cm = duration / 29 / 2;
  
  if(cm < 10 ){
    myservo.write(90);
    delay(100);
  }else{
    myservo.write(0);
  }
  delay(2000);
}