Thursday, 6 August 2026

AI Based Trash Segregation Robotic ARM using Computer Vision

Here is the complete step-by-step engineering documentation and implementation guide for building an AI-Based Trash Segregation Robotic Arm using ESP32, Computer Vision, n8n Automation, Telegram Voice Alerts, and Cloud Analytics.


1. Full Project Description

This system automates waste segregation using Edge/Cloud AI and Agentic IoT workflow automation.

  • Computer Vision & Classification: An ESP32-CAM or USB Camera captures images of waste arriving on a conveyor or platform. The frame is evaluated using a lightweight Object Detection Model (YOLOv8 / Teachable Machine model deployed locally or via API).

  • Actuation Mechanics: Based on the detected class (Biodegradable, Non-Biodegradable / Plastic, Metal, Hazardous), the ESP32 micro-controller drives a multi-DOF servo robotic arm to pick and place the item into its respective bin.

  • Agentic IoT & n8n Workflow Automation: Upon each classification, event telemetry is dispatched via HTTP POST/MQTT to an n8n self-hosted instance. An Agentic AI node evaluates continuous operational logs.

  • Cloud Logging & Visualization:

    • ThingSpeak: Logs real-time sensor parameters (current, servo angles, object counts, operational power).

    • Google Sheets: Acts as a relational event database tracking historical sorting records and timestamps.

  • Telegram Voice Alerts: n8n converts critical alerts (e.g., bin overflow, motor stall, high power consumption) to speech using TTS (ElevenLabs / OpenAI TTS) and sends .ogg voice notes to a Telegram group.

2. Components List

Hardware Components

  1. ESP32 DevKit V1 (Main Microcontroller)

  2. ESP32-CAM Module (Vision capture node)

  3. 4-DOF or 6-DOF Robotic Arm Kit (with MG996R or SG90 Servos)

  4. PCA9685 16-Channel 12-bit PWM Servo Driver (I2C interface)

  5. ACS712 Current Sensor (5A) (For power & load monitoring)

  6. HC-SR04 Ultrasonic Sensors x3 (Bin full level detection)

  7. 5V 5A High-Current DC Power Supply (Dedicated for servos)

  8. 5V 2A Micro-USB Supply (For ESP32 boards)

  9. Logic Level Shifter (3.3V to 5V) (Optional, for sensor safety)

Software & Cloud Stack

  1. Arduino IDE / PlatformIO (ESP32 Firmware)

  2. n8n (Self-hosted workflow automation platform)

  3. ThingSpeak API (Telemetry dashboard)

  4. Google Sheets API (Data logging)

  5. Telegram Bot API (Voice and text alerts)

  6. OpenCV / Roboflow / Teachable Machine / Ollama (Image Classification Agent)

3. Circuit Schematic Diagram

Hardware Wiring Connections

Component Pin / Terminal ESP32 Board
PCA9685 Servo Driver VCC 3.3V / 5V

GND GND

SDA GPIO 21

SCL GPIO 22
ACS712 Current Sensor VCC 5V

GND GND

OUT GPIO 34 (Analog In)
Ultrasonic Sensor (Bin 1) Trig / Echo GPIO 12 / GPIO 13
Ultrasonic Sensor (Bin 2) Trig / Echo GPIO 14 / GPIO 27
Servos (Base, Shoulder, Elbow, Gripper) Channel 0 - 3 Wired to PCA9685
External Power (5V 5A) V+ / V- PCA9685 Power Screw Terminal

Crucial Power Rule: NEVER power the servo motors directly from the ESP32 5V/3.3V pins. Connect external 5V 5A directly to the PCA9685 terminal block, and ensure a common ground between the external power supply and the ESP32.

4. Flowchart

 [Start] --> [ESP32-CAM Captures Image]
                 |
                 v
     [Send Image to AI Agent / Classifier]
                 |
                 v
     [Object Class Identified?]
        /        |        \
   (Plastic)  (Metal)   (Bio-degradable)
      /          |          \
 [Bin A Pos] [Bin B Pos]  [Bin C Pos]
      \          |          /
                 v
   [PCA9685 Drives Servos to Place Item]
                 |
                 v
   [ACS712 Reads Current + Power Consumption]
                 |
                 v
   [HTTP POST Payload sent to n8n Webhook]
                 |
         +-------+-------+
         |               |
         v               v
  [Google Sheets]  [ThingSpeak]
  (Row Added)     (Fields Updated)
                         |
                         v
          [n8n Agentic AI Evaluates Metrics]
                         |
          {Is Bin Full OR High Power Spike?}
                     /        \
                  (Yes)       (No)
                   /            \
     [Generate TTS Voice]    [End Cycle]
                   |
      [Telegram Voice Alert]

