Build a color-sorting machine with Arduino

FunduinoPublished on

Schokolinsen automatisch nach Farbe sortieren

Bei diesem Projekt baust du eine Sortiermaschine für farbige Schokolinsen. Ein Farbsensor TCS3200 erkennt die Farbe, ein Servomotor schwenkt die Linse in das passende Fach.

Die Anleitung geht in mehreren Etappen vor: Zuerst testest du den Farbsensor allein, dann die beiden Servomotoren, anschließend kalibrierst du die Farbwerte und zum Schluss setzt du alles zu einem Programm zusammen.

Beachte beim Kalibrieren: Die Farbwerte hängen stark vom Umgebungslicht ab. Du musst die Werte an deinem Aufbau neu ermitteln, die Zahlen aus der Anleitung sind nur Richtwerte.

Finished color-sorting machine with sorted chocolate candies
The finished sorting machine

How the sorting machine works

Unsorted chocolate candies of different colors are placed in a container and then scanned one by one. You don't need a color sensor module for this: the color is analyzed based on the intensity of light reflected from a colored LED.

The process in detail:

  • The first candy falls from the candy container into a recess in the turntable.
  • The turntable moves the candy in front of the scanning area using a servo motor.
  • The candy is illuminated in sequence with red, green, and blue light from the RGB LED. At the same time, the intensity of the reflected light is measured for all three colors. A red candy reflects red light very well, but only a little green light.
  • The microcontroller compares the measurements and determines the color.
  • The candy chute turns toward the storage compartment for the matching color.
  • The turntable rotates downward, the chocolate candy falls onto the collection funnel, and slides into its compartment.
  • The turntable returns to its starting position, and the next candy slides into place.
Exploded view of the color sorting machine with numbered parts
The components at a glance
Candy falls from the container into the recess in the turntable.
Step 1: Load the candy
The rotary wheel turns the candy in front of the scanning area
Step 2: turn in front of the sensor
1 / 3

Required materials

  • 1× microcontroller, e.g. Funduino UNO – Arduino-compatible
  • 1× breadboard (small)
  • 20× breadboard jumper wires, male/female
  • 20× breadboard jumper wires, male/male
  • 1× RGB LED
  • 1× photoresistor
  • 1× 1 kΩ or 10 kΩ resistor
  • 2× TowerPro SG90 or better servo motors
  • 3D-printed parts: base plate with individual compartments, base carrier for motors and sensors, cover plate with candy container, candy chute, rotary wheel

Assembly: populate the base carrier

The base plate is fitted with the servo motor for the wheel, an RGB LED, and a photoresistor. Secure the photoresistor and RGB LED with a drop of glue, and the servo motor with the included screws.

Overview of the sorting machine setup
Assembly instructions
Base plate equipped with a servo motor, RGB LED, and photoresistor
Assembled base carrier
1 / 2

Assembly: Mount the slide and servo

A servo horn - also called a servo bracket, servo lever, or servo arm - is attached to the underside of the candy chute. Trim it slightly to make it fit. Push the chute onto the servo without gluing or screwing it in place: it will be secured later with a small screw once the correct servo position has been found.

Slide the servo motor, together with the assembled chute, into the base frame from the side and secure it with screws from underneath. Then insert the preassembled base frame into the base plate.

Candy chute with a shortened servo horn on the underside
Slide with servo horn
Servomotor with slide, pushed sideways into the base holder
Insert the servo with the slide
Preassembled base support in the base plate
Insert the base carrier
1 / 3

Design: Rotary wheel and cover plate

The rotary knob also gets a shortened servo horn and is placed on the servo in the base mount - again without glue or screws until the position is finalized.

Place the cover plate with the candy container on top of the assembly. It is not secured now or later, as you’ll need to remove it repeatedly during programming. Besides holding the candies, it also covers the sensor area to keep ambient light out.

Rotary wheel with shortened servo horn on the servo
Mount the rotary knob
Cover plate with candy container on the structure
Place the cover plate on top.
1 / 2

Wiring

Connect:

  • 2× servo motor - two wires for the power supply and one signal wire
  • 1× RGB LED - three contacts for red, green, and blue, plus a shared wire for GND
  • 1× photoresistor and 1× resistor (1 kOhm or 10 kOhm) - together, they form a voltage divider. The voltage between them is measured and changes with the light intensity.

The circuit diagrams are available for two different breadboards.

