DesignSpark Electrical Logolinkedin
Menu Search
Ask a Question

3 Jul 2018, 12:51

Arduino-Powered Mechatronic Colour Sorter

If you are like me and you only really enjoy the red Skittles or the yellow ones, this fun Arduino powered project could be just the thing for you. Along the way, you will learn how to overcome the challenges of controlling both Stepper Motors and Servo Motors and how to classify objects, in this case by sensing their colour using an Arduino. These basic principles will form the basis of your next mechatronic or robotic creation.   

Full code samples and documentation for the project is on Github

Breaking it down

Colour Sorter is able to sort coloured counters (in this case Skittles) into 5 different colours. It works by loading counters from a hopper into a rotating guide arm, powered by a Servo Motor. The arm has a circular retention hole in it which moves the counter to a position beneath an RGB colour sensor. Once the colour has been determined by the sensor, a rotating platter, powered by a Stepper Motor is moved to the correct position for that colour, beneath the collection point. The arm then moves the counter over the ejection hole where it drops into a  container, at which point the arm returns to its starting position and the cycle repeats itself. An Arduino Uno (715-4081) microcontroller board coordinates the functions of the various components.

There are a number of design challenges to overcome building a machine like this so I tackled the problem by breaking the project down into three separate components, that could be built and tested individually. This allowed me to focus on one smaller design area at a time, before combining each solution into the finished machine.

Stepping Up

To start I designed, built and coded the rotating platter. An RSPro Stepper Motor (535-0489) was used, which is a general purpose, bipolar, 4 wire motor, capable of 200 steps per revolution. It has more than enough torque for this project and has many possible applications for future machines.

Bipolar Stepper Motors have two coils which when energised, rotate the motor in either direction, very accurately, one step at a time. The motor specification determines the number of possible steps for each revolution. To control the motor using Arduino, an H-bridge circuit is required but to simplify matters I used an EasyDriver board which is an Open Source design with quality documentation.

EasyDriver needs only 3 of the Arduino digital I/O pins to control it. One pin sends a 1-millisecond timed pulse which steps the motor. One pin determines the direction of rotation. A third pin enables the motor, which allows current to be reduced when the motor is stationary. The default setting for EasyDriver enables micro-stepping by ⅛ step to give a smoother rotation.

StepTest.ino contains an array. Each array index represents one of 5 colours to be sorted. The array values are the zero-based position need to return to the origin or collection point for the platter.

int indexArray[] = {0, 4, 3, 2, 1};

Array values are updated each time the platter moves one position. So to move a particular index to the origin, the number of positions can be calculated along with the optimum direction. setPosition() function accomplishes this.

move() function takes the number of positions to move the platter and the direction. So to move by 1 position, the motor steps 200 per revolution, at 8 microsteps per step, for 5 positions so 320  (200 * 8 / 5) steps are needed. Here is the important code segment that does this.

for (int i = 0; i < steps; i++)   // Move the number of micro-steps
 {
   digitalWrite(stp, HIGH); //Trigger one step forward
   delay(1);
   digitalWrite(stp, LOW); // Pull step pin low so it can be triggered again
   delay(1);
 }

This simple technique can be used in all sorts of Stepper Motor projects.

Moving On

Next up is the construction and coding of the Colour Sorter platform and the arm to move the coloured counters. This was constructed from 3mm hardboard which is easy to work with.  A Parallax Standard Servo Motor (781-3058) was used to control the arm, mounted beneath the platform. Servo motors automatically return to their start position so are ideal for controlling the arm. The Arduino Servo library uses a single control pin to set the number of degrees of rotation of the servo from the origin.

ServoTest.ino contains separate functions, moveToOrigin(), moveToSensor() and moveToEjector() that encapsulate the 3 positions needed for the servo.

Here is the code to move a given number of degrees. The loop increment can be increased to speed up movement at the expense of smoothness. A small delay is needed to allow each position to be reached.

for (pos = 0; pos <= 30; pos += 1) { // goes from 0 degrees to 30 degrees in steps of 1 degree
   myservo.write(pos);              // tell servo to go to position in variable 'pos'
   delay(15);                       // waits 15ms for the servo to reach the position
 }

Again, this technique can be used in most servo driven projects.

Sorting It

The final component in the jigsaw was the Colour Sensing for which a TCS34725 based board by Adafruit was used. Colour sensing is a complex subject and this board comes with sophisticated libraries that can calculate Colour Temperature which was used to determine the individual colour of each counter.

SensorTest.ino obtains calibration values for each colour and tests the sensor. Make sure you do this in consistent background lighting conditions as the sensor is very sensitive to changes in lighting. Achieving a reliable mapping of colour temperature to specific colour was the most difficult part of the project and gives you an understanding of why some people suffer from colour blindness.

The test code outputs the Min and Max colour temperatures for each colour to the Arduino serial monitor. This mapping is used in the final code to index the correct position of the platter.

Getting Together

Now that each part of the project has been developed and tested everything comes together in ColourSorter.ino

I used my bench power supply set at 7V which is slightly over spec for the servo but gave no issues. This meant no extra circuitry was required.

getIndex() function is the important addition here and contains a series of if statements that set bounds for each colour temperature obtained from the sensor calibration exercise. The return value of the function is the index position of the platter for that colour, used to drive the Stepper Motor.

if (colour > 3878 && colour <= 4183) {
   Serial.println("Yellow");
   return index = 1;
 }

Back To the Future

Building this project has been a great fun way to learn about controlling both Stepper Motors and Servo Motors and also about classifying coloured objects. All really good building blocks for other Mechatronic and Robotic projects.

The hardest challenge for this build was obtaining accurate and consistent colour readings and if I were to do further work, I might add an additional platter position to collect “rejects”. That way it may be possible to create gaps in the colour temperature mapping to get a tighter tolerance on the colour. Then any counters that are not classified could be rejected and then re-sorted.

Having said that, Arduino Colour Sorter achieves between 98%-99% accuracy. If you have any suggestions as to alternative ways of classifying colour then please comment below.

Oh and don’t eat too many of the Skittles while you are building this!

I'm a big Open Source fan and Linux aficionado with a penchant for Italian motorbikes

3 Jul 2018, 12:51

Comments

July 20, 2018 07:51

Thanks for the informative article. I soon realized that sampling colour is a complicated business but in fact the libraries supplied with the TCS34725 board were very effective. I used the "experimental" library as it did seem to give better results. It measures raw RGB values and use these to calculate a colour temperature.

0 Votes

July 17, 2018 15:33

Nice article and great job on the setup. Rather than sorting by color temperature, you'd get better results by computing the LCH color space value of each sample. You can then compare each one to a known target using CMC tolerancing. This is the method used to sort & QC samples in the plastics and textiles industries.

Here's a link to a document that details this approach: https://www.xrite.com/-/media/xrite/files/whitepaper_pdfs/l10-001_a_guide_to_understanding_color_communication/l10-001_understand_color_en.pdf

0 Votes