M&Ms and Skittles sorting machine

Overview of the M&Ms and Skittles sorting machine

External links: Reddit thread, Github project files, Imgur post.

This project, the M&Ms and Skittles sorting machine, was on my to-do list for a long time. I finally managed to complete the project after working on it for several months in my free time. The machine uses an Arduino microcontroller, stepper motors, an RGB colour sensor and 3D-printed parts to sort several types of candy by their respective colours. The video below shows the machine in action.

The machine sorts M&Ms and Skittles by colour by performing optical measurements using the colour sensor. It can be modified to sort any type of coloured object, as long as the individual pieces have a regular shape and (very) similar dimensions. It takes approximately 2-3 minutes to sort a 300 g bag of Skittles / M&Ms and the mechanism sorts about 2 pieces per second. The machine is 250 mm in diameter and approximately 300 mm in height.

Overview of the M&Ms and Skittles sorting machine
The finished M&Ms and Skittles sorting machine
Close-up view of M&Ms
M&Ms come in six different colours

I decided to base my machine on a similar mechanism that I found on YouTube (lots of credit to them!), which uses a rotating wheel to scan the pieces of candy. After deciding on the details, I started designing the machine from scratch using CAD software and I implemented my own improvements in the process.

Measuring the dimensions of M&Ms
Measuring a bunch of M&Ms to determine the average size
Detail view of the candy processing unit inside the finished machine
Detail view of the candy processing unit

I started working on this machine in May 2016 and only finished it in December of that same year. It took a lot of time to design and build the machine, and I kept optimizing the parts and software after the first prototype was done. Including all prototypes and spare parts, I spent nearly €500 on this machine. Well worth it, considering everything I learned.

The sorting process

While in the hopper, candy pieces are constantly mixed to prevent clogging at the inlet of the upper feed tube. The scanner consists of a small wheel with four slots which are spaced 90 degrees from each other. After a piece enters a slot, the wheel, which is powered by a stepper motor, rotates 90 degrees. The RGB sensor then takes three measurements which take 30 milliseconds each. The Arduino controller then determines the item colour (based on reference data) and positions the exit tube (also using a stepper motor) to guide the piece to the correct container. Just before the exit tube reaches its target position, the wheel turns another 90 degrees to drop the piece. The process then repeats. During the process, visual feedback is provided using the LED strip that encircles the machine.

Designing and building the machine

Exploded view of the sorting machine
Exploded view of the sorting machine

After the concepting phase, every part of the machine was modelled using CAD software: Siemens NX10 in my case. Over the course of a few months, I went through several revisions before even ordering anything. Sometimes I’d run into a component sourcing problem, sometimes I figured out that a particular part would not work for some reason. After finishing the models, they were sent to a 3D printing company for production.

A shapeways box containing 3D printed parts
It’s always nice to receive boxes like this one
3D-printed parts for the sorting machine, first version
First version of the machine’s 3D-printed parts

After receiving the printed parts, I assembled them and integrated them with some of the motors and electronics to perform tests. This resulted in a list of bugs and shortcomings which had to be corrected for a second iteration. Some parts did not fit together properly because of tolerance issues. Also, candy would get stuck at the bottom of the hopper and in the exit tube for various reasons. All of that was fixed in the second design iteration.

Various packaged 3D-printed parts
Second revision, white 3D printed parts
First and second revision of the candy processing unit body
The new vs. the old candy processing unit body
First and second revision of the exit funnel
The new vs. the old exit funnel

The problem with the exit tube was easily fixed by increasing the slope of the tube. The clogging issue was much harder. After considering some different solutions, I eventually came up with a de-clogging device which uses a rotating hopper with indents in the side. This mixes the pieces and prevents them from getting stuck.

This new mechanism resolves the clogging issue

After finishing the 3D-printed parts, I was ready to start designing an enclosure for the machine. I decided the machine should have a cylindrical shape with regular features all around the outer body. This allows a user or spectator to enjoy watching the machine at work from any angle.

Inspired by the Pibow case for the Raspberry Pi, I designed the enclosure to consist of a stack of seperate pieces with a fixed height of 9 mm. This applies to the whole body, except for the upper part, which is a large piece of PVC tubing. All layered pieces are made of MDF wood, which is lasercut according to my digital drawings.

The sorting machine's baseplate
The bottom two layers of MDF form the base of the machine

The two bottom layers serve as a base for the machine. The second layer has six slots for the bowls that’ll hold the sorted pieces. The small holes are for wiring and structural reinforcement purposes.

Lasercut MDF pieces that together form a pillar component
Fourteen individual MDF pieces will together form one of three pillars
Constructing the machine's pillars from MDF layers
Assembly of a pillar

The top part of the machine rests on three periodically spaced pillars. One pillar consists of fourteen wooden pieces which are glued together. The pillars are assembled using threaded rod as a guide. Glue is applied between each layer. After the stack is done, it is pressed together using threaded rod and bolts. After drying, the pillars are sanded to smoothen them ahead of a paint job.

Bottom part of the machine before painting
Bottom half of the machine before painting it
Detail view of stiffining elements in the machine frame
These copper tubes provide additional strength and stiffness to the frame

Besides the three small pillars, a holder for the lower stepper motor is glued to the base of the machine, right on top of the center. Before gluing, the pillars are aligned with the middle plate of the machine by running threaded rods and pieces of copper tube through the holes shown before. The copper tubing is permanently glued in place, the threaded rod is not.

The machine's "middle floor" being painted outside
Painting the middle plate
Painting the lower portion of the machine
Painting the lower half of the machine

After the glue dries, the individual pieces of the machine are painted using a base layer and multiple layers of white spray paint. I was looking to get a very smooth surface finish so some sanding between layers was needed too.

