M&M Sorter using OpenCV and Arduino

by on May.23, 2013, under Code, Hardware, IT

After my first OpenCV project which dealt with object tracking, I decided to do something a little more challenging and complex, but also more fun. I went for M&M sorting. You might have already seen this idea being realized somewhere online, I know I’ve seen a ton of different examples. However, everyone seems to have a different approach to doing it, so I figured I’ll give it a shot myself.

For those of you that want to jump straight into the action or just want to watch the pretty M&Ms fly about, you can go directly to my video showing the program execution:

If you’re interested in some more details on how the sub-parts of the project function, take a look at this video explaining things a bit better:

Finally, for those of you more into reading, and those looking for the specifics let me present the fine details of the project:

– Nothing special about the loader and unloader. They are simple servo motors which are just instructed to rotate back and forth to drop the M&Ms.

– The print head of the printer (Epson C43UX) acts as a transport mechanism. It’s run by the printer’s original bipolar stepper motor (Model EM-462). The stepper itself is being controlled by an SN754410NE H-Bridge. The following image taken from the Arduino site ( shows how to connect and drive the stepper motor:

– NOTE: I couldn’t find too many details about this stepper motor online so I had to do most of my calculations manually by different measurements and experimentation. As it turns out, the motor (EM-462) has a coil resistance of 9.2 Ohms and a coil wire diameter of 0.28 +/- 0.02 mm. Its current rating is 1A. Using Ohm’s law we may calculate that the motor voltage rating is around 9V. Also, the stepper moves 7.5 degrees per step, so 48 steps for a complete circle. This is important to know in order to set the stepper up properly in the Arduino code. I had it setup with 50 steps in the beginning and it was skipping steps every now and then.

– The printer’s power supply has 3 wires. I cut off the middle wire, and just took the positive voltage and ground wires. The power supply gives a voltage of 36.1V between those wires. I used a 7809 voltage regulator to drop the voltage down to 9V, however, 36V is a bit of an upper limit on that regulator so it gets extremely hot. I’ll probably look into using the LM317 regulator to get the project running more stable.

– NOTE: I have a disassembly video for the Epson C43UX which I will upload as soon as possible and link it to this post so you can follow along if you want.

That’s more or less it. There are a bunch of places where I could improve on this project, like the loading mechanism which is quite flimsy in its current state. Also, I’m probably going to include a white LED to point to the M&Ms while the camera analyzes them so that I get better results regardless of lighting conditions. Additionally, I’ll do some more work on the 9V voltage regulator so that I find a good solution which won’t overheat as much as what I currently have. I’ll be sure to post updates as soon as any improvements come along.

You can download the OpenCV (C++) code here, and the Arduino code here.

I hope you enjoyed this little project. Feel free to post any questions or comments.

:, , , , , , ,

2 Comments for this entry

  • Lawrence

    Hello. Nice project.
    I am lawrence from philippines.
    I have question.

    If you increase the supply voltage of the stepper motor, from 9v to 20v(example), the current intake of the stepper motor will decrease (from 1A to lower value).is this possible? Because I saw the schematic of the original driver, it’s supply voltage to the allegro chip driver is 36v.


  • parshav

    i am trying to make an interactive head. i have used opencv to detect face using a webcam. for giving command to the port i have used Tserial.h and tserial.cpp file. my visual c++ code is compiling without error and so as my arduino code. the problem is my motors are not working according to the code, means it is not copying face movement. what should i do? please help….
    used basic idea from

Leave a Reply