The objective of this project is to design an intelligent trash bin that automatically opens its lid when a user's hand approaches and closes it after the waste has been disposed of. This touchless operation improves hygiene, convenience, and user comfort while demonstrating the practical application of embedded systems and automation.
The system is built around an ESP32 development board, an HC-SR04 ultrasonic distance sensor, and an SG90 servo motor.
Operation
1- The HC-SR04 ultrasonic sensor continuously measures the distance between the trash bin and any nearby object.
2- When the sensor detects a hand or object within a predefined distance (for example, less than 20 cm), it sends the measured distance to the ESP32 board.
3- The ESP32 processes this information and determines whether the object is close enough to activate the opening mechanism.
4- If the detection condition is satisfied, the ESP32 commands the SG90 servo motor to rotate and open the trash bin lid automatically.
5- After a short delay, allowing the user to dispose of the waste, the ESP32 instructs the servo motor to return to its initial position, closing the lid.
6- The system then resumes monitoring the surroundings, waiting for the next detection event.
1. Arduino UNO

The ESP32 is a powerful 32-bit microcontroller with built-in Wi-Fi and Bluetooth connectivity. It serves as the brain of the smart trash bin by processing data from the ultrasonic sensor and controlling the servo motor. Thanks to its high processing speed, low power consumption, and support for MicroPython, the ESP32 is widely used in IoT and automation projects.
2. HC-SR04 sensor

The HC-SR04 is a non-contact distance sensor that measures the distance to an object using ultrasonic sound waves. It emits a high-frequency pulse and measures the time taken for the echo to return after reflecting from an object.
In this project, the sensor detects the presence of a user's hand near the trash bin and sends the distance information to the ESP32.
3. Servo motor

The SG90 is a compact and lightweight micro servo motor capable of rotating approximately 180 degrees. It receives control signals from the ESP32 and converts them into precise angular movements.
In the smart trash bin system, the SG90 servo motor is responsible for automatically opening and closing the lid.
4. Jumper Wires

Jumper wires are used to establish electrical connections between the ESP32 board, the HC-SR04 sensor and the servo motor.
5. Breadboard

The breadboard is an essential prototyping tool used to assemble and test electronic circuits without soldering. In the smart trash bin system, it plays a key role in connecting and organizing the components such as the ESP32 board, HC-SR04 ultrasonic sensor, and SG90 servo motor.


1- Connection of HC-SR04 sensor to ESP32 board
| HC-SR04 sensor | ESP32 board |
|---|---|
| VCC | 3V |
| GND | GND |
| Trig | GPIO 23 |
| Echo | GPIO 22 |
2- Connection of servo motor to ESP32 board
| Servo motor | ESP32 board |
|---|---|
| red wire (+) | 5V |
| Brown wire (-) | GND |
| Yellow wire (S) | GPIO 21 |
This program controls an automatic smart trash bin using an ESP32 board, an HC-SR04 ultrasonic sensor, and an SG90 servo motor.
You must install this library : hc-sr04 for HC-SR04 sensor
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# ========================================================== # Import required libraries # ========================================================== import machine from machine import Pin, SoftI2C from hcsr04 import HCSR04 # HC-SR04 ultrasonic sensor library import time # Time delays and timing control from servo import Servo # Servo motor control library # ========================================================== # HC-SR04 Ultrasonic Sensor Initialization # trigger_pin : sends ultrasonic pulse # echo_pin : receives reflected pulse # echo_timeout_us : maximum waiting time for echo response # ========================================================== sensor = HCSR04( trigger_pin=23, echo_pin=22, echo_timeout_us=1000000 ) # ========================================================== # Servo motor initialization on GPIO21 # The trash bin lid starts in the closed position (0°) # ========================================================== servo_motor = Servo(pin=21) servo_motor.move(0) # ========================================================== # Main loop # ========================================================== while True: # ------------------------------------------------------ # Measure distance between the sensor and the object # placed in front of the trash bin # ------------------------------------------------------ distance = sensor.distance_cm() # Optional debug output (uncomment if needed) # print("Distance =", distance, "cm") # ------------------------------------------------------ # If an object is detected closer than 5 cm, # open the lid by rotating the servo to 90° # ------------------------------------------------------ if distance < 5: # Open the lid servo_motor.move(90) # Keep the lid open for 3 seconds time.sleep_ms(3000) else: # -------------------------------------------------- # No object detected within range: # Close the lid (0° position) # -------------------------------------------------- servo_motor.move(0) # ------------------------------------------------------ # Small delay before the next measurement # to avoid overly fast sensor readings # ------------------------------------------------------ time.sleep_ms(500) |
Explanation of the program
At startup, the ESP32 board initializes serial communication and sets the servomotor to the closed position (0°). The ultrasonic sensor continuously measures the distance between the trash bin and any nearby object.
When a hand or an object is detected at a distance of less than 3 cm, the ESP32 board sends a command to the SG90 servomotor to rotate to 90°, automatically opening the lid of the trash bin. The lid remains open for 3 seconds to allow the user to dispose of waste.
If no object is detected within the specified distance, the servomotor returns to 0°, keeping the lid closed. The process is repeated continuously, allowing the trash bin to operate automatically without physical contact.
This program provides a simple and hygienic solution for automatic waste disposal by combining distance detection and servo motor control.
Educational robotics refers to the use of robots and robotics technology to promote learning in educational settings. It involves the integration of technology, engineering, and computer science into the classroom, allowing students to engage in hands-on, project-based learning experiences.
In this context, our website represents an excellent resource for parents, teachers and children who wish to discover robotics.
Zaouiet Kontech-Jemmel-Monastir-Tunisia
Robotic site created by Mohamed Ali Haj Salah - Teacher info