5. ESP32 Source Code

Flash this C++ code onto your main ESP32 DevKit V1 using the Arduino IDE. Make sure to install the Adafruit_PWMServoDriver and WiFi libraries.

C++

#include <WiFi.h>
#include <HTTPClient.h>
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>

// WiFi Configuration
const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";

// n8n Webhook Endpoint
const char* n8n_webhook_url = "http://YOUR_N8N_IP:5678/webhook/trash-segregation";

// PCA9685 PWM Setup
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
#define SERVOMIN  150 // Minimum pulse length count out of 4096
#define SERVOMAX  600 // Maximum pulse length count out of 4096

// Sensor Pins
#define CURRENT_SENSOR_PIN 34

void setup() {
  Serial.begin(115200);
  Wire.begin(21, 22); // SDA, SCL
  pwm.begin();
  pwm.setPWMFreq(60); // Analog servos run at ~60Hz

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("\nWiFi Connected!");
  
  // Set default home position
  moveHome();
}

void loop() {
  if (Serial.available() > 0) {
    String input = Serial.readStringUntil('\n');
    input.trim();
    
    if (input.startsWith("SORT:")) {
      String trashType = input.substring(5);
      float currentmA = readCurrent();
      float powerWatts = (currentmA / 1000.0) * 5.0; // P = V * I
      
      executeSorting(trashType);
      sendTelemetryToN8N(trashType, currentmA, powerWatts);
    }
  }
}

void setAngle(uint8_t num, double angle) {
  double pulse = map(angle, 0, 180, SERVOMIN, SERVOMAX);
  pwm.setPWM(num, 0, pulse);
}

void moveHome() {
  setAngle(0, 90);  // Base
  setAngle(1, 45);  // Shoulder
  setAngle(2, 45);  // Elbow
  setAngle(3, 0);   // Gripper Open
}

void executeSorting(String category) {
  // Pick sequence
  setAngle(3, 90);  // Close Gripper
  delay(500);
  setAngle(1, 90);  // Lift arm
  delay(500);

  // Rotate base to designated bin
  if (category == "PLASTIC") setAngle(0, 30);
  else if (category == "METAL") setAngle(0, 90);
  else if (category == "ORGANIC") setAngle(0, 150);
  else setAngle(0, 180); // Default/Unknown

  delay(1000);
  setAngle(3, 0);   // Drop item
  delay(500);
  
  moveHome();       // Return to ready state
}

float readCurrent() {
  int rawADC = analogRead(CURRENT_SENSOR_PIN);
  float voltage = (rawADC / 4095.0) * 3.3;
  // Offset for ACS712 5A module (VCC/2 centered, ~185mV/A sensitivity)
  float currentA = (voltage - 1.65) / 0.185; 
  return abs(currentA * 1000.0); // Return mA
}

void sendTelemetryToN8N(String type, float current, float power) {
  if (WiFi.status() == WL_CONNECTED) {
    HTTPClient http;
    http.begin(n8n_webhook_url);
    http.addHeader("Content-Type", "application/json");

    String jsonPayload = "{";
    jsonPayload += "\"waste_type\":\"" + type + "\",";
    jsonPayload += "\"current_mA\":" + String(current) + ",";
    jsonPayload += "\"power_W\":" + String(power);
    jsonPayload += "}";

    int httpResponseCode = http.POST(jsonPayload);
    Serial.print("n8n Webhook Response: ");
    Serial.println(httpResponseCode);
    http.end();
  }
}

6. n8n Workflow JSON

Save the block below as a .json file and import it directly into your n8n canvas via Workflow -> Import from File.

JSON

