The ESP8266 NodeMCU Smart Trash Bin System is designed to create an intelligent and contactless waste management solution using IoT technology. Its main objective is to automatically open the trash bin lid when a user approaches, reducing the need for physical contact and improving hygiene. This system demonstrates how embedded systems can be applied to solve real-life problems in a simple and efficient way.
The functioning of the system is based on three main components: the ESP8266 NodeMCU, the HC-SR04 ultrasonic sensor, and the SG90 servo motor. The ultrasonic sensor is responsible for continuously measuring the distance between the bin and any approaching object by sending and receiving ultrasonic waves. This allows the system to detect the presence of a user near the bin in real time.
Once the distance data is collected, the NodeMCU processes the information and compares it with a predefined threshold value. If an object (such as a hand) is detected within a certain range, the microcontroller triggers the servo motor. This decision-making process is the core of the smart behavior of the system.
When the condition is met, the SG90 servo motor automatically rotates to open the lid of the trash bin, allowing the user to dispose of waste without touching the bin. After a short delay, and when no object is detected near the sensor, the system sends another command to the servo motor to return the lid to its closed position.
1. ESP826 NodeMCU

The ESP8266 NodeMCU Smart Trash Bin System is built using three main electronic components: the ESP8266 NodeMCU, the HC-SR04 ultrasonic sensor, and the SG90 servo motor. Each component plays an important role in making the system smart and automatic.
2. HC-SR04 sensor

The HC-SR04 ultrasonic sensor is used to detect the presence of a user near the trash bin. It works by sending ultrasonic waves and measuring the time taken for the echo to return after hitting an object. Based on this time, the sensor calculates the distance between the bin and the object. This information is then sent to the NodeMCU, allowing the system to know when someone is close enough to trigger the lid opening.
3. Servo motor

he SG90 servo motor is responsible for the mechanical movement of the trash bin lid. When it receives a signal from the NodeMCU, it rotates to a specific angle to open the lid and allows the user to throw waste inside. After a short delay, it returns to its original position to close the lid automatically. This makes the system fully automated and contactless.
4. Jumper Wires

Jumper wires are used to establish electrical connections between the ESP8266 NodeMCU, 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 ESP8266 NodeMCU, HC-SR04 ultrasonic sensor, and SG90 servo motor.


1- Connection of HC-SR04 sensor to ESP8266 NodeMCU
| HC-SR04 sensor | ESP8266 NodeMCU |
|---|---|
| VCC | 3V |
| GND | GND |
| Trig | D0 |
| Echo | D1 |
2- Connection of servo motor to EESP8266 NodeMCU
| Servo motor | ESP8266 NodeMCU |
|---|---|
| red wire (+) | 5V |
| Brown wire (-) | GND |
| Yellow wire (S) | D2 |
This program controls an automatic smart trash bin using an ESP8266 NodeMCU, 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 |
import machine from machine import Pin, SoftI2C from hcsr04 import HCSR04 import time from servo import Servo # ========================================================== # HC-SR04 Ultrasonic Sensor Initialization # ========================================================== # trigger_pin: sends the ultrasonic pulse # echo_pin : receives the reflected signal (echo) # echo_timeout_us: maximum waiting time for echo response sensor = HCSR04(trigger_pin=16, echo_pin=5, echo_timeout_us=1000000) # ========================================================== # Servo Motor Initialization # ========================================================== # Servo connected to GPIO pin 4 # Initial position: 0° (closed or rest position) servo_motor = Servo(pin=4) servo_motor.move(0) # ========================================================== # Main program loop # ========================================================== while True: # Measure the distance between the sensor and an object (in cm) distance = sensor.distance_cm() # Optional: print distance for debugging # print('distance =', distance, 'cm') # ====================================================== # Object detection condition # ====================================================== # If an object is detected closer than 5 cm: # the servo motor activates (opens the bin/door) if distance < 5: servo_motor.move(90) # open position (90 degrees) time.sleep_ms(3000) # keep it open for 3 seconds else: servo_motor.move(0) # return to closed position # Small delay to avoid too frequent readings time.sleep_ms(500) |
Explanation of the program
This program is designed to create an automatic smart system using an ESP-based microcontroller, an HC-SR04 ultrasonic sensor, and a servo motor. Its main goal is to detect the presence of an object or a hand near the sensor and automatically trigger a mechanical action, such as opening a lid or door.
At the beginning of the program, the required libraries are imported. The HCSR04 library is used to communicate with the ultrasonic sensor, while the Servo library is used to control the servo motor. The time module is also included to manage delays in the system, ensuring smooth and stable operation.
The ultrasonic sensor is then initialized using two GPIO pins: one for triggering the ultrasonic signal and another for receiving the echo. The sensor works by sending an ultrasonic wave and measuring the time it takes for the wave to bounce back after hitting an object. This time is converted into a distance value in centimeters.
Next, the servo motor is initialized and connected to a specific GPIO pin. It is first set to position 0 degrees, which represents the closed or resting state of the system. This ensures that the mechanism starts in a stable and known position before entering the main loop.
Inside the main loop, the program continuously measures the distance between the sensor and any nearby object. This allows the system to react in real time. If the detected distance is less than 5 cm, the program considers that an object or hand is close enough to activate the system.
When this condition is met, the servo motor rotates to 90 degrees, simulating an opening action such as lifting a trash bin lid. The system then keeps this position for 3 seconds to allow the user enough time to interact with it. After this delay, or if no object is detected, the servo returns to its original 0-degree position, closing the lid.
Finally, a short delay is added in each loop iteration to avoid excessive sensor readings and to ensure stable performance. Overall, this program demonstrates a simple but effective automatic detection and 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