Skip to content

Repository files navigation

JONICA — Color Caps Classifier

A mechatronic machine that classifies soda-bottle caps by color from a camera feed and physically sorts them into per-color outputs in real time. It combines computer vision (OpenCV on a Raspberry Pi), finite-state-machine firmware on an Arduino / ATmega328P, custom PCBs, and 3D-printed mechanical parts.

University engineering-competition project — Escuela de Ingeniería Electrónica, Facultad de Ciencias Exactas, Ingeniería y Agrimensura (FCEIA), Universidad Nacional de Rosario. The brief (set by the course): build a device that uses webcam images to classify caps by color and separate the red ones — minimum red/green/blue, with extra credit for yellow/orange/grey — designed to be scalable to an industrial setting in speed and throughput.

📹 Demo video: https://drive.google.com/open?id=1r29vyWqCW7NpwiLaEyCusYoAQh5E9Sr1


How it works

Hopper ─► paddle presents a cap under the camera + LEDs
        ─► Raspberry Pi (OpenCV, HSV) classifies the color
        ─► color code sent over serial / RS485
        ─► ATmega328P queues it (circular buffer) and drives stepper-actuated levers
        ─► cap is routed in free-fall to the matching output
   (CNY70 reflective sensors track each cap's passage along the way)

The caps are sorted in free-fall — a deliberate choice for speed. Levers driven by stepper motors deflect each falling cap into the right path.

Architecture

Three subsystems integrated into one machine:

1. Vision — Raspberry Pi 3 + PiCamera (Python / OpenCV)

  • HSV color segmentation with per-color cv2.inRange masks (blue, green, yellow, orange, red). Red is matched across two hue ranges to handle the hue wrap-around at 0/180°.
  • Gaussian blur + bilateral filtering to denoise while preserving edges.
  • A calibration tool (calibracion.py) with live HSV trackbars to tune each color band.
  • LED illumination at the capture point for consistent lighting.

2. Control firmware — Arduino / ATmega328P (C/C++)

  • Three finite state machines (one per stepper) plus FSMs for serial reception and cap selection — cooperative, non-blocking design.
  • A4988 micro-stepping drivers (4 in total) for the NEMA 17 steppers; step pulses generated from a Timer1 ISR (1 ms tick).
  • A circular buffer queues detected caps so classification and actuation can run decoupled without losing or mis-routing caps.
  • CNY70 reflective optical sensors detect cap passage (with debounce logic).
  • Timeout handling resets state if a cap is lost mid-cycle.

3. Electronics & mechanics

  • Custom PCBs designed in KiCad and Eagle: CNY70 sensor boards and motor/Pololu shields (Circuito/, Sensor IR/, Shield pololu/).
  • RS485 (MAX485) multipoint bus between the Pi and the Arduino(s): it adapts UART voltage levels, adds noise immunity, and — being multipoint — lets the system scale (one ATmega328P per motor as bus slaves, Raspberry Pi as master) to handle more colors/outputs.
  • Mechanical design in SolidWorks, 3D-printed (Creality CR-10): the classification paddle (which also mounts the camera, LEDs, and its motor), selector levers, motor mounts, and a Raspberry Pi case (Modelos v4/, Caño para seleccionar tapas/). Wooden frame, hopper, and output tube.

Hardware

Part Role
Raspberry Pi 3 Model B + PiCamera Image capture, OpenCV classification, master controller
Arduino UNO / Nano (ATmega328P) Real-time motor control + sensor handling
3× NEMA 17 stepper motors Paddle rotation + cap-selection levers
4× A4988 stepper drivers Micro-stepping motor drive
CNY70 reflective optical sensors Cap-passage detection
MAX485 transceiver RS485 serial link (Pi ↔ Arduino)
LED illumination + PC DC power supply Lighting + system power

Design decisions (from the project report)

  • Free-fall sorting chosen over an arm/turntable classifier (those were too slow/bulky).
  • Stepper motors + levers chosen over servos (too slow) and pneumatic valves (too expensive).
  • RS485 multipoint chosen so the architecture scales to more bins without a redesign.

Repository layout

Path Contents
SRC/ Cleaned-up source: vision (calibracion.py, script-picam-foto.py) + FSM motor firmware (nuevo_motores/)
JONICA Rasp/ Raspberry Pi scripts (PiCamera capture, color selection, demos)
Script/ Earlier vision-script iterations + test assets
programa motores/ Arduino firmware iterations (v1–v14), RS485 transmitter, sensor/comm modules
Motor_paso_a_paso.ino/, Prueba_motor_unipolar/ Stepper-motor test sketches
Circuito/ KiCad PCB projects (v1, v2, shield) + Gerbers / G-code
Sensor IR/, Shield pololu/ Eagle PCB designs
Modelos v4/, Caño para seleccionar tapas/ SolidWorks CAD, STL, and slicer G-code for the printed/mechanical parts
INFORME/ Project reports (Spanish) + state-machine and schematic diagrams

Running the vision module

On a Raspberry Pi (or any machine with a camera) with Python 3 and OpenCV:

pip install opencv-python numpy pyserial
# On a Raspberry Pi, also: picamera + RPi.GPIO

python3 SRC/calibracion.py   # tune the HSV ranges for your caps/lighting
python3 Script/script.py     # send color commands to the Arduino over /dev/ttyAMA0 @ 9600

The code reflects its competition-time state (Spanish comments, some hardcoded paths, multiple preserved iterations). It's published as a hardware/software portfolio reference, not a packaged product.

Team

Jonatan Arroyo · Tomás Ayi · Paula Diaz · Mariano Echavarría — Ingeniería Electrónica, UNR.

About

A project made mostly in python to classifie color caps using OpenCV

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages