3D Printed Arduino Sorting Machine :: Member Project

What the heck is a 3D Printed Arduino Sorting Machine? It’s what happens when you have some Arduino programming skills, 3D printing ability, and a sweet tooth for red colored skittles! This machine, created completely from scratch, automatically sorts a bag of skittles by color!

3D Printed Arduino Sorting Machine in Action!

While this may be a specific solution to a niche problem, you can imagine what other unique problems you could tackle with these kinds of creative  prototyping skills in your back pocket. Thanks to our academy member Carlos for showing us yet another cool Arduino project!

Read below to learn more about his project… Also, check out his AWESOME youtube video embedded below which takes you step-by-step through the 3D Printed Arduino Sorting Machine!

What components were used in your project? 

  • Arduino Uno
  • Servo Motor
  • Potentiometer
  • Luminosity Sensor
  • AC/DC adapter

Why the heck did you build a 3D Printed Arduino Sorting Machine?

I really love the Red Skittles! Also, I saw this project some time ago and I wanted to give it a try now that I’m more confident in programming Arduino.

Is this project for work or your own personal interest?

The aliens made me do it!

How does your project work?

First, I made an Arduino sketch to help me understand how the TCS3200 sensor works. I watched some YouTube videos and looked for more information on the Internet.

The initial code plans for the 3D Printed Arduino Sorting Machine

I created an initial sketch that measured the values of Red, Green, Blue and Clear present on a colored object. This program would be used as a “User Defined Function” in the completed project.

After that I made a little program with 2 potentiometers to find the correct positions of the Servos. I used functions such as analogRead, Map & Servo library to figure out these values.

First assembly of the 3D Printed Arduino Sorting Machine

Finally I “assembled” the programs to make my machine work. Some calibration was necessary to get a good combination of values to determinate the color value “sensed” and to tell the servo where to move.

Click here to skip down to the code below!

Click here to download the schematic!

What was your biggest struggle as you worked through this project?

The first struggle was to really understand how the TCS3200 worked and how to interpret the output from this sensor.

The second struggle (and the one which made me cry and dream about this project) was the fact that I needed to be very careful about the ambient light. Depending on the hour of the day (and the amount of ambient daylight present) the TCS3200 readings were different, so my parameters were no longer effective.

Did the project end up as you expected?

I made some changes in the end that I was not expecting. In the beginning I wanted to use a small stepper motor, but this didn’t work as expected. The stepper motor was losing steps due to not having enough torque.

I swapped the stepper motor out for a more powerful Servo and it worked much better. In the future, I will try it again with a more powerful Stepper motor.

Looking back on this project, what can you say you have learned about programming and/or electronics through the creation process?

I learned a lot on this project. At the beginning I was afraid even to try. I was always saying “I am not ready for this!” but after some hard work and re-watching some of the lessons on P.E.A. I was able to succeed.

The 3D Printed Arduino Sorting Machine completed in all its glory!

I learned more about User Defined Functions (module 9 in P.E.A.’s Arduino Course for Absolute Beginners) and I used a lot of what I learned here; Map, Switch, if Statement, for statement, libraries, etc.

Thanks Michael and the folks over at P.E.A.!

Was the training at Programming Electronics Academy able to help you build your skill?

Of course! I am very satisfied with the results I achieved. I never thought I would be able to create/understand a program of this type. There is still a long way to go, but this small success encourages me to keep on working hard.

Are there any final notes you would like to say about this project submission that we haven’t covered elsewhere?

I just want to thank Michael again and invite my Programming Electronics colleagues / students to share their projects so we can learn more together.

About Carlos

Carlos is an Electromechanical engineer currently working as a Continuous Improvement engineer. He’s been “into” electronics for the past five years. He started really digging into programming with the Arduino about a month ago.

Carlos also has a YouTube channel you should checkout -> Electromechanical Guy.

Arduino Color Sorting Sketch:

/* A program for a little "Color Sorting Machine"
    This machine will sort the colors of "Skittles"
    A 1st Servo motor will do X steps to put in position the candy,
    then the Color Sensor will perform his job,
    the 2nd Servo will move to the color position
    and then the stepper motor will move again.

    For the project we will use:
    1x Micro Servo: SG90
    1x Servo: MG995
    1x Color Sensor: GY31 TCS3200
    1x Arduino Uno

    Inspired from the code made by Dejan Nedelkovski
    https://www.youtube.com/user/DejanNedelkovski
*/

#include<Servo.h>

//Define the pins of the color sensor
#define s0 2 //S0 pin of the sensor on Arduino pin#2 
#define s1 3 //S1 pin of the sensor on Arduino pin#3 
#define s2 4 //S2 pin of the sensor on Arduino pin#4 
#define s3 5 //S3 pin of the sensor on Arduino pin#5
#define sensorOut 6 //Output pin of the sensor on Arduino pin#6 


//Variable to save the data coming from the sensorOut
int frequency = 0;

// Variable to save the color
byte identifiedColor = 0;

//Declaring Servos

Servo feedingServo;
Servo decisionServo;

//Declaring general delays
byte smallDelay_ms = 5;
byte mediumDelay_ms = 30;
byte bigDelay_ms = 1000;
byte veryBigDelay_ms = 2000;


//Delcaring position of the feederServo
byte pos1FeederServo = 165;
byte pos2FeederServo = 92;
byte pos3FeederServo = 12;

//Delcaring position of the decisionServo
byte decisionServo_RED = 14;
byte decisionServo_ORANGE = 53;
byte decisionServo_GREEN = 85;
byte decisionServo_YELLOW = 115;
byte decisionServo_PURPLE = 165;

void setup() {

  //Set the pins of the Color Sensor
  pinMode (s0, OUTPUT);
  pinMode (s1, OUTPUT);
  pinMode (s2, OUTPUT);
  pinMode (s3, OUTPUT);
  pinMode (sensorOut, INPUT);

  /* The pins S0 & S1 works for the frequency scaling
      L - L = Power Down
      L - H = 2%
      H - L = 20%
      H - H = 100%
  */
  //Using at 100%
  digitalWrite(s0, HIGH);
  digitalWrite(s1, HIGH);

  //Attaching the Servos
  feedingServo.attach (9);
  decisionServo.attach(11);

  //Set the serial communication in bytes per second
  Serial.begin(9600);

}//Close setup

void loop() {

  //Write "steps" of 1 degree to the servo until pos2 (Sensor position)
  for (int i = pos1FeederServo; i > pos2FeederServo ; i--) {

    feedingServo.write (i);
    delay (mediumDelay_ms);

  }//close for loop

  //Delay to stabilize readings
  delay(bigDelay_ms);

  //We launch the Function getTheColor
  identifiedColor = getTheColor();

  //Switch Case to decide wich color are we reading
  /*********IMPORTANT********
   * Please calibrate your Servos and the TCS3200 sensor before uploading this program,
   * Depending on mechanical installation the positions of the servos may change
   * Also the TCS3200 can give you different readings compared to mines
   * because of the ambient lighting (light changes in general)
   */
  switch (identifiedColor) {

    //Case for Red
    case 1:
      decisionServo.write (decisionServo_RED);
      break;

    //Case for Orange
    case 2:
      decisionServo.write (decisionServo_ORANGE);
      break;

    //Case for Green
    case 3:
      decisionServo.write (decisionServo_GREEN);
      break;

    //Case for Yellow
    case 4:
      decisionServo.write (decisionServo_YELLOW);
      break;

    //Case for Purple
    case 5:
      decisionServo.write (decisionServo_PURPLE);
      break;

  }// close Switch Case

  //Delay to exit position
  delay(bigDelay_ms);


  //Move Feeding servo to exit position
  for (int i = pos2FeederServo; i > pos3FeederServo; i--) {

    feedingServo.write (i);
    delay (mediumDelay_ms);

  }//Close for loop - Servo to Exit Position

  //Delay to return servo
  delay(bigDelay_ms);


  //Return the Feeding Servo to initial position
  for (int i = pos3FeederServo; i < pos1FeederServo ; i++) {

    feedingServo.write (i);
    delay (smallDelay_ms);
  }//Close for loop - Servo initial Position


  //Return the Value to 0
  identifiedColor = 0;

  //Delay to begin again
  delay (veryBigDelay_ms);

}//close loop