{
  "name": "AI Trash Segregation & Alert Workflow",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "trash-segregation",
        "options": {}
      },
      "id": "node-1",
      "name": "ESP32 Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [250, 300]
    },
    {
      "parameters": {
        "operation": "append",
        "sheetId": "YOUR_GOOGLE_SHEET_ID",
        "range": "Sheet1!A:C",
        "options": {}
      },
      "id": "node-2",
      "name": "Google Sheets",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4,
      "position": [500, 200]
    },
    {
      "parameters": {
        "requestMethod": "GET",
        "url": "=https://api.thingspeak.com/update?api_key=YOUR_THINGSPEAK_WRITE_KEY&field1={{$json.body.current_mA}}&field2={{$json.body.power_W}}"
      },
      "id": "node-3",
      "name": "ThingSpeak Update",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [500, 400]
    },
    {
      "parameters": {
        "conditions": {
          "number": [
            {
              "value1": "={{ $json.body.power_W }}",
              "operation": "larger",
              "value2": 2.5
            }
          ]
        }
      },
      "id": "node-4",
      "name": "Check Anomaly",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [720, 300]
    },
    {
      "parameters": {
        "chatId": "YOUR_TELEGRAM_CHAT_ID",
        "text": "=⚠️ ALERT: High Servo Power Consumption Detected! Voltage Draw: {{ $json.body.power_W }} W. Check for motor jam."
      },
      "id": "node-5",
      "name": "Telegram Alert",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1,
      "position": [950, 200]
    }
  ],
  "connections": {
    "ESP32 Webhook": {
      "main": [
        [
          { "node": "Google Sheets", "type": "main", "index": 0 },
          { "node": "ThingSpeak Update", "type": "main", "index": 0 },
          { "node": "Check Anomaly", "type": "main", "index": 0 }
        ]
      ]
    },
    "Check Anomaly": {
      "main": [
        [
          { "node": "Telegram Alert", "type": "main", "index": 0 }
        ]
      ]
    }
  }
}

7. Setup & Configurations

1.Telegram Bot Setup:Create Bot & Obtain API Tokens.
  1. Search for @BotFather in Telegram and start a chat.

  2. Send /newbot, name your bot, and save the generated HTTP API Token.

  3. Search for @userinfobot, press /start, and copy your personal Id (Chat ID).

  4. Paste these credentials into the n8n Telegram Node settings.

2.Google Sheets Integration:Setup Cloud Logging Database.
  1. Go to Google Cloud Console and enable the Google Sheets API.

  2. Create a Service Account, download the JSON key file, and link it inside n8n under OAuth2/Service Account credentials.

  3. Create a Google Sheet with headers: Timestamp | Waste Category | Current (mA) | Power (W).

  4. Share the sheet with the Service Account email address giving Editor permission.

3.ThingSpeak Dashboard Setup:Real-time Visualization.
  1. Sign up at ThingSpeak.com.

  2. Create a New Channel named AI Trash Segregation System.

  3. Enable two fields: Field 1: Current (mA), Field 2: Power (Watts).

  4. Copy the Write API Key and paste it into the n8n HTTP Request node URL.

4.AI Power Consumption Prediction Logic:Agentic Anomaly Detection.

The ACS712 sensor streams current data. If a servo gets jammed by heavy waste:

  • Standard operational draw = 0.2A to 0.5A (1.0W - 2.5W).

  • Stall current = > 1.2A (> 6.0W).

  • The n8n agent detects values over threshold and dynamically routes an urgent speech warning to Telegram.

5.Voice Notification Automation:Text-to-Speech Engine.
  1. Inside n8n, send the anomaly prompt text to an OpenAI TTS node (tts-1 model) or ElevenLabs API.

  2. Set output audio encoding to .ogg / OPUS.

  3. Wire the resulting audio binary to the Telegram Node (Send Audio / Voice Note).

8. Future Enhancements & Deployment Guide

  1. Edge AI Processing: Replace cloud-based image inference with Edge Impulse / ESP32-S3 Eye or a Raspberry Pi 4/5 running local YOLOv8-nano to remove latency completely.

  2. Reverse Kinematics: Upgrade from hardcoded angular movements to inverse kinematics (IK) trajectories for smooth motion profiles and adaptive grip height.

  3. Solar & Battery System: Add an 18650 Li-ion battery backup array with solar charging for off-grid municipal deployment.


No comments:

Post a Comment