An AI-powered autonomous garbage collection robot using ESP32, computer vision, and environmental monitoring.
- AI-based Object Detection: Utilizes YOLO with a laptop webcam for real-time trash detection
- Autonomous Operation: Runs without manual intervention using ESP32 microcontroller
- Environmental Monitoring: Tracks temperature, humidity, and smoke levels
- USB Serial Communication: Direct laptop-to-ESP32 communication via USB
- Failsafe Mechanism: Automatically stops if communication is lost
- Robotic Arm: Picks up and disposes of detected trash automatically
- Trash Detection: Identifies trash using AI-powered computer vision
- Navigation: Moves autonomously towards detected objects with ESP32 control
- Obstacle Avoidance: Uses ultrasonic sensors for safe movement
- Trash Collection: Picks up trash using a 3-axis robotic arm with servo motors
- Trash Disposal: Places collected trash into a designated bin
- Environmental Monitoring: Logs temperature, humidity, and smoke data to serial output
- Real-time Monitoring: Live video feed with detection overlays
- Laptop camera captures live video feed
- Python script processes frames using YOLO AI model
- Detection results sent to ESP32 via USB serial connection
- ESP32 controls robot movement, arm, and sensors
- Environmental data logged to serial monitor for debugging
- Real-time feedback displayed on laptop screen
- Laptop → ESP32: Movement commands (
F,R,S,C,H) - ESP32 → Laptop: Status messages and sensor data via serial
ESP32 Pin → L298N Pin
GPIO 2 → IN1 (Left Motor Forward)
GPIO 4 → IN2 (Left Motor Backward)
GPIO 16 → IN3 (Right Motor Forward)
GPIO 17 → IN4 (Right Motor Backward)
GPIO 5 → ENA (Left Motor Enable)
GPIO 18 → ENB (Right Motor Enable)
ESP32 Pin → Sensor
GPIO 12 → HC-SR04 TRIG
GPIO 14 → HC-SR04 ECHO
GPIO 23 → DHT22 Data
GPIO 35 → MQ-2 Analog Out
ESP32 Pin → Servo
GPIO 25 → Base Servo Signal
GPIO 26 → Arm Servo Signal
GPIO 27 → Gripper Servo Signal
ESP32 VIN → L298N +12V (Battery Positive)
ESP32 GND → Common Ground
ESP32 3.3V → Sensors VCC
-
Install Arduino IDE (version 1.8.x or 2.x)
-
Add ESP32 Board Support:
- Go to File → Preferences
- Add to Additional Board Manager URLs:
https://dl.espressif.com/dl/package_esp32_index.json - Go to Tools → Board → Boards Manager
- Search "ESP32" and install "ESP32 by Espressif Systems"
-
Install Required Libraries:
- DHT sensor library (for temperature/humidity) - ESP32Servo (for servo motor control)
-
Install Python 3.8+ from python.org
-
Install required packages:
pip install -r requirements.txt
-
Verify installations:
python -c "import cv2, ultralytics, serial, supervision; print('All packages installed successfully')"
-
Configure ESP32 code:
// No additional configuration needed for basic operation // The code is ready to use with USB serial communication
-
Upload ESP32 code:
- Select "ESP32 Dev Module" as board
- Choose correct COM port
- Upload the code
| Component | Specification | Purpose |
|---|---|---|
| Laptop/Desktop | Windows 10/11, 8GB+ RAM | AI processing and control |
| Built-in/USB Webcam | 720p or higher resolution | Live video feed for detection |
| USB Port | USB 2.0/3.0 | Serial connection to ESP32 |
| Python 3.8+ | Latest version recommended | Running detection script |
- Laptop ↔ ESP32: USB cable for serial communication
- ESP32: Controls all robot hardware (motors, sensors, arm)
- Power: Independent battery system for robot mobility
-
Build the chassis:
- Mount motors to the chassis
- Attach wheels to motors
- Install battery holder
-
Mount ESP32 and components:
- Secure ESP32 to chassis
- Mount L298N motor driver
- Install sensors in appropriate locations
-
Assemble robotic arm:
- Mount base servo to chassis
- Attach arm servo to base
- Install gripper servo at end
- Create simple gripper mechanism
-
Complete wiring according to the diagram above
-
Power connections:
- Connect 18650 batteries in series (7.4V)
- Add power switch for safety
- Ensure proper voltage regulation
-
Configure ESP32 code:
- No WiFi or IoT setup required
- Code is ready for USB serial communication
-
Upload ESP32 code:
- Select "ESP32 Dev Module" as board
- Choose correct COM port
- Upload the code
-
Configure Python script:
- Ensure
best.ptYOLO model is in the project folder - The script will auto-detect the ESP32 COM port
- If auto-detection fails, manually set the port in the code
- Ensure
-
Train your model (or use pre-trained):
- Use YOLOv8 for garbage detection
- Train on garbage/trash dataset
- Export as
best.pt
-
Place model file:
- Copy
best.ptto project directory - Ensure path is correct in
test.py
- Copy
-
Connect hardware:
- Connect ESP32 to laptop via USB cable
- Ensure robot is powered with batteries
- Check ESP32 connection in Device Manager (should show COM port)
-
Position laptop:
- Place laptop where camera has clear view of operating area
- Ensure laptop is plugged in or has sufficient battery
- Keep USB cable connected to ESP32
-
Power on the robot:
- Turn on robot battery power switch
- ESP32 should initialize (check status LED)
- Verify ESP32 serial connection to laptop
-
Start Python detection on laptop:
cd C:\HARDARE\MicroLabGarbageCollector python test.py
- Python script will auto-detect ESP32 COM port
- Camera window should open showing live feed
- Wait for "System initialized successfully!" message
-
Monitor system status:
- Check serial monitor in Arduino IDE for ESP32 status
- Environmental sensor data will be logged to serial output
- Watch for any error messages or warnings
-
Activate autonomous mode:
- System automatically starts when Python script detects trash
- Robot will begin searching for garbage
- Monitor progress through laptop video feed
-
Detection Phase:
- Laptop camera continuously scans environment
- YOLO AI identifies trash objects in real-time
- Detection results shown in laptop video window
-
Command Phase:
- Python script sends movement commands to ESP32 via USB
- Commands:
F(forward),R(rotate),S(stop),C(collect)
-
Robot Response:
- ESP32 receives commands and controls robot hardware
- Motors move robot toward detected trash
- Ultrasonic sensor prevents collisions
- Robotic arm activates for collection
-
Monitoring Phase:
- Environmental data logged to ESP32 serial monitor
- System status messages printed to serial output
- Live video feed on laptop shows progress
- Search Mode: Robot rotates to scan for garbage
- Approach Mode: Moves toward detected trash
- Collection Mode: Uses robotic arm to pick up trash
- Disposal Mode: Moves to disposal area and drops trash
Manual Controls (when Python script is active):
ESC: Emergency stop and exitS: Stop robot movementC: Force trash collection sequence
Serial Monitor Controls (Arduino IDE):
- View real-time sensor data
- Monitor system status messages
- Debug communication issues
- Temperature: Real-time monitoring via serial output
- Humidity: Environmental conditions logged
- Smoke Level: Fire/smoke detection with alerts
- Distance: Obstacle detection range
- Connection Status: USB serial communication status
- Robot State: Searching/Moving/Collecting (via serial messages)
- Detection Count: Number of items detected
- Frame Rate: Video processing speed
-
ESP32 won't start properly:
- Check battery voltage (should be 7.4V)
- Verify USB connection to laptop
- Check serial monitor for error messages
- Ensure all libraries are installed
-
Serial communication fails:
- Verify COM port in Device Manager
- Check USB cable connection
- Restart both devices
-
Camera not detected:
- Close other applications using camera
- Check camera permissions
- Try different USB port
-
Motors not responding:
- Check power supply voltage
- Verify L298N connections
- Test with multimeter
-
Servo arm not moving:
- Check servo power supply
- Verify PWM connections
- Test individual servos
ESP32 Serial Monitor:
F - Move forward
R - Rotate right
S - Stop
C - Collect trash
H - Heartbeat
Python Debug:
# Test camera only
python -c "import cv2; cap = cv2.VideoCapture(0); print('Camera OK' if cap.isOpened() else 'Camera Error')"
# Test serial connection
python -c "import serial.tools.list_ports; [print(p.device, p.description) for p in serial.tools.list_ports.comports()]"Edit the YOLO model training to detect specific types of garbage:
- Plastic bottles
- Cans
- Paper waste
- Organic waste
In MicroLabGarbageCollector.ino:
// Motor speed (0-255)
analogWrite(MOTOR_LEFT_ENABLE, 200);
// Detection distance threshold
if (getDistance() > 20) {- Define new pins in ESP32 code
- Add sensor reading functions
- Update serial output messages
- Test sensor functionality
- Battery voltage levels
- Motor brush condition
- Sensor calibration
- Servo arm alignment
- USB connection stability
- Clean camera lens regularly
- Remove debris from wheels
- Check for loose connections
- Update software as needed
- Fork the repository
- Create feature branch
- Commit changes
- Push to branch
- Create Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
For issues and questions:
- Create an issue on GitHub
- Check the troubleshooting section
- Review hardware connections
- Verify software dependencies
- GPS navigation for larger areas
- Multi-camera setup for 360° vision
- AI-powered path planning
- Trash sorting capabilities
- Solar panel charging
- Mobile app for direct control
- Fleet management for multiple robots
Happy Building!
Remember to test each component individually before integrating the complete system.