Sorting Machine

Sorting Machine

Introduction

This robotics project focuses on building a device that sorts two different objects using a machine‑learning model from Google Teachable Machine.

This prototype features an automatic feeding system that continuously drops red and yellow balls down a spiral, where they are identified and sorted. A high‑torque DC motor drives the feeding mechanism, while a Raspberry Pi Camera V2 mounted on the side continuously captures images. These images are fed into a machine‑learning model, which then signals two servo motors to physically sort the objects.

This guide describes our specific prototype, but the concepts and steps are applicable to many custom designs.

Parts Used

Essentials

  • CanaKit Raspberry Pi 4 (4GB) Starter PRO Kit
  • Display monitor
  • Wireless keyboard & mouse
  • Long camera cables
  • Raspberry Pi Camera Module V2
  • Coral USB Accelerator
  • USB Flash Drive

Hardware / Electronics

  • 12V DC power supply
  • Jumper wires
  • Small breadboard
  • White LED (optional)
  • Servo motors (2)
  • DC motor
  • Voltage regulator
  • Potentiometer
  • Screwdriver

DIY / Fabrication

  • Hot glue gun
  • Cardboard
  • Digital caliper
  • Plexiglass

Physical Setup

Most of our build was 3D‑printed, but a 3D printer is not strictly required. Alternative DIY material can be used to construct the device.

Main Components

  • Dropping Mechanism: A DC motor drives an Archimedes spiral that feeds the red and yellow balls at a controlled pace. The balls move down a spiral until they are stopped at the trapdoor.
  • Trapdoor Arm: One servo motor holds the object in place until a clear image frame has been captured by the camera. After a prediction has been made, the trapdoor arm lifts up to allow the ball to fall through to the sorting arm.
  • Camera: The camera is mounted to the side of the trapdoor arm to capture images of the balls.
  • Sorting Arm: A second servo motor physically moves the balls left or right based on the prediction from the machine‑learning model.
  • Buckets: Buckets are placed beneath the sorting arm to collect the sorted objects.

Software Setup

Follow the official Raspberry Pi documentation to install Raspberry Pi OS on your Raspberry Pi 4. Make sure to install Raspberry Pi OS Bullseye (2024-10-22) (not Bookworm or Buster). After installation, complete the standard setup process.

Required Software Versions

  • OpenCV: 4.11.0.86
  • NumPy: 1.19.5
  • Picamera2: 0.3.12

Installing Packages

Begin in the home directory and ensure you are in the base environment. Run the following commands exactly as shown:

# 1. Add the Coral Repo and Key
echo "deb https://packages.cloud.google.com/apt coral-edgetpu-stable main" | sudo tee /etc/apt/sources.list.d/coral-edgetpu.list
curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -

# 2. Update and install system dependencies
sudo apt-get update
sudo apt-get install python3-pycoral

# 3. Install Python dependencies 
pip3 install picamera2==0.3.12
pip3 install opencv-contrib-python==4.11.0.86
pip3 install numpy==1.19.5

Note: You may see errors regarding certain “coral cloud” packages; ignore them as long as the install completes. If you run into additional issues, consult the Teachable Sorter | Coral documentation.

Verify Installation

To check that your library versions are correctly installed, run the following commands in the terminal:

python3 -c "import cv2, numpy; print(f'OpenCV: {cv2.__version__}\nNumPy: {numpy.__version__}')"
apt policy python3-picamera2

Note: If your downloads still fail, ensure you're connected and that your time and date settings are up to date.

Camera Setup and Calibration

The Raspberry Pi Camera V2 is used in this project. For reliable performance, ensure:

  • Adequate lighting is present
  • The camera is at least 2 inches away from the object
  • The camera is mounted securely with no movement

Connecting and Focusing

Connect the camera to the Raspberry Pi using the white ribbon cable. Always power off the Raspberry Pi before connecting or disconnecting the camera.

Included with your camera is a white circular focusing tool. Place the narrow end over the lens of the camera and twist until your image is sharp.

Camera Settings

The camera uses auto‑exposure, which may cause overexposed images in low‑light or enclosed environments. To resolve this, you can manually set exposure and gain values in your code to achieve consistent, correctly exposed images. You can view the exposure and gain values we set in our code at the top of the main file.

Creating a Model

Once your physical setup is complete, manually place objects at the trapdoor location to collect training images. Run the script training.py and follow the instructions displayed in the terminal. Be sure to collect images of all objects as well as background images with no objects present. Transfer these images to a device where you can access the internet.

Training the Model

  1. Go to teachablemachine.withgoogle.com and create an Image Project.
  2. Create 3 classes: "Red", "Yellow", and "Background". You can use your different labels so long as you change the labeling variables in code.
  3. Upload your samples to each class and click "Train Model."
  4. Test the model’s accuracy using the preview panel.

Exporting the Model

  1. Click "Export Model" and select the "TensorFlow Lite" tab.
  2. Choose EdgeTPU as the model conversion type and click "Download my model."
  3. Transfer the resulting .zip file to your Raspberry Pi via a USB flash drive or cloud storage.
  4. Unzip your zip file in model_files after you've finished downloading your code from github in the Running Code section.

Retraining Tips

  • Add more high‑quality sample images showing multiple angles of each object.
  • Review existing images and remove blurry or low‑quality frames.
  • Increase the number of training epochs using the Advanced dropdown on the Teachable Machine website and retrain the model.

Hardware Setup

Breadboard Overview

  • All electrical connections are made on a breadboard.
  • If you are unfamiliar with breadboards, watch the following overview video: https://youtu.be/6WReFkfrUIk

Power Supply to Potentiometer

  • Designate one breadboard rail as the 12V supply (the other will later be used for 5V). 
  • Connect the 12V power supply to the breadboard, then connect it to the potentiometer. 
  • Always check the labels on the components before making connections.

Potentiometer to DC Motor

  • Connect the potentiometer output to the DC motor.
  • The motor should now rotate, and its speed should be adjustable via the potentiometer.

Voltage Regulator Connection

  • Provide 12V input to the voltage regulator.
  • Verify pin labels carefully to avoid damage.

Regulator to Servos

  • Use the voltage regulator’s 5V output to power the servo motors.
  • Connect this 5V output to a breadboard rail and use it as the servo power source.
  • DO NOT connect the orange/yellow signal wire to anything yet; this will later connect to the Raspberry Pi for control.

White LED (Optional)

  • Use the raspberry pi’s 3.3V output to power an optional white LED to illuminate your objects for a clear camera view.

Running Code

Download the code repository from GitHub, which should contain 2 folders:

  • The folder named "main" will be the directory for your main sorting program. It contains main.py, module files, and a folder containing the model files we created for our prototype. Remember to replace the files in the model_files folder with the files you've created for your model.
  • The folder named "training" will contain a single training script that you can run to collect training images as discussed previously.

Logic Flowchart

The following flowchart illustrates the logical sequence the sorting program follows, from image capture to motor actuation:

-- Insert Flowchart Here --

You may need to alter values in code such as degree of movement, class names, and exposure, so that the program is geared towards your class names and build.

More: If you’re feeling confident in tackling this project, it is also possible to alter the code to include sorting for multiple classes.