
1. Project Overview
A smart sorting bin that uses a USB webcam connected to the computer and a Google Teachable Machine model to tell paper from plastic. When the model is at least 90% sure, a web page sends one letter over Web Serial to an Arduino UNO. The Arduino moves the servo flap toward the right compartment and lights the LED for that type of waste. A third class, Nothing, means there is nothing in front of the camera. It is a suggested, reproducible design, not a tested product.
2. How It Works
The page opens the camera and loads the model from Teachable Machine (model link). For every frame it computes the probability of each class. If the same class stays on top at 90% or more for 5 frames in a row, the page sends one letter: P for paper, L for plastic, N for nothing. The Arduino reads the letter at 9600 baud, lights the matching LED and moves the servo to its angle, then returns the servo to neutral after 1.5 seconds. The model runs on the computer, not on the Arduino. The Arduino only carries out one letter per result.
3. Components Required
- Arduino UNO x 1 - 🛒 Add to cart
- USB cable Type-A to Type-B 30cm x 1 - 🛒 Add to cart
- USB webcam 720p with microphone x 1 - 🛒 Add to cart
- SG90 servo motor x 1 - 🛒 Add to cart
- Red LED 5mm (1 piece) x 1 - 🛒 Add to cart
- Green LED 5mm (1 piece) x 1 - 🛒 Add to cart
- Resistor 220 ohm 1/4W (1 piece each) x 2 - 🛒 Add to cart
- Breadboard 830 points x 1 - 🛒 Add to cart
- Male-to-male jumper wires 20cm x 1 set - 🛒 Add to cart
- Cardboard or printed bin with two compartments and a sorting flap x 1 - not in the store yet, to be added
Click here to open the components table
4. Circuit Diagram
Make the connections according to the diagram: servo signal to Arduino D9, servo V+ to 5V, servo GND to GND. D7 goes through a 220 ohm resistor to the Plastic LED (+), D8 through a 220 ohm resistor to the Paper LED (+), and both LED (-) legs to GND. The webcam and the Arduino USB cable both plug into the computer.
Click here to open the wiring diagram
Diagram colors: orange = servo signal D9, pink = Paper LED D8, purple = Plastic LED D7, red = 5V, black = GND, gray = USB. Fritzing parts are licensed CC BY-SA. This is a teaching diagram, not a photo of a tested build.
5. Wiring Table
Read each row from the From pin to the To pin and check power, ground and signals before you connect the USB cable. Both LED (-) legs and the servo GND join the Arduino GND.
Click here to open the wiring table
6. Connection Steps
Wire the project in this order, with the USB cable unplugged. The table has the same steps with a check for each.
- Upload waste_sorting_bin.ino from the Arduino IDE to the UNO, then close the Serial Monitor, because the port cannot be shared with the web page.
- Wire the circuit as in the wiring table with a common GND, and check that each LED has a 220 ohm resistor.
- Save the web page code as index.html and run it from a local server (for example python -m http.server), then open it in Chrome or Edge.
- Click Connect Arduino and pick the port, then click Start Camera and allow camera access.
- Show a sheet of paper, then a piece of plastic, to the camera.
Click here to open the connection steps table
7. Source Code
Upload this code to the Arduino UNO (file name waste_sorting_bin.ino). It reads P, L or N over Serial at 9600. The servo angles are defaults: change PAPER_ANGLE and PLASTIC_ANGLE to match your bin.
#include <Servo.h>
const byte SERVO_PIN = 9;
const byte PLASTIC_LED_PIN = 7;
const byte PAPER_LED_PIN = 8;
const byte NEUTRAL_ANGLE = 90;
const byte PAPER_ANGLE = 45; // adjust to your bin
const byte PLASTIC_ANGLE = 135; // adjust to your bin
const unsigned long HOLD_MS = 1500;
Servo sorter;
unsigned long returnAt = 0;
void showResult(char c) {
if (c == 'P') {
digitalWrite(PAPER_LED_PIN, HIGH);
digitalWrite(PLASTIC_LED_PIN, LOW);
sorter.write(PAPER_ANGLE);
returnAt = millis() + HOLD_MS;
} else if (c == 'L') {
digitalWrite(PLASTIC_LED_PIN, HIGH);
digitalWrite(PAPER_LED_PIN, LOW);
sorter.write(PLASTIC_ANGLE);
returnAt = millis() + HOLD_MS;
} else if (c == 'N') {
digitalWrite(PAPER_LED_PIN, LOW);
digitalWrite(PLASTIC_LED_PIN, LOW);
sorter.write(NEUTRAL_ANGLE);
returnAt = 0;
}
}
void setup() {
Serial.begin(9600);
pinMode(PLASTIC_LED_PIN, OUTPUT);
pinMode(PAPER_LED_PIN, OUTPUT);
sorter.attach(SERVO_PIN);
showResult('N');
}
void loop() {
while (Serial.available() > 0) {
showResult((char)Serial.read());
}
if (returnAt != 0 && (long)(millis() - returnAt) >= 0) {
sorter.write(NEUTRAL_ANGLE);
returnAt = 0;
}
}
Save this page as index.html (HTML, JavaScript, TensorFlow.js, Teachable Machine and Web Serial). Open it in Chrome or Edge through http://localhost or https, click Connect Arduino, then Start Camera. Web Serial does not work in Firefox or Safari.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>AI Smart Waste Sorting Bin</title>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@1.3.1/dist/tf.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@teachablemachine/image@0.8/dist/teachablemachine-image.min.js"></script>
</head>
<body>
<h2>AI Smart Waste Sorting Bin</h2>
<button id="connectBtn">Connect Arduino</button>
<button id="startBtn">Start Camera</button>
<div id="webcam"></div>
<div id="label">Waiting...</div>
<script>
const MODEL_URL = "https://teachablemachine.withgoogle.com/models/qyoLJzorq/";
const THRESHOLD = 0.9;
const STABLE_FRAMES = 5;
const COMMANDS = { paper: "P", plastic: "L", nothing: "N" };
let model, webcam, writer;
let candidate = "";
let count = 0;
let lastSent = "";
document.getElementById("connectBtn").onclick = async () => {
const port = await navigator.serial.requestPort();
await port.open({ baudRate: 9600 });
writer = port.writable.getWriter();
document.getElementById("label").textContent = "Arduino connected";
};
document.getElementById("startBtn").onclick = async () => {
model = await tmImage.load(MODEL_URL + "model.json", MODEL_URL + "metadata.json");
webcam = new tmImage.Webcam(300, 300, true);
await webcam.setup();
await webcam.play();
document.getElementById("webcam").appendChild(webcam.canvas);
window.requestAnimationFrame(loop);
};
async function loop() {
webcam.update();
await predict();
window.requestAnimationFrame(loop);
}
async function predict() {
const predictions = await model.predict(webcam.canvas);
let best = predictions[0];
for (const p of predictions) {
if (p.probability > best.probability) best = p;
}
const name = best.className.toLowerCase();
document.getElementById("label").textContent =
best.className + " " + (best.probability * 100).toFixed(0) + "%";
if (best.probability < THRESHOLD) { count = 0; return; }
if (name === candidate) { count++; } else { candidate = name; count = 1; }
if (count >= STABLE_FRAMES && COMMANDS[name] && name !== lastSent) {
await send(COMMANDS[name]);
lastSent = name;
}
}
async function send(ch) {
if (!writer) return;
await writer.write(new TextEncoder().encode(ch));
}
</script>
</body>
</html>
Still to verify: the class names inside the model (Paper, Plastic, Nothing), the servo direction and angles, stable classification under different light, and Web Serial in your browser.
8. Testing & Troubleshooting
- Nothing: nothing in front of the camera; both LEDs off and the servo at neutral 90 degrees.
- Paper: show paper; the Paper LED lights and the servo moves to the paper angle, then returns.
- Plastic: show a piece of plastic; the Plastic LED lights and the servo moves to the plastic angle, then returns.
- Low confidence: an unclear object; no command is sent below 90%.
- Serial not connecting: close the Serial Monitor, use Chrome or Edge, and check 9600 baud.
- Servo jitter or reset: if the UNO restarts when the servo moves, use an external 5V supply with a common GND.
- Wrong direction: swap the PAPER_ANGLE and PLASTIC_ANGLE values in the code.
9. Improvements
- Add a third class (for example metal) after training the model, with its own letter, LED and angle.
- Add a distance sensor so classification starts only when an object is present.
- Show the class name and confidence on an LCD.
- Train the model with more images from different backgrounds and light to raise accuracy.
10. Safety
The code, wiring, servo angles and model accuracy are not verified until the project is built and tested. This is a low-voltage 5V DC project only and is never connected to mains voltage. Model link: teachablemachine.withgoogle.com/models/qyoLJzorq

Comments & discussion (0)