Skip to main contentSkip to footer

How to Build an Automatic Bin Opener Using Arduino

No Comments

Building a touchless, automatic dustbin is an excellent entry point into electronics. It solves a practical problem—keeping your hands clean when throwing away trash—while teaching you the fundamentals of using sensors and motors.

Instead of jumping straight into wiring up physical hardware, this guide approaches the build like a professional engineer: we will define the hardware, simulate the circuit to ensure the logic works, and then build the physical prototype.

Here is the video tutorial:

Understanding Your Components

Before plugging anything in, it helps to know what each piece of hardware is actually doing in the system.

  • Arduino Uno: This is the “brain” of your project. It is a programmable microcontroller that reads data from the sensor, processes it using your code, and sends exact movement commands to the motor.

  • HC-SR04 Ultrasonic Sensor: This acts as the “eyes.” It works exactly like a bat’s echolocation. It sends out an ultrasonic sound wave (too high for humans to hear) and listens for that wave to bounce off your hand and return. By measuring how long the echo takes, it calculates how close your hand is.

  • SG90 Servo Motor: This is the “muscle.” Unlike standard motors that spin continuously, a servo motor can be programmed to turn to a specific, precise angle (like 0° to stay closed, or 90° to open the lid) and hold that position.

  • 9V Battery & Barrel Connector: The power supply. The Arduino needs electricity to run when disconnected from your computer.

  • Jumper Wires: The “nervous system” that carries power and data between the board, sensor, and motor.

Wiring the Circuit

Every electronic component needs two things to function: Power (which flows in a loop from positive to negative) and Data (the instructions).

  • VCC stands for Voltage Common Collector—this is your positive power input.

  • GND stands for Ground—this is the negative power return that completes the electrical loop.

Use your jumper wires to make the following connections:

Component Pin on Component Arduino Pin What this connection does
Ultrasonic Sensor VCC 5V Provides 5 volts of power to the sensor.
Ultrasonic Sensor GND GND Completes the power circuit for the sensor.
Ultrasonic Sensor TRIG D12 The Arduino sends a signal here to trigger the sound wave.
Ultrasonic Sensor ECHO D13 The sensor sends a signal back here when it hears the echo.
Servo Motor Signal (Usually Orange) D9 The Arduino sends exact angle commands to the motor.
Servo Motor VCC (Usually Red) 3.3V Provides a lower 3.3 volts of power to the motor.
Servo Motor GND (Usually Brown) GND Completes the power circuit for the motor.

Phase 1: Simulation in Tinkercad

Professionals simulate circuits before building them. It prevents you from accidentally frying components with incorrect wiring and lets you test your code logic instantly. Tinkercad is a free, browser-based simulator.

1.Create the Virtual Circuit:Drag and drop your components.

Open Tinkercad, create a new Circuit, and drag an Arduino Uno, an HC-SR04 sensor, and a Micro Servo onto the workspace. Wire them exactly according to the table above.

2.Write the Logic Code:

Open the “Code” panel and switch to Blocks. You need to tell the Arduino how to react to distance. Set up an if/else statement:

  • IF distance is less than < 10 cm $\rightarrow$ rotate servo to 90° (lid opens).

  • ELSE $\rightarrow$ rotate servo to 0° (lid stays closed).

3.Add a Delay:

Inside the “IF” block, add a 3-second delay after the servo turns to 90°.

Why? Without a delay, the sensor will read that your hand moved away and immediately slam the lid shut while you are still throwing your trash away. The delay gives you time to drop the item.

4.Run the Simulation:

Click “Start Simulation.” Click on the ultrasonic sensor to drag the virtual object closer than 10 cm. You should see the servo motor spin to 90 degrees, wait three seconds, and return to 0 degrees.

Here is the code below

// C++ code
//
#include <Servo.h>

long readUltrasonicDistance(int triggerPin, int echoPin)
{
  pinMode(triggerPin, OUTPUT);  // Clear the trigger
  digitalWrite(triggerPin, LOW);
  delayMicroseconds(2);
  // Sets the trigger pin to HIGH state for 10 microseconds
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPin, LOW);
  pinMode(echoPin, INPUT);
  // Reads the echo pin, and returns the sound wave travel time in microseconds
  return pulseIn(echoPin, HIGH);
}

Servo servo_9;

void setup()
{
  servo_9.attach(9, 500, 2500);
}

void loop()
{
  if (0.01723 * readUltrasonicDistance(12, 13) < 10) {
    servo_9.write(90);
    delay(3000); // Wait for 3000 millisecond(s)
  } else {
    servo_9.write(0);
  }
  delay(10); //for block stability
}

Phase 2: Building the Physical Prototype

Once your simulation works perfectly, it’s time to translate it to the real world.

1.Wire the Hardware:

Duplicate your Tinkercad wiring on the physical Arduino. Make sure your Arduino is unplugged from your computer while doing this to avoid accidental short circuits.

2.Build the Hinge Mechanism:

The tiny plastic arm that comes with the servo isn’t long enough to open a cardboard lid. Tape or glue an ice cream stick to the servo arm to act as a lever.

3.Mount the Components:

Cut a small hole in the front of your cardboard box for the two “eyes” of the ultrasonic sensor to peek through. Tape the sensor securely inside. Mount the servo motor on the inside wall of the box so that when the ice cream stick rotates upward, it pushes the cardboard lid open.

4.Upload the Code:

Connect the Arduino to your computer via USB. Export your block code from Tinkercad (or copy the text version), paste it into the Arduino IDE software, and click “Upload.”

5.Power and Test:The final check.

Unplug the USB and plug in your 9V battery via the barrel jack. Wave your hand within 10 cm of the front sensor. The lid should pop up, wait for 3 seconds, and close.

 

Tweaking for Perfection

Real-world physics differ slightly from simulations. If the servo pushes the lid too far back (causing it to fall over), adjust the code so it only opens to 70° or 80° instead of 90°. If the lid jitters, double-check that your wires are firmly seated in the pins.

Previous Post
Building an Arduino LED Memory Game for Kids
Next Post
Build an Easy Color-Coded DIY Study Timer with Arduino (No Soldering)

Latest Updates