Hate Blue M&M’s? Sort Them Using The Power Of An IPhone!

M&M Sorting machine

Some people really like eating specific M&M colors… You could spend hours sorting your packs of M&M’s into color specific piles, or you could build a machine to do it for you.

That’s exactly what [ReviewMyLife] decided to do, and it’s quite impressive! He’s using a rotating hopper to release M&M’s into a chute one-by-one, and then an iPhone to perform color recognition as the M&M falls past it. That information is then communicated over Bluetooth to the Arduino which actuates a high-speed electromagnetic gate to force the M&M down the right chute for sorting.

The machine works surprisingly well for a prototype that was hot glued together out of foam board, but fear not, he plans to upgrade it now that the proof of concept has been confirmed. He’s hoping to get rid of the iPhone and replace it with a Raspberry Pi for starters, 3D print some of the parts, and consolidate its power supply. Currently he’s using three separate supplies to power the Arduino, electromagnets, and the hopper motor — not very efficient!

Also it’s not very good at sorting brown M&M’s. They look too much like a cross between orange and a shadow on the iPhone. He thinks a bit of work on the image recognition software and maybe some LED lights to help the camera might do the trick.

We’ve seen some systems done like this before, including one that can sort tiny little beads! OCD readers eat your hearts out!

31 thoughts on “Hate Blue M&M’s? Sort Them Using The Power Of An IPhone!

  1. I wish I had the time and skill to waste my life doing pointless but interesting little inventions like this. 2 kids kinda takes the wind/energy out of my sails …. On a daily basis. They’re worth it though and seeing these things o. hackaday gives you a little faith in the future of humanity.

  2. Cool project… but the upgrade will add pi because?
    It’s like if you have an Arduino or a Pi in it hackaday will probably cover it, but if you arbitrarily stuff both in there they have to cover it…

    Haven’t I seen an arduino use a camera before? I would swear I have even seen color detection done before. Why can’t it do the whole thing? Why do you need bluetooth? Why do you need a Pi?
    What’s next an electron probing microscope connected to a super computer by way of satellites?

    Don’t mean to be a party pooper, I just would find it much more interesting to revise by using less or incorporate more features.

    1. Best case scenario 20MHz of a “slow” Atmel chip that Arduino uses with 640*480=307200 pixels to clock out gives you one frame every 15ms on a very good day, add to that some processing (on the fly as the Arduino does not have enough memory to store this data) and you’re at about 1-5seconds/frame. I’m impressed by the speed of the processing here, unless the RPi does processing at a really low level it won’t reach the same speeds (source: I’ve worked on RPi and done image processing on it). An Arduino and RPi really complete each other, one does high level processing the other takes care on I/O and you can make anything happen on budget.

      1. What jaromirs said basically. I am aware this is fast, and that the arduino can’t handle high resolution images quickly(at all) but you need a small amount of pixels.

        My whole point really is that with this simple feature set, a Pi is a huge waste. Add features or use hardware within range of actual needs. I am not advocating using a 555 timer, but an Pi is beyond overkill.

        If you use that hardware why not analyze the shape, do quality control inspection, analyze if the logo is visible, or something else interesting. If you use a Pi why do you need an arduino still?

  3. My colleague (ReviewMyLife) made this out of a series of conversations we had at work. We’ve all seen M&M sorters before but the speed of existing designs have been somewhat lacklustre. There’s definitely room for improvement on this design but it’s probably the fastest project online. The gate array can be further compacted by a circular ‘flower’ arrangement with the gates pushing their respective chutes into the centre on close. That’ll also give each gate the same time to react to the color recognition instruction from the iPhone as the the fall distance is the same.

    Just to re-iterate, this prototype does indeed use an arduino, iPhone and bluetooth but that’s not the end design. ReviewMyLife wants to reduce this down to just a Pi and a camera but wanted a proof of concept for the hopper and gate array first… give him time to reduce it down whilst improving the real innovative parts of this design.

    Looking forward to the next prototype mate!

  4. My colleague (ReviewMyLife) made this out of a series of conversations we had at work. We’ve all seen M&M sorters before but the speed of existing designs have been somewhat lacklustre. There’s definitely room for improvement on this design but it’s probably the fastest project online. The gate array can be further compacted by a circular ‘flower’ arrangement with the gates pushing their respective chutes into the centre on close. That’ll also give each gate the same time to react to the color recognition instruction from the iPhone as the the fall distance is the same.

    Just to re-iterate, this prototype does indeed use an arduino, iPhone and bluetooth module but that’s not the end design. ReviewMyLife wants to reduce this down to just a Pi and a camera but wanted a proof of concept for the hopper and gate array first… give him time to reduce it down whilst improving the real innovative parts of this design. As discussed, it might not be possible to replace the iPhone with a Pi and camera due to color recognition speeds, but maybe an android alternative might be a welcome addition.

    Looking forward to the next prototype mate!

      1. Well, didn’t know that. I was born early ’70s and I can only remember M&M not having red until mid 80’s Still, give me tan color. It’s the only way to get “Cinamon” flavored candy ;)

    1. I think one’s choice of candy coated chocolate depends on what one had when they were a kid. A couple years ago, I tried the candy coated chocolate called “Smarties” in most countries, and didn’t like it. (The two, while sharing a common description, are totally different)

  5. They use a system for sifting cranberries where they fall past a color detector and air jets shoot the bad ones into the reject bucket.

    Eat way to do this without the fast reflex processing would be to embed color sensors into a cog that accepts one at a time and can turn to dump it left or right.

  6. Take a simple photodiode and an RGB LED. While the RGB is normally switched all colors on. you can detect when the M&M is in front of the photodiode. Now switch all three colors seperately fast. and you can detect the color. No need for such an overpowered iphone killer. And no need for a shitduino btw.

  7. Here’s a bit more explanation on some of the decisions:

    I did first try a basic colour sensor attached directly to the Arduino. But those simple colour sensors are really slow and need calibration (this at least applies to the ones that are cheap and are available for the Arduino – I’m sure a factory quality one may be better!).

    Single pixel analysis would only work if the M&M was static and held in front of the sensor (these sensors are too slow to detect an M&M in free fall – never mind spot its colour). These sensors spray out a continuous signal – so you can’t just read ‘the colour’. You have to sample the individual values you are getting back from it for long enough to get a stable reading. And that’s just for one colour sensor (the green for example). You then have to do the same for the red, blue and white sensors.

    I wanted to spot the M&M and detect its colour whilst it was falling so I needed something faster.

    From looking at a single pixel you can’t even be sure of the colour. M&Ms aren’t flat objects – they are curved and so have a range of colours around them. You can’t tell for certain if you are looking at a light part of the red, or a darker part of an orange M&M, or maybe a lighter part of the brown for example. The colours of each M&M aren’t unique – the colour ranges overlap. You need to be able to sample a larger part of the M&M to get a better chance of figuring out the colour.

    This is also why the camera is placed a bit back from the chute – to give it a wider field of view so that it can find the M&M which is falling somewhere in front of it. To guarantee that you could spot it from close up you’d need a faster camera.

    To improve the speed the software doesn’t analyse the whole camera image – for performance reasons it analyses a vertical strip about 20 pixels wide. I was using the high speed 120 FPS iPhone 5S camera to give me the best chance of spotting the M&M.

        1. Likely that color sensor connects to the Arduino via I2C. That protocol is convenient for attaching multiple peripherals using a single set of shared pins, which helps keep wiring simple; but for that convenience, speed is sacrificed. A sensor that produces native analog outputs will be a *lot* faster.

          An example of modifying your setup to use such a color sensor:

          1) Create a narrower section of the chute where the M&M will be roughly centered.
          2) At the measurement point, shine a IR LED through a hole in the left side of the chute. Mount a phototransistor in a hole in the right side. When the M&M breaks the beam, it’s time to read the color sensor. (Bonus points if you detect this using an interrupt, so you don’t have to continuously poll in a loop.)
          3) Mount the color sensor and a white LED on the front window of the measurement point. The white LED could be on all the time, or just when a measurement is taken.

          Also a tip for the software side. You’re probably trying to detect color by comparing the RGB values against minimum and maximum thresholds, like this:

          If R>192 and G<64 and B<64 Then MMColor=Red
          (and so on for other colors)

          And as you've found, it's hard to come up with a set of thresholds that accurately detect color when the M&M may be tilted. It can be very helpful to first convert RGB to another color system, like HSL (Hue, Saturation, Luminance). This is easier to compare because Hue represents the basic color all by itself. Saturation represents how vivid the color is – most will be high, but brown in particular will be low. And Luminance represents the brightness or amount of shadowing caused by M&M tilt, which can probably be ignored completely. Try taking a photo of some M&Ms, and in a photo editing program (Photoshop, GIMP, etc.) examine some pixel values in HSL. You'll quicky see how Hue is more predictable for a particular color of M&M, regardless of shadowing or where on the M&M you pull the pixel from.

Leave a Reply to RunnerPackCancel reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.