Do you save your favourite coloured M&Ms to eat last, or are you constantly fighting over different colours with family and friends? Let us introduce you to the DIYODE Lolly Sorter, designed to stop the battle of the colours.
BUILD TIME: 10 Hours
DIFFICULTY RATING: Intermediate
It seems someone at the office has a sweet tooth, as for this month I was tasked with creating a machine that can sort M&M’s by their colour. It turns out it wasn’t as complex as I originally thought, and it was quite inexpensive. There is quite a bit of 3D printing required, but the electronics is quite straightforward. By using the Arduino platform we have made it user friendly to even the novice electronics enthusiast.
THE BROAD OVERVIEW
The heart of the device is the Arduino Nano development board in conjunction with the DFRobot I/O shield. This shield allows us to easily connect the servos and colour sensor without needing to solder any components.
We chose the Arduino compatible colour sensor sold by Jaycar to differentiate between the 6 different M&M colours, along with 2 micro servos; one for the receiving action and the other for depositing.
HOW IT WORKS
THE SENSOR
The sensor uses the TCS230 programmable colour light to frequency converter IC.
This IC uses an 8x8 array of photodiodes covered with red, green, blue and clear filters. These filters allow it to detect the intensity of each wavelength by converting the current flowing through the photodiode into a readable frequency.
Photodiodes work similarly to the photovoltaic solar panels on your roof albeit not very effectively/efficiently. In this situation when light enters the diode a small amount of current is transferred, this small amount of current is then converted into a pulsating DC signal with a frequency that varies in relation to the level of current flowing.
This frequency can then be read from the out pin using the Pulsein() function in the Arduino sketch. This function simply samples the input and calculates the frequency at which the signal is changing.
By manipulating the potential on the 2 input pins, S2 and S3 we control which photodiodes have current flowing through them at a particular time.
S2 | S3 | Photodiode |
---|---|---|
Low | Low | Red |
Low | High | Blue |
High | Low | Clear |
High | High | Green |
Therefore, if we want to sample the intensity of the red wavelengths, we would need to have 0V on pin S2 and S3. Likewise, to sample the green wavelengths S2 and S3 would both need to be pulled high.
With all of that understood, let’s consider our M&M’s. It is just a matter of bringing an M&M it into the field of view of the sensor and checking for the amount of red, green, and blue hues by first manipulating the S2 and S3 pins and then reading the frequency using the pulsein() function. By repeating this for red, green and blue we can differentiate between the 6 different M&M colours, then deposit the M&M into a particular container depending on the results.
Note: For the collection and depositing mechanism, I took inspiration from an existing design by Dejan at How To Mechatronics who made a similar device which sorted Skittles by colour. Check it out at: https://howtomechatronics.com/projects/arduino-color-sorter-project/
The Fundamental Build:
Colour Detection Prototype
Parts Required: | Jaycar | ||
---|---|---|---|
1 × Arduino Compatible Nano | XC4414 | ||
1 × Nano I/O Shield | - | ||
1 × TCS230 Colour Sensor | XC3708 | ||
2 × GWS Mini Servos | YM2760 |
My first step for prototyping this design was to get comfortable using the TCS230 IC. Having never used this device before I opted to create a basic prototyping test rig which would allow me to create a basic program interfacing the sensor and the servos. I also used this prototype as a proof of concept before beginning to model an enclosure.
Whilst it isn’t necessary for you to replicate the prototype, I will describe it for you so you can see how I arrived at the final build. It was a great way for me to learn about the sensor, and an easy way for me to identify the range of RGB values for each colour of M&M.
If you’re keen to get started sorting your M&M’s, you can skip to the main build if you do not want to experience the same prototyping process I went through.
ELECTRONICS
The electronics used in the prototype only differ slightly from the final device. For the prototype, I used two mini servos which turned out to be much larger and more powerful than what was needed for the device. Using the smaller servos helped reduce costs in the final build.
The prototype used an I/O Shield I had in my workshop, but the main build I chose to use the DFROBOT I/O shield seeing that it is more commonly available.
I also recommend that you read the ‘What The Tech’ article in Issue 14, where we describe the “RGB colour Sensor”. The article describes adding a series of LEDs to aid in the colour detection. This extra light significantly increases the effectiveness of the sensor.
THE CODE
I started out by using the sensor’s sample code downloaded from the Jaycar website. From there, I modified the sketch to suit the application by adding servo control and a series of if statements to represent each colour.
To decide the parameters for each specific colour, I simply put the M&M in front of the sensor and set the program to loop 1000 times recording each result using the serial monitor. I then repeated this 5 times per M&M colour using 5 differently coloured M&M’s.
After this, I imported the 5000 points of data into Excel and created a histogram. This displayed the numerical data for each M&M based on the amount of red, green and blue variables in each colour. This allowed me to statistically create the average expected values of red, green, and blue for each of the 6 colours of M&M’s.
This, of course, showed there is the possibility that the device may not correctly identify an M&M’s colour, and scanned as an outlier. Therefore, I wrote the sketch in a way that if the detected colour was not within the statically expected range, the M&M would simply be rescanned until it is clearly identified. Whilst this can slow down the process, it significantly reduces the potential for an M&M to be incorrectly identified. I could have easily just used the min and max values from the data, but I was concerned that there would be overlaps with colours. Red and brown, for example, are very similar, and if I used the min and max values they could easily be incorrectly identified and placed in the incorrect container.
At this point, I tested the ability to add servo control to the sketch, which presented no issues. I did, however, learn that the mini servos I chose were overkill for the project. I decided to switch them for the tower pro micro servos, which are half the price, substantially smaller and were more comfortable running from the USB input.
SETUP
#define COLOUROUT A5
#define COLOURS2 A4
#define COLOURS3 A3
#include <Servo.h>
Servo retrieve;
Servo deposit;
void setup() {
retrieve.attach(9);
deposit.attach(10);
Serial.begin(115200);
pinMode(COLOUROUT,INPUT);
pinMode(COLOURS2,OUTPUT);
pinMode(COLOURS3,OUTPUT);
}
LOOP
void loop() {
retrieve.write(145);
deposit.write(115);
colourred();
Serial.print("RED:");
Serial.print(getintensity());
colourgreen();
Serial.print("GREEN:");
Serial.print(getintensity());
colourblue();
Serial.print("BLUE:");
Serial.println(getintensity());
delay(200);
}
void colourred(){ //select red
digitalWrite(COLOURS2,LOW);
digitalWrite(COLOURS3,LOW);
}
void colourblue(){ //select blue
digitalWrite(COLOURS2,LOW);
digitalWrite(COLOURS3,HIGH);
}
void colourwhite(){ //select white
digitalWrite(COLOURS2,HIGH);
digitalWrite(COLOURS3,LOW);
}
void colourgreen(){ //select green
digitalWrite(COLOURS2,HIGH);
digitalWrite(COLOURS3,HIGH);
}
int getintensity(){ //measure intensity with oversampling
int a=0;
int b=255;
for(int i=0;i<10;i++)
{a=a+pulseIn(COLOUROUT,LOW);}
if(a>9){b=2550/a;}
return b;
}
The Main Build
Lolly Sorter
ADDITIONAL PARTS REQUIRED | Jaycar | ||
---|---|---|---|
1 × Arduino Compatible Nano | XC4414 | Z6372 | CE05101 |
1 × Nano I/O Shield | - | - | DFR0012 |
1 × TCS230 Colour Sensor | XC3708 | - | - |
2 × Micro Servos | YM2758 | MB95026 | SER0006 |
7 × 5mm White LED | ZD0190 | Z0876D | ADA754 |
7 × 100Ω 1/4W Resistors* | RR0548 | R0034 | CE05092 |
*Quantity shown, may be sold in packs. Hot glue, super glue, hookup wire and prototyping accessories may also be required.
ELECTRONICS
The project has been designed to be powered by a DC power supply or via USB. If you choose to power the servos by the USB you must remove the jumper on the I/O Shield from the GND selector and place it across the servo power selection pin. This allows the servos to be powered via the USB port, however, if you wish to power the servos externally you must return this jumper to the GND position.
I have made every effort to make the project solderless to make it easy to construct. The only part which requires soldering is the optional LED attachment, which is not difficult to build. It consists of 7 clear LEDs soldered in parallel, each one having a 100 Ohm current limiting resistor soldered to one of its legs. This arrangement is then connected to the 5V and GND pins of the I/O shield.
SENSOR WIRING
Sensor | I/O Shield |
---|---|
VCC | 5V |
GND | GND |
OUT | A5 |
S2 | A4 |
S3 | A3 |
SERVO WIRING
The two servos are connected to digital pins 9 and 10. The yellow signal wire goes to the blue side and the black gnd wire going to the black side. Wire the retrieve servo to pin 9 and deposit servo to pin 10.
3D PRINTED ENCLOSURE
The enclosure consists of a number of separate components. Whilst not overly complex, each piece needs to be assembled in a specific order.
Majority of the components were designed to print without the need for support material. This will help to reduce the cleanup time, as well as reducing the amount of plastic required.
I printed all of the parts on 0.3mm layer height using my FlashForge Creator Pro. I settled on black PLA because my test prints in transparent and red PLA produced inconsistent readings. I suspect the transparent case allows varying levels of light into the enclosure, which affects the readings.
To print and assemble the device I recommend you follow the following steps. All of the print files are available to download on our website.
BASE
Print the base first as the other components connect to it. This part takes about 11 hours to print and should be printed on its back for best results.
PLATFORM
The platform should be printed next as it is secured to the base using hot glue. It needs to be positioned so that the hole is centred over the cutout in the base to allow the lollies to fall into the delivery tube. It should be printed top side down for best results.
SWIPER
The swiper is attached to the first servo horn using small screws. I suspect it is possible to glue it to the horn however I strongly suggest not using superglue for the task as it can easily wick along the surface and into the gears of the servo ruining it. Once it is attached to the servo you can then attach the servo to the Base using the provided screws. For best results print this part on one of the flat sides.
THE COLOUR SENSOR
After assembling the collection components the next step would be to attach the sensor. It is designed to fit into the small cutout in the base, above the swiper and platform. Given the limited space, I recommend connecting the wires before installing this part and securing them with a dab of hot glue. Once the sensor is in place you need to bend the LEDs in a way that they are clear of the swiper but still aimed at the centre of the lolly when the swiper is directly under it. This can be a little tricky as there is a very small amount of space. Keep in mind the sensor has a working distance of only 10mm, so it is very important to keep this distance. After getting the LEDs bent into a working position I recommend securing the sensor to the base using a bead of hot glue. This will ensure the device does not come loose and alter the readings.
OPTIONAL SLIM LED BRACKET
This part holds the LEDs used to increase the reliability of the sensor readings. Without it, I found that the readings could be vastly different with only small changes in ambient lighting. I recommend printing this flat on the bed with the taper pointing up. This is angled in a way that it can print without support.
DELIVERY TUBE
The delivery tube is the only part where support material is mandatory. If possible, I recommend using a slicer that has the ability for custom support structures such as Simplify3D. This will mean that minimal support material is used inside the tube.
Any support material in the tub is very hard to remove and the rough surface left behind can cause enough resistance to slow down the M&M ejection speed enough to allow for the M&M to get stuck in the tube.
To reduce the support material, I recommend printing the tube vertically as seen in the screenshot above. After printing the part, you need to glue or screw the part to the servo horn.
Make sure the servo horn is already attached to the servo in this instance, as it is impossible to attach it after attaching the delivery tube to the horn. Also, when you attach the delivery tube, ensure it is central to the range of movement of the servo. To do this I simply attached the horn and gently actuated the servo to both extremes and made sure the delivery tube was attached in the centre of this movement.
HOPPER
I have made several iterations of the hopper in an attempt to rectify the feeding issue. I will add a couple of the most successful designs for you to decide which you prefer. All of them were printed with the funnel mouth side down, and require no support. After printing, it simply slots into the top of the base.
DFROBOT I/O SHIELD
I have added an .stl file called Arduino_uno_template. You can use this to drill holes in the bottom of the case to mount the I/O shield as you see fit. I have also made holes in the centre of the base, however, I expect others may like it mounted elsewhere. I recommend mounting it with 3 brass standoffs to ensure it is fixed securely to the enclosure.
COVER
The cover is a very simple print which can be printed by itself or in conjunction with the logo and a dual extrusion printer. For best results print it face down without support material.
Once printed, it simply fits over the front of the base. It is designed to have a friction fit, so it can require a little force to get it on the first time. If it seems too tight use a sharp craft knife to deburr the mating edges.
Once you have the device assembled you can simply run a cable through the side hole and program the Arduino Nano.
THE CODE
When first programming/powering the device, I recommend you have the front cover removed. The servos can go into a self-test mode where they move to three or four locations. These locations may be outside of the possible range if the front cover is connected.
You will also need to modify the program to suit your servo positions. You can do this by modifying the values in the “Servoname”.write(##); command.
In my case, to load an M&M into the swiper I needed to have the servo go to position 145, therefore, I used the command: retrieve.write(145);
Position 145 may not be identical for you depending on where/how you attached the horn to the servo. Likewise, all of the positions for the deposit servo will need to be modified to suit your setup. However, if you centred the servo and delivery tube it shouldn’t be too far off.
SETUP
#include <Servo.h>
Servo retrieve;
Servo deposit;
#define COLOUROUT A5
#define COLOURS2 A4
#define COLOURS3 A2
int red = 0;
int green = 0 ;
int blue = 0;
int colour = 0;
bool confirm = false;
int stall = 0;
void setup() {
retrieve.attach(9);
deposit.attach(10);
Serial.begin(9600);
pinMode(COLOUROUT,INPUT);
pinMode(COLOURS2,OUTPUT);
pinMode(COLOURS3,OUTPUT);
}
LOOP
This code is too long to list in our mag, but is available for download below.
TESTING
Hopefully, your lolly sorter will work properly when power is applied and M&M’s dropped into the hopper. I needed to do a little tinkering to get it to work smoothly. As with any DIY device with moving parts, there is the possibility of jamming.
In my build, the M&M’s occasionally jammed in the hopper and would not feed correctly when the hopper was filled high. Ideally, a mechanical device could be added, which would engage when there is a misfeed/stall, and agitate the hopper to get the lollies to flow again.
On the rare occasion, an M&M may not have sufficient velocity to leave the delivery tube, this normally results in the lolly being discharged from the delivery tube when the next lolly enters the tube.
It does not, however, appear to be a common issue. Although I believe it could be rectified by slightly increasing the angle of the delivery tube to increase the ejection velocity.
WHERE TO FROM HERE?
I built this device to work with M&M’s, but it is could very easily be modified to accept other lollies, such as skittles.
All you would need to do is import a few components into TinkerCad and adjust the size and or thickness of the following components.
- Swiper
- Platform
- Base
- Delivery tube
To help avoid jams, a third servo with a rotating auger could be added to the hopper.
This auger would constantly agitate the lollies at the intake to help prevent the hopper from jamming and allow the user to add significantly more lollies to the hopper.
Perhaps the project could be adapted to sort jelly beans, Lego pieces, seeds, buttons, beads, etc?