Arduino M&M Color Sorter

10,450

189

24

Introduction: Arduino M&M Color Sorter

About: I'm Jack and I like to make stuff :)

Are you tired of sorting your m&ms by hand? Looking for a silly way to spend your time and 3D printer filament? Then this is the Instructable for you.

This is a project that I built for one of my engineering courses, in which we were challenged to use an Arduino, some basic electrical components, and a 3D printer to build something creative that implemented sensing, actuation, and autonomous sequencing. I chose to tackle the important problem of unsorted m&m candies.

This project took several weeks of testing and iteration, so in this Instructable I will just be explaining my final design, with notes on the design process and tips for how to build one yourself.

Since I was building this project in my apartment with limited access to basic tools, I designed this project to maximize the use of my one tool, the 3D printer. Virtually no other tools are necessary, and the required electrical components are listed below.

Also if you dig this project be sure to vote for it in the Arduino Contest and the Colors of the Rainbow Contest!

Supplies

Arduino Uno

Stepper Motor (part# 28BYJ-48)

Stepper Motor Driver (part# ULN2003)

Servo Motor (part# SG90)

Limit Switch (part # DBWDKG-FT01)

Photo-resistor

RGB LED

White LED

220 Ohm Resistors (4)

10 Kilo Ohm Resistor

3" 6-32 bolts and nuts (3)

Jam Jar (Brand: Bonne Mamon)

Plastic Water Bottle (Brand: SmartWater)

0.8mm plexiglass

A BUNCH OF M&MS (for experimentation, of course)

Step 1: How to Sense Color

Part of what intrigued me about this concept was that the limited assortment of components I had to work with didn’t include a camera or color sensor, so I would have to devising my own system for sensing color.

On its most basic level, any type of sensor that can distinguish color is just measuring the relative amounts of different wavelengths of light. One of the components I did have at my disposal was a photo-resistor; a resistor that changes resistance based on the intensity of the light it is exposed to. I figured that if I could isolate an m&m in a dark, controlled environment and then use an RGB LED to shine multiple wavelengths of light on it, I could use the photo-resistor to measure the intensity of each reflection, and these values could then be used to distinguish colors.

This is the setup that I implemented, and it worked very effectively. The next step shows the mechanics and orientation of the sensing setup, and the code is detailed later, but I wanted to briefly comment on this system, as it is something that could be further developed in another project.

The code that I wrote for the sensing operation basically just shines the LED at full brightness displaying first red, then green, then blue light, and takes a voltage reading from the photo-resistor during each flash. These values are simply stored as references for each color and then subsequent m&ms are tested in the same way and their values are compared to the references. In other words, the machine just matches responses to references. It cannot, independent of the references, determine the color of a given m&m. In order to create a setup—using photo-resistors—that could detect the RGB composition of a sample, you would have to take into account the variable luminosity of the LED at different wavelengths, as well as the variable response of the photo-resistor to different wavelengths. You would also need to have references for a white and black sample to determine the bounds of the photo-resistor response. This is something I would love to dive into in a further project. @fjordcarver has made some great progress in his Instructable where he explains this process in more detail, and I also think there is potential for using multiple photo-resistors with colored films as an alternative to the LED method. Let me know if you do anything with this concept!

Step 2: Design: Primary Mechanism

Alright, I’m a mechanical engineering student so this is probably my favorite part of the design. I designed this machine to be compact and vertically oriented for the visual effect of the unsorted m&m’s gradually sifting down into the colored bins below. With this design, each m&m has to be singled out from the upper reservoir, placed into the dark sensing chamber, the chamber has to be shut, and then the m&m must be dispensed from the chamber and directed into one of the six color bins. The fun of it, in my opinion, is that nearly this entire process is accomplished with only one moving part.

I’ll attempt to explain, but it’s easier to just take a look at the pieces and visualize it. The upper disk, lower disk, and center pin connect to form a rotator that spins within the central sensing compartment piece. The central sensing compartment has a chamber that, when an m&m is dropped in the top, funnels it into a controlled position where it can be viewed from the side by the LED and the photo-resistor housed in the sensor housing. The central sensing compartment is stationary, and the rotator is driven from above by a stepper motor mounted in the piece labeled “top layer.” The top layer also serves as the base of the m&m reservoir and houses a limit switch that rides up against a cam on the upper disk.

The sequence starts with the upper disk oriented with the hole 90 degrees counterclockwise from the sensing compartment. At this loading position the upper disk selects an m&m from the reservoir and, rotating clockwise, drops it into the sensing chamber. By rotating 90 degrees more, the chamber is closed on top and bottom, and the sensing operation can be performed in a controlled, dark, environment. Then the rotator turns 180 degrees to return to the loading position and, depending on the direction in which it rotates to get there (clockwise or counterclockwise), the identified m&m will meet one of the two ramps on the lower disk, directing it in one of two directions out of the chamber. In this way the rotator selects the m&m, isolates it for sensing, and sorts it into one of two groups as it selects another. The only remaining action is to further sort from two groups into six groups, which is accomplished by the Secondary Mechanism.

The limit switch and the cam on the upper disk are used to zero the position of the rotator, since the small stepper motor has no feedback and is prone to skipping steps. Additionally, a white LED is mounted in the back of the sensing chamber to aid in detecting if an m&m has been successfully loaded.

Step 3: Design: Secondary Mechanism

With the identified m&ms sorted into two color groups, the Secondary Mechanism further sorts each group into three colors. The mechanism consists of two ramps that funnel the dispensed m&m into one of two locations on opposite sides of a downward facing servo motor. The servo motor rotates the final divider, which sits above a jar with dividers sectioning it into six 60 degree compartments. Depending on its position, the final divider will direct the m&m either straight down, 60 degrees clockwise, or 60 degrees counter-clockwise to land in the appropriate compartment.

While I was happy with the simplicity and effectiveness of this design, the final divider was modeled to be a bit too shallow. This meant that when rotated to direct an m&m clockwise or counter-clockwise, there was not a lot of clearance and the m&m would occasionally get stuck and not slide through the part. Then, when the divider returned to the middle position, the m&m would fall into the wrong bin. This was a relatively infrequent problem, but it ended up being the only reason for occasional mis-sorts. A redesign of the final divider could fix the issue pretty effectively.

Step 4: Design: Other Components

The m&m reservoir is made from the bottom of a SmartWater water bottle, and I modeled the top portion of the machine to fit it snugly. This includes a funnel piece that fits on top of the stepper motor and directs m&ms towards the loading site.

As I was developing the machine, I noticed that the assembly fit nicely on the top of the jam jar that I was using to store my test m&ms. I decided to lean into that idea, and I designed a lid that would allow the whole machine to screw onto the top of the jam jar (brand: Bonne Mamon). I made some clear dividers for the jar by scoring and snapping some 0.8mm plexiglass. These pieces were just held in place with two basic 3D printed parts, and the process of sizing and fitting the dividers was pretty trial-and-error.

Step 5: Assembly Part 1

I modeled all of these parts in SolidWorks and printed them in PLA at 0.28mm layer height on my Ender 3 Pro. I modeled nearly everything to be pressure fit with the components I had, so it is possible that some of the holes and openings on these models may have to be resized if you are working with slightly different components or print settings. In these sections I have included the STL models and technical drawings for all the parts.

Central Sensing Compartment – A white LED is glued into the back of the sensing chamber using hot glue, being careful to say clear of the bottom surface of the part. All the pieces in the Primary assembly are printed in black to minimize reflection, and I also glued in some matte-black construction paper as shown to further minimize reflection within the sensing area. I am not sure if this had a significant effect on sensor accuracy, but I assume it helped a tiny bit. This could also be accomplished by sanding the area to a matte finish or using a matte printer filament.

Sensor Housing – The RGB LED and the photo-resistor are hot glued into the sensor housing piece as shown, which then snaps into the side of the central sensing compartment.

Rotator – The rotator is printed in 3 parts (could be printed in two if you have good print settings). These parts fit together snugly but I also drove in a small screw from the bottom of the lower disk into the center pin for good measure. The disks of the rotator are assembled sandwiching the central sensing compartment as shown.

Top Layer and Funnel – The stepper motor snaps into the top layer facing downwards. The limit switch is fixed on the side of the top layer with two screws that thread through the slots, through holes in the limit switch, and into a small 3D printed part labeled “limit switch holder.” The slots enable the limit switch to be adjusted for optimal contact with the cam. The funnel piece also fits snugly on top of the stepper motor.

Step 6: Assembly Part 2

Secondary Mechanism – The servo motor pressure fits into the rectangular hole in the piece labeled “secondary funnel,” and the final divider is attached to the servo arm. If you have worked with these little servos before you’ll probably know that attaching things to them is not that easy. I modeled the final divider with a grove inside of it that fits one of the included servo arms snugly (you’ll have to cut off part of the arm though). Then, I drove in the included screw to secure that arm to the servo rotor. This worked quite well and minimized space.

Full Assembly with Spacers – With all these components printed and mechanisms assembled, everything can now be connected using three 3” 6-32 machine screws and the accompanying nuts. I printed spacers of various lengths to separate each layer of the machine, and while the full assembly drawing includes approximate spacing distances, you will probably have to determine optimal spacer lengths yourself. 3” bolts were longest I had access to, so the machine was designed accordingly. If you have longer bolts, that would give you the freedom to space out the Secondary mechanism a bit more and mount it higher above the jar.

Reservoir and Jar – As mentioned earlier, the reservoir is just a SmartWater water bottle that I cut in half and cut a hole in the bottom of. The top layer and funnel are designed to fit it snugly. The jar is outfitted with plexiglass dividers that fit into the divider holders (3D printed). The plexiglass is 0.8mm thick and you will have to cut it to size according to your jar and the height of the mechanism when screwed on. For permanence the divider assembly can be glued into the jar, but only after you have ensured that it is oriented to line up with the mechanism when screwed on.

Note: aside from the Primary Mechanism being black, you could print these parts in any color. I chose to print in black and white because I liked the aesthetic of the only color on the machine being the color of the m&ms. In my opinion, definitely worth the extra 20$ I spent on filament.

Step 7: Wiring

The machine is controlled with an Arduino Uno, and the wiring is pretty simple. I’ve drawn up a nice wiring diagram to specify everything but there are just a few things to note.

- Each LED, including the white LED in the back of the chamber, is wired with a 220 Ohm resistor.

- The limit switch wired to Pin 13 uses the Arduino’s internal pull-up resistor, which must be set in the Arduino Setup routine.

- The photo-resistor is wired in a voltage divider with a 10 Kilo Ohm resistor. Analog pin A0 reads the intermediate voltage from the voltage divider.

- Be sure to power the stepper and servo motors independently of the Arduino (ie. not downstream). Powering the Arduino via USB and powering the stepper from the Arduino’s 5V output will cause problems.

In the future I would love to get all these electronics condensed so they could be mounted on the machine and battery powered, but for this project I didn't have the time or tools to solder anything and most of the wiring was done on a breadboard.

Step 8: Code

I have linked the full code below. It utilizes the Stepper.h and Servo.h Arduino libraries. I've also attached a more complete break down of the code from my official project report, but here is a quick summary of the sequence of events.

The Machine starts by resetting to the zero position, and then begins loading m&ms to collect initial reference values. The first six m&ms that the machine tests should include one of each color (the order doesn't matter but I usually feed them in one at a time to control which colors are fed into which bins). After the first six have been tested and sorted, the rest of the m&ms can be loaded.

Each time the rotator drops an m&m into the chamber and the chamber has been closed, the white LED flashes and a photo-resistor value is collected to determine if an m&m is actually in the chamber. If not (ie. the upper disk failed to collect one) then the rotator will make another full rotation to try again. If there is an m&m there, then the sensing procedure with the RGB LED and photo-resistor will be performed. The photo-resistor values will then be compared to the references using a least-squares approach, and a closest match will be determined. Then, depending on the result, the final divider position will be changed and the rotator will turn in the appropriate direction to dispense the m&m into the corresponding compartment. The code also keeps track of how many m&ms of each color have been identified, and it updates the reference value so that each reference is the average for all identified m&ms of that color.

A final routine in the main loop keeps track of how many times in a row the machine has failed to load an m&m. If this number exceeds 10, then it assumes that all m&ms have been sorted, and the routine prints some statistics on the run and ends the program.

Again, the actual process is a lot more complicated than this, so see the attached code and breakdown for a more detailed description.

Step 9: Final Results

And with that, you are now free from the painstaking task of sorting your m&ms manually!

This project was a ton of fun and was a very effective proof of concept. A full sort can handle about 150 m&ms at about 10 m&ms/minute (not terribly efficient, but very fun to watch). For anyone interested, my experimentation suggests that blue and orange are the most common m&m colors, showing up with twice the frequency of any of the other colors. This turn out to be consistent with published data from the nearest MARS factory.

I am really happy with the reliability, space-efficiency, and aesthetic of this design. The color identification system now works flawlessly, thanks, in large part, to the refining of reference values as m&ms are collected. The only sorting errors that occur are those associated with the final divider, as discussed in step 4. In the future I would like to condense the electronics for this project so that the machine can be self-contained and possibly battery powered. It would also benefit from a redesign of the final divider and longer screws to enable the machine to sit a bit higher on the jar. Beyond that, I think there are lots of applications for this basic system of color sensing and I’m looking forward to seeing if anyone applies that idea to a future project. I also think that this design could be easily modified to handle the important task of sorting skittles.

Thank you so much for reading this Instructable! If you liked it, please vote for it in the Arduino Contest and the Colors of the Rainbow Contest! Happy making :)

Colors of the Rainbow Contest

Grand Prize in the
Colors of the Rainbow Contest

Be the First to Share

    Recommendations

    • Robots Contest

      Robots Contest
    • 3D Printed Student Design Challenge

      3D Printed Student Design Challenge
    • Made with Math Contest

      Made with Math Contest

    24 Comments

    0
    artist_pw
    artist_pw

    6 weeks ago on Introduction

    This is so great and you're funny! Now, on to easily solve one of the Millennium Problems, because I know you could after making this!

    0
    CraftythingsByHope
    CraftythingsByHope

    2 months ago

    Congratulations on winning the Grand Prize! You definitely deserved it for this clever clever project!

    0
    dcolemans
    dcolemans

    3 months ago

    What a great machine! I love it.

    0
    WUVIE
    WUVIE

    3 months ago

    Wow. Wow. Wow. This is way over my head, though I am consumed with amazement. This is utterly fabulous!

    0
    Doglover 8D
    Doglover 8D

    3 months ago

    So cool! Great idea!
    Where did you get the idea from?

    0
    n4mwd
    n4mwd

    3 months ago

    This is a very cool project for the whole family to enjoy. I'm wondering how many m&m's didn't survive long enough to make it to the video. Also, after looking at the design, there is more than one moving part. Your comment about "one moving part" threw me for a loop until I realized there were actually two motors and two moving parts. Still, very clever for sure.

    0
    jeff.verive
    jeff.verive

    4 months ago

    Very cool! As scifiguy451 suggested, you could use the RGB LED as a sensor, and the Arduino has enough A/D channels to make it possible.

    One question: how do you deal with the 3D printer fumes in your apartment?

    0
    n4mwd
    n4mwd

    Reply 3 months ago

    I use PetG here. No fumes.

    0
    Jackofalltrades_
    Jackofalltrades_

    Reply 4 months ago

    separate room with the window open... not a great solution I'll admit

    0
    jeff.verive
    jeff.verive

    Reply 4 months ago

    Whatever works! :)

    0
    surfer_max
    surfer_max

    4 months ago

    great engineering - very nice!

    0
    NikonUser
    NikonUser

    4 months ago on Step 9

    What a great idea - an absolute essential tool for every M&M (or Smarties for us Brits...) aficionado. I think they must have something similar on the LegoMasters show, to reset the brick room each night (that or a hundred minions on minimum wage working through the night to resort all the bricks).

    For the arduino code, I would have thought that with the RGB LED being a constant component being driven with a regulated voltage, and the M&Ms in a controlled and light sealed cell, you should be able to build a small script to measure the RGB reflectivity for each colour of M&M, and return calibration values to you, which could then be written in to the main code as constants, so that the calibration process only has to be done once, rather than each time you restart or reset the arduino? It would also allow for the red candies to always be delivered to silo 1 for example, rather than it simply replicating the order that you place them during the existing manual calibration routine.
    A further quality assurance type extension could be to spit out any individual M&M that doesn’t give a reading that matches the expected values (within acceptable tolerances), so that badly coloured, or broken candies that are exposing too much of the chocolate interior rather than the coloured shell, are put into a reject container for subsequent disposal (or more likely, consumption) as the operator sees fit. Yes - that’s going to require a rethink of the mechanism, with perhaps a stage 0 rotor passing each candy either to the existing two rotors if good readings are made, or to a reject port on the side of the mechanism?

    All in all, a great fun project, and while primarily a frivolous demonstration in this form, there are many real-world applications using such mechanisms and logic. Well done.
    0
    JohnW51
    JohnW51

    4 months ago

    This is a great project. Congratulations! If I'm your instructor, you get a solid A. Maybe even and A+. Simpler is always better. I love the mechanical simplicity of this device. The method of determining the color of the M&M piece is very clever, and the method of controlling where the piece ends up is just as clever.

    0
    The Real Phil Swift
    The Real Phil Swift

    Question 4 months ago

    I love this project and I would like to modify it so that I can enclose the electronics in the machine. Could you supply the 3d files so I could modify it? Thanks

    0
    m instructables
    m instructables

    4 months ago

    Great idea! Your presentation style is very enjoyable. A color sorting machine has been on my todo list for years.

    Hopefully, I can use yours to finally make one.

    I do agree that you might not want to limit to the six colors - after all you have mint m&ms, etc and other special holiday colors.

    Not to mention that, if I remember correctly, skittles has colors like purple.

    And let's not forget Resese pieces...

    Good luck in the contest! I hope you win!

    You have my vote!

    0
    RobertB765
    RobertB765

    4 months ago

    Hello, I love this project and I can't wait to get started on it. I've downloaded the stl. files and I have noticed parts 7, 8, 9, 11, and 12 are missing in your instructables for the project. Also, the spacer stl. file is missing but that piece is relatively simple to create. I have all of the other electronic components available and ready for the project. I was hoping to get started on it today. I'll drop a vote for you in the contest, this is an awesome project and it looks like it's going to be a lot of fun and I have already learned from it. I would have never thought of using a photo-resistor in this manor, Ingenious!

    thank you

    Okay, my bad, LOL,,,, I believe I just found the missing stl. files. It irritates me when I see people either not reading through all of the material or have neglected to pay attention to the contents of the material. Now I find myself guilty of doing that very thing that irritates me,,,, LOL!!! SORRY!






    0
    Jackofalltrades_
    Jackofalltrades_

    Reply 4 months ago

    no worries! haha. Good luck with the build! let me know if you have any more questions

    0
    juropeki
    juropeki

    4 months ago

    Chapeau ! A great idea and a very simple but effective way of distinguishing the colours. The "useless" things are most fun, aren't they. Well done.

    0
    MarkH342
    MarkH342

    4 months ago

    Wow. That is all I can say.