Circuit diagram of the color sorting machine, version 1
Circuit diagram for the first breadboard
Wiring diagram of the color sorting machine, Variant 2
Circuit diagram for the second breadboard
1 / 2

Position the rotary knob

First, separate the rotary wheel from the servo motor and use the first sketch to turn the motor to its home position. Then you can mount and screw the rotary wheel in place.

Next, fine-tune the three positions: “Load” is the top position, where the candy falls into the recess. “Scan” is the position in front of the sensor area. “Ext” is the position where the rotary wheel turns downward, allowing the candy to fall into the funnel.

The second sketch moves to the three positions in sequence. If they don’t match, adjust the rotation angles.

Turn the rotary knob to the starting position.
#include <Servo.h>
Servo Turntable; // The servo is called Turntable.
// Servo positions
int Load=0; // The position named “Load” has a rotation angle of 0.
void setup()
{
Turntable.attach(4); // Connect the servo to pin 4.
}
void loop()
{
Turntable.write(Load); // Load the position.
delay(1000); // Wait until the position is reached.
}
Move to the three positions of the rotary knob
#include <Servo.h>
Servo Drehrad; // The servo is called Drehrad.
// Servo positions
int Load=0; // Rotation angle for loading the candy
int Scan=55; // Rotation angle for scanning the candy
int Ext=120; // Rotation angle for removing the candy
void setup()
{
Drehrad.attach(4); // Connect the servo to pin 4
}
void loop()
{
Drehrad.write(Load); // Set the loading position.
delay(1000); // Wait until the position is reached.
Drehrad.write(Scan); // Set the position for scanning the candy.
delay(2000); // Wait until the position is reached.
Drehrad.write(Ext); // Set the position for ejecting the candy.
delay(3000); // Wait until the position is reached.
}
Adjust these three rotation angles
int Load=0; // Rotation angle for loading the candy
int Scan=55; // Rotation angle for scanning the candy
int Ext=120; // Rotation angle for removing the candy

Position the slide

First, disconnect the chute from the servo motor and turn the motor to a central starting position of 90 degrees. Then fit the chute centrally over the storage compartments - it’s worth securing it with a small screw.

Use the next sketch to check which angles are needed to align the chute with the six compartments. At all six positions, the chute must be turned so that the candy can fall exactly into the respective compartment.

Turn the slide to the middle position
#include <Servo.h>
Servo servoSchwenker; // The servo is called servoSchwenker.
void setup()
{
servoSchwenker.attach(3); // Connect the servo to pin 3
}
void loop()
{
servoSchwenker.write(90); // Set the center position
delay(1000); // Wait until the position is reached.
}
Move to the six defined positions
#include <Servo.h>
Servo servoSchwenker; // The servo is called servoSchwenker.
// Servo positions
int Fach1=0;
int Fach2=45;
int Fach3=75;
int Fach4=105;
int Fach5=135;
int Fach6=180;
void setup()
{
servoSchwenker.attach(3); // Connect the servo to pin 3
}
void loop()
{
servoSchwenker.write(Fach1); // The servo turns to position 1 (Fach1)
delay(1000); // Wait until the position is reached.
servoSchwenker.write(Fach2); // The servo turns to position 2 (Fach2)
delay(1000); // Wait until the position is reached.
servoSchwenker.write(Fach3); // The servo turns to position 3 (Fach3)
delay(1000); // Wait until the position is reached.
servoSchwenker.write(Fach4); // The servo turns to position 4 (Fach4)
delay(1000); // Wait until the position is reached.
servoSchwenker.write(Fach5); // The servo turns to position 5 (Fach5)
delay(1000); // Wait until the position is reached.
servoSchwenker.write(Fach6); // The servo turns to position 6 (Fach6)
delay(1000); // Wait until the position is reached.
}
Adjust these six angles
int Compartment1=0;
int Compartment2=45;
int Compartment3=75;
int Compartment4=105;
int Compartment5=135;
int Compartment6=180;

The scanning process: measuring color values

Use the following sketch to determine the color of a single candy. The wheel rotates from the “Load” position to the sensor area, the measurement is taken, and the readings for all three colors appear in the Serial Monitor.

To prevent individual outliers from distorting the measurement, each color is measured 200 times and an average is calculated.