Assembly of a stepper motor into the machine frame
Close-up view of the stepper motor in its mount
Overview of stepper motor cable routing in the machine frame
Motor wires are routed through the base plate

The stepper motor for the exit tube is now ready to be mounted. Its wires are routed through the base of the machine and then go up again through one of the pillars. This is necessary, because the control unit and motor drivers are all in the upper part of the machine.

Placement and assembly of WS2812B LEDs in the machine frame
Six WS28212B LEDs are glued to the middle plate
Testing the RGB LEDs using an Arduino Uno
These LEDs act as spot lights and shine down on the candy bins

The scanning mechanism as well as the LEDs are attached to the middle plate of the machine. In the image above, I’ve glued six WS2812B leds to the middle plate to shine down on the bowls that hold the candy. They are covered by a piece of opaque acrylic to diffuse the light. Later, an outwards-facing strip of LEDs will be glued to the wooden ring for additional lighting effects.

Placement of the power connector within the frame
Power connector

A barrel connector is attached to the base of the machine. Its wires are routed upwards through a pillar in the same way as the stepper wires. The machine takes 12V power from an external source (an adapter).

Uniting the machine's bottom and middle parts
Assembly of the middle plate

The middle plate is only held in place by the threaded rod and copper guides. Glueing it in place would make it impossible to remove the exit tube and stepper motor from their mount.

The middle PVC wall of the machine in the painting process
Painting the upper body

This piece of large tubing is the upper frame part of the machine. As described before, it’s a piece of PVC. After sanding and degreasing it, a base layer of paint is applied. After that dries, a few coats of white paint are applied such as was done with the other parts.

Top-down view of the inside of the machine
Three threaded rods and wooden guides keep the PVC ring in place

For each of the three threaded rods, two guides (“camel-shaped” pieces of wood) are glued to the inside of the PVC ring to keep it in place when the machine is assembled. The part is then stacked on the bottom part of the machine. In the image above, a plywood ring is also visible. This prevents light from the LED strip (which is directly below) from interfering with the colour measurements. I had not anticipated an interaction between these LEDs and the colour sensor, which is why I had to manually cut this ring from a piece of wood as I didn’t feel like placing another lasercutting order for this part only.

Top view of the controller boards
Control unit, top view
Bottom view of the controller boards, wiring clearly visible
Control unit, bottom view

The brain of this machine consists of four individual boards – two Arduino Nanos and two EasyDrivers. One Arduino handles the scanning and colour recognition process, while the other one handles the animations for the LEDs. The EasyDrivers drive the stepper motors, since the Nano can’t do this by itself. The individual boards are plugged in using their pin headers which allows for easy replacements if one fails. It also greatly reduces the wiring mess, since each of the four boards are electrically connected using a base board that provides all the necessary interconnections.

Detail view of the singulator and color recognition unit at the heart of the machine
Detail view of the rotating wheel and sensor mount
Detail view of the microswitch that keeps track of the sorting wheel's position
Detail view of the microswitch that provides position feedback

As discussed earlier, the scanner / candy processing unit has a rotating wheel with four slots. On the right side, a color scanner is mounted to measure color. The rotating wheel is powered by a stepper motor, but to be able to precisely position the wheel in front of the sensor, a microswitch is used which closes when a slot lines up with the sensor. To achieve this, a small disc with four notches rotates in sync with the wheel. As soon as one of the notches in the disc reaches the microswitch roller, the software knows to stop rotating the wheel and starts the measurements.

Detail view of the encoder wheel
Notched disc that rotates with the motor and wheel to provide feedback
View of the candy processing unit mounted in the machine
The candy processing unit is now mounted to the frame

The scanner part is mounted on the middle plate of the machine using four bolts. To reduce noise caused by vibrations, small pieces of vinyl are placed between the wood and the plastic.

Detail view of the LED strip in the machine
Circular RGB LED strip
Front view of the assembled upper part of the machine
Opaque acrylic ring which diffuses the light

The circular LED strip is glued to the middle plate of the machine. It has exactly 32 RGB leds. It will be used to show rainbow patterns when the machine is in stand-by, and it will display measured colour during the sorting process. Between the PVC ring and the middle plate, there is an opaque ring that diffuses the light from the LED strip behind it.

Push buttons that are used for the sorting machine
Input: two illuminated push buttons
Bottom view of the top plate of the machine, buttons visible
Mounting the push buttons in the top plate

There are two pushbuttons on top of the machine with built-in LEDs. The left one is used to start and stop the sorting process whereas the right button is used to toggle between M&Ms and Skittles. This toggle is necessary because the colours of both kinds of candy are slightly different. Therefore, different calibration profiles are required.

View inside the finished machine, electronics and mechanical parts visible
System integration

After connecting all wires, this is wat the wiring situation looks like. As is shown, a buck converter was also introduced to reduce the 12 V input voltage to 5 V for the LED strips, Arduinos and mixer servo. I have to admit that all of this looks slightly messy, but it is what it is, and most importantly, the machine works!

View of the top of the machine and the funnel filled with M&Ms
Putting the top plate on and loading some candy into the hopper

Finally, the top plate is put in place and it is secured by putting cap nuts on the threaded rods. The hopper/funnel is also added. The machine is now completely ready to sort M&M’s and Skittles!

Several bins filled with M&Ms sorted by colour
Sorted M&Ms

This concludes the build log of my candy sorting machine. Thank you for reading this post! If you have any suggestions, remarks, questions or anything else, feel free to contact me and I’ll get back to you as soon as possible. Please do remember to check out the links at the top of this page if you are looking for design files.


  1. Nice! Do you eat the red ones last? Have you found that there is an even distribution of colours? Perhaps the device can count how many of each colour and give totals too!

Leave a comment

Your email address will not be published. Required fields are marked *