/*-----User Defined Function-----
   Function Definition:
   getTheColor

   Purpose:
   Apply the filters on the photodiodes to get the R,G&B values

   Return value:
   color      The combination of  R, G , B , C values to decide the color.

    ****Important****
    It is recommended to run the program alone
    With an excel document and the serial monitor open
    check the readings to find the values for each color and change those values below
*/

int getTheColor() {


  //---Read RED values... S2 LOW - S3 LOW---
  digitalWrite(s2, LOW);
  digitalWrite(s3, LOW);
  frequency = pulseIn(sensorOut, LOW);

  int redValue = frequency;

  Serial.print("R=");
  Serial.print(redValue);
  Serial.print("  ");

  delay(50);

  //---Read Green values ... S2 LOW - S3 HIGH---
  digitalWrite(s2, LOW);
  digitalWrite(s3, HIGH);
  frequency = pulseIn(sensorOut, LOW);

  int greenValue = frequency;

  Serial.print("G=");
  Serial.print(greenValue);
  Serial.print("  ");

  delay(50);


  //---Read Blue values ... S2 HIGH - S3 LOW---
  digitalWrite(s2, HIGH);
  digitalWrite(s3, LOW);
  frequency = pulseIn(sensorOut, LOW);

  int blueValue = frequency ;

  Serial.print("B=");
  Serial.print(blueValue);
  Serial.print("  ");

  delay(50);


    //---Read Clear values ... S2 HIGH - S3 HIGH---
  digitalWrite(s2, HIGH);
  digitalWrite(s3, HIGH);
  frequency = pulseIn(sensorOut, LOW);

  int clearValue = frequency ;

  Serial.print("C=");
  Serial.print(clearValue);
  Serial.println("  ");

  delay(50);


  //////////Parameters for Red//////////
  if (redValue<38 & redValue>14 & clearValue<58 & clearValue>=45 ) {
    identifiedColor = 1; // Red
  }

  //////////Parameters for Orange//////////
  if (redValue<27 & redValue>10  & clearValue<45 & clearValue>=35) {
    identifiedColor = 2; // Orange
  }


  //////////Parameters for Green//////////
  if (blueValue<15 & blueValue>4 & clearValue<35 & clearValue>20 ) {
    identifiedColor = 3; // Green
  }

  //////////Parameters for Yellow//////////
  if (redValue<20 & redValue>8 & clearValue<24 & clearValue>10 ) {
    identifiedColor = 4; // Yellow
  }

  //////////Parameters for Purple//////////
  if ( redValue<49 & redValue>39  & clearValue<65 & clearValue>50) {
    identifiedColor = 5; // Purple
  }


  //--- Return the value found---
  return identifiedColor;

}//Close Function getTheColor

installing Arduino libraries

Installing Arduino Libraries | Beginners Guide

IoT sewage project

Pumping poo! An IoT sewage project

ESP32 webOTA updates

How to update ESP32 firmware using web OTA [Guide + Code]

error message Brackets Thumbnail V1

expected declaration before ‘}’ token [SOLVED]

Compilation SOLVED | 1

Compilation error: expected ‘;’ before [SOLVED]

Learn how to structure your code

6 Comments

  1. Jim on June 13, 2020 at 2:23 pm

    Great project! Love how you went through the Project Design process in the beginning. Did you find that this helped cut down on iterations during the build/troubleshooting process?

  2. Kris on June 13, 2020 at 3:27 pm

    Thank you, Mike, for exposing me to this project and its creator, the Electromechanical Guy. I needed it! Currently, I am learning Fusion 360 because I want to utilize 3D printing in my electronics projects, so this idea of a project combining the two skills got me even stronger motivated in pursuing my goal.
    Kudos to you and the Electromechanical Guy!

  3. Josh Gold on November 29, 2020 at 1:04 pm

    This is awesome! Are you able to share the STL files, please?

  4. Robert Love on January 19, 2021 at 1:02 pm

    can you share the STL files?

  5. Marcel Hebert on July 12, 2021 at 10:01 am

    I saw this on Instructables.
    Good luck on the contest.

Leave a Comment