Color readings for blue, green, and red in the Serial Monitor
The readings in the Serial Monitor
Scan process: Measure and output color values
// Add servo
#include <Servo.h>
Servo servoDrehrad;
// Servo positions
int Load=0;
int Scan=55;
int m=1;
// RGB LED
int LEDrot = 5; // Red color on pin 5
int LEDblau=6; // Blue color on pin 6
int LEDgruen = 9; // Green color on pin 9
int p=300; // p is the variable for a pause
int LeuchtkraftBlau = 200; // Brightness of the individual LED colors. These values are chosen so that all colors have the same measured brightness against a black background.
int LeuchtkraftRot = 200; // Brightness of the individual LED colors. These values are chosen so that all colors have the same measured brightness against a black background.
int LeuchtkraftGruen = 200; // Brightness of the individual LED colors. These values are chosen so that all colors have the same measured brightness against a black background.
int dunkel = 0; // A value of 0 means a voltage of 0 V - i.e. LED off.
long FarbwertRot=0; // Variable for measured values with an initial value of 0
long FarbwertGruen=0; // Variable for measured values with an initial value of 0
long FarbwertBlau=0; // Variable for measured values with an initial value of 0
int DurchschnittFarbwertRot=0; // Variable for measured values with an initial value of 0
int DurchschnittFarbwertGruen=0; // Variable for measured values with an initial value of 0
int DurchschnittFarbwertBlau=0; // Variable for measured values with an initial value of 0
// Photoresistor
int eingang= A0; // The word "eingang" now stands for the value "A0" (designation of analog port 0)
int sensorWert = 0; // Variable for the sensor value with 0 as the initial value
void setup()
{
// Add servo and center it
servoDrehrad.attach(4); // The setup contains the information that the servo is connected to pin 4 via the control wire (yellow). Another pin can of course also be used here.
Serial.begin(9600); // Communication with the serial port is started. This is needed to display the actual reading later in the serial monitor.
pinMode(LEDblau, OUTPUT); // Define LED color as output
pinMode(LEDgruen, OUTPUT); // Define LED color as output
pinMode(LEDrot, OUTPUT); // Define LED color as output
}
void loop()
{
// Clear all variables from previous measurements
FarbwertRot=0;
FarbwertGruen=0;
FarbwertBlau=0;
// Now load the candies to be measured. Be sure to watch the data in the Serial Monitor.
servoDrehrad.write (Load); // Load a new candy. If the candies don't load properly, you can program a brief back-and-forth movement of the servo here.
delay(1000);
servoDrehrad.write (Scan); // Turn the candy tray toward the sensor for measurement
delay(1000);
// Blue scanning process begins.
analogWrite(LEDblau, LeuchtkraftBlau); // Turn on blue
delay(p); // pause
// Repeat 200 times to calculate a good average for the measurement and minimize incorrect readings.
for (int i=1; i <= 200; i++)
{
sensorWert =analogRead(eingang);
FarbwertBlau=FarbwertBlau+sensorWert;
delay(m); // Wait one millisecond
}
DurchschnittFarbwertBlau=FarbwertBlau/200; // Divide the 200 added readings by 200 to obtain an average.
// Blue color output
Serial.print("Durchschnitt FarbwertBlau= " ); // Output in the Serial Monitor: the text "Durchschnitt FarbwertBlau= "
Serial.print(DurchschnittFarbwertBlau); // Output in the Serial Monitor. The Serial.print command sends the measured color value to the serial monitor.
analogWrite(LEDblau, dunkel); // Turn off the blue LED
// Green scanning process begins.
analogWrite(LEDgruen, LeuchtkraftGruen); // Turn on green
delay(p); // pause
// Repeat 200 times to calculate a good average for the measurement and minimize incorrect readings.
for (int i=1; i <= 200; i++)
{
sensorWert =analogRead(eingang);
FarbwertGruen=FarbwertGruen+sensorWert;
delay(m); // Wait one millisecond
}
DurchschnittFarbwertGruen=FarbwertGruen/200; // Divide the 200 added readings by 100 to obtain an average.
// Green color output
Serial.print("Durchschnitt FarbwertGruen= " ); // Output in the Serial Monitor: the text "Durchschnitt FarbwertGruen= "
Serial.print(DurchschnittFarbwertGruen); // Output in the Serial Monitor. The Serial.print command sends the measured color value to the serial monitor.
analogWrite(LEDgruen, dunkel); // Turn off the green LED
// Red scanning process begins.
analogWrite(LEDrot, LeuchtkraftRot); // Turn on red
delay(p); // pause
// Repeat 200 times to calculate a good average for the measurement and minimize incorrect readings.
for (int i=1; i <= 200; i++)
{
sensorWert =analogRead(eingang);
FarbwertRot=FarbwertRot+sensorWert;
delay(m); // Wait one millisecond
}
DurchschnittFarbwertRot=FarbwertRot/200; // Divide the 200 added readings by 100 to obtain an average.
// Red color output
Serial.print("Durchschnitt FarbwertRot= " ); // Output in the Serial Monitor: the text "Durchschnitt FarbwertRot= "
Serial.println(DurchschnittFarbwertRot); // Output in the Serial Monitor. The Serial.print command sends the measured color value to the serial monitor.
analogWrite(LEDrot, dunkel); // Turn off the red LED
// The measured values for the individual colors have been displayed in the Serial Monitor. Now comes the important part of the evaluation. Here, you need to determine which measured values correspond to each candy color.
}

Evaluate the measured values

In the IF condition, the measured values for all three colors are evaluated. To prevent slight color variations in the candies from causing the color to go unrecognized, specify a range for each color.

For example: A red candy had measured values of 200 for blue, 250 for green, and 750 for red. You would then choose recognition ranges of 180–220 for blue, 230–270 for green, and 730–770 for red. This makes ONE IF condition that includes all three ranges.

Then extend the code to eject the candy into the appropriate compartment.

Schematic: an IF condition for the red candy
// Für den Fall, dass ein Rotes Bonbon vorhanden ist.
if (DurchschnittFarbwertBlau > 180 && DurchschnittFarbwertBlau < 220 && DurchschnittFarbwertGruen > 230 && DurchschnittFarbwertGruen < 270 && DurchschnittFarbwertRot > 730 && DurchschnittFarbwertRot < 770)
{
//In dieser geschweiften Klammer wird später ergänzt, was passiert, wenn die oben genannte Bedingung erfüllt ist.
}
Extension: Eject the candy into the appropriate compartment
// Für den Fall, dass ein rotes Bonbon vorhanden ist.
if (DurchschnittFarbwertBlau > 180 && DurchschnittFarbwertBlau < 220 && DurchschnittFarbwertGruen > 230 && DurchschnittFarbwertGruen < 270 && DurchschnittFarbwertRot > 730 && DurchschnittFarbwertRot < 770)
{
servoSchwenker.write (0); // Position des Schwenkers geht zu Fach1
delay(1000); // Pause, bis die Drehung der Rutsche zum richtigen Fach abgeschlossen ist.
servoDrehrad.write (ext); //Das Drehrad dreht sich und das Bonbon fällt unten in den Trichter der Rutsche.
delay(1000); // Pause, bis die Drehung erfolgt ist und das Bonbon in den Trichter gefallen ist.
}

The complete example program

The following sketch sorts three different-colored candies and has been tested for functionality. Any unrecognized color ends up in its own compartment via the else branch.

Complete example program for three candy colors
// Add servos
 #include <Servo.h>
 Servo servoSchwenker;
 Servo servoDrehrad;
// Servo positions
int Load=0;
int Scan=55;
int ext=120;
int m=1;
// RGB LED
int LEDrot = 5; // Red color on pin 5
int LEDblau=6; // Blue color on pin 6
int LEDgruen = 9; // Green color on pin 9
int p=50; // p is the variable for a pause
int LeuchtkraftBlau = 200; // Brightness of the individual LED colors. The values are chosen so that the brightness measured for all colors is the same against a black background.
int LeuchtkraftRot = 200; // Brightness of the individual LED colors.
int LeuchtkraftGruen = 200; // Brightness of the individual LED colors.
int dunkel = 0; // A value of 0 means 0 V - so the LED is off.
long FarbwertRot=0; // Variable for measurement values with an initial value of 0
long FarbwertGruen=0;
long FarbwertBlau=0;
int DurchschnittFarbwertRot=0;
int DurchschnittFarbwertGruen=0;
int DurchschnittFarbwertBlau=0;
int messungen =200;
// Photoresistor
int eingang= A0; //The word "eingang" now stands for the value "A0"
int sensorWert = 0; //Variable for the sensor value with 0 as the initial value
void setup()
{
//Add servos and center them
servoSchwenker.attach(3); //Servo connected to the control wire (yellow) at pin 3.
servoDrehrad.attach(4); //Servo connected to the control wire (yellow) at pin 4.
servoSchwenker.write (90); // The swing arm moves to a central position.
delay(2000); //Wait until the servos stop. Otherwise, fluctuating voltage could affect the measured values.
Serial.begin(9600);
pinMode(LEDblau, OUTPUT); // Define LED color as output
pinMode(LEDgruen, OUTPUT); // Define LED color as output
pinMode(LEDrot, OUTPUT); // Define LED color as output
}
void loop()
{
// Clear all variables from previous measurements
FarbwertRot=0;
FarbwertGruen=0;
FarbwertBlau=0;
servoDrehrad.write (Load); // Load a new candy.
delay(1000);
servoDrehrad.write (Scan); // Turn the candy holder toward the sensor for measurement
delay(1000);
// Blue scanning process begins.
analogWrite(LEDblau, LeuchtkraftBlau); // turn on blue
delay(p); // pause
  for (int i=1; i <= messungen; i++)
  {
    sensorWert =analogRead(eingang);
    FarbwertBlau=FarbwertBlau+sensorWert;
    delay(m); //Wait one millisecond
  }
DurchschnittFarbwertBlau=FarbwertBlau/messungen;
//Blue color output
Serial.print("Average blue color value= " );
Serial.print(DurchschnittFarbwertBlau);
analogWrite(LEDblau, dunkel); // Turn off the blue LED
// Green scanning process begins.
analogWrite(LEDgruen, LeuchtkraftGruen); // turn on green
delay(p); // pause
  for (int i=1; i <= messungen; i++)
  {
    sensorWert =analogRead(eingang);
    FarbwertGruen=FarbwertGruen+sensorWert;
    delay(m); //Wait one millisecond
  }
DurchschnittFarbwertGruen=FarbwertGruen/messungen;
//Green color output
Serial.print("Average green color value= " );
Serial.print(DurchschnittFarbwertGruen);
analogWrite(LEDgruen, dunkel); // Turn off the green LED
// Red scanning process begins.
analogWrite(LEDrot, LeuchtkraftRot); // turn on red
delay(p); // pause
  for (int i=1; i <= messungen; i++)
  {
    sensorWert =analogRead(eingang);
    FarbwertRot=FarbwertRot+sensorWert;
    delay(m); //Wait one millisecond
  }
DurchschnittFarbwertRot=FarbwertRot/messungen;
//Red color output
Serial.print("Average red color value= " );
Serial.println(DurchschnittFarbwertRot);
analogWrite(LEDrot, dunkel); // Turn off the red LED
// Evaluation and processing
// If red:
if (DurchschnittFarbwertBlau > 140 && DurchschnittFarbwertBlau < 230 && DurchschnittFarbwertGruen > 140 && DurchschnittFarbwertGruen < 220 && DurchschnittFarbwertRot > 550 && DurchschnittFarbwertRot < 750) //Case 1: Red candy
{
servoSchwenker.write (0); // The swing arm moves to position 1
delay(500);
servoDrehrad.write (ext); //The turntable rotates and the candy falls into the funnel below
delay(700);
}
// If green:
else if (DurchschnittFarbwertBlau > 260 && DurchschnittFarbwertBlau < 340 && DurchschnittFarbwertGruen > 290 && DurchschnittFarbwertGruen < 430 && DurchschnittFarbwertRot > 260 && DurchschnittFarbwertRot < 320) //Case 2: Green candy
{
servoSchwenker.write (30); // The swing arm moves to position 1
delay(500);
servoDrehrad.write (ext);
delay(700);
}
// If blue:
else if (DurchschnittFarbwertBlau > 450 && DurchschnittFarbwertBlau < 490 && DurchschnittFarbwertGruen > 300 && DurchschnittFarbwertGruen < 360 && DurchschnittFarbwertRot > 140 && DurchschnittFarbwertRot < 245) //Case 3: Blue candy
{
servoSchwenker.write (60); // The swing arm moves to position 1
delay(500);
servoDrehrad.write (ext);
delay(700);
}
else
{
servoSchwenker.write (180); // The swing arm moves to position 1
delay(500);
servoDrehrad.write (ext);
delay(700);
}
}
Rate the instructions

Comments

Ask a question or share an experience

Contact us if something doesn’t work. Please include your board and the error message.

We review every comment before publication. Your email address remains with us.