Introduction: EAL M&M Sorting Machine

This instructable is about making a Machine that sorts M&M's by colour.

The machine is designed in 123D CAD-software and printed by a DaVinci jr.1.0 printer.

It is made with 2 small stepper motors driven by 2 SBT0811 and 1 colour sensor, GY-31, all controlled by an Arduino Mega 2560.

Step 1: Creating the Machine

123D by AutoCAD is fairly simple to use. I have little experience in drawing 3D but i think the result has come out allright. All parts are created from scratch.

Using the DaVinci Jr. 1.0 is OK, but it is not as accurate as some of the more expensive printers. This made me print almost all parts twice or more, before it all fit together.

Step 2: Wiring Diagram and I/O-list

I/O-list

Each of the stepper motors requires 4 PWM outputs and a 5V supply and ground as well:

Driver for Stepper Motor for the Sorting Wheel

  • Pin 2 => IN1
  • Pin 3 => IN3
  • Pin 4 => IN2
  • Pin 5 => IN4

Driver for the Stepper Motor for the Dispenser

  • Pin 8 => IN1
  • Pin 9 => IN3
  • Pin 11 =>IN2
  • Pin12 =>IN4

The Colour Sensor requires four digital outputs and an analogue input and 5V + ground:

Digital IN

  • Pin 22 => S0
  • Pin 24 => S1
  • Pin 26 => S2
  • Pin 28 => S3

Analogue IN

  • Pin 7 => OUT

Step 3: Programming the Machine

Programming the Arduino was quite a challenge since my experience with this is very little.

It is a very simple program, roughly 3 steps as shown in the flowchart.

  1. The colour sensor reads the Red, Green and Blue values of the M&M
  2. The dispenser moves to predefined position
  3. The Sorting wheel turns 90 degrees, disposes the M&M, moves a new to the sensor and gets a second ready

The colour sensor gave me quite a few problems. Every time i defined limits for two or three colours, the values changed and i would start over. In the end i just threw in the towel and decided to make the machine only sort red, green and blue (for now, i will make it work at some point ;) ).

The two stepper motors are fairly easy to use. It is the first time i have programmed stepper motors, so some of you probably have a better way for the positioning, but it works.

#include "Stepper.h"
#define STEPS 32 //Number of steps for rev of internal shaft

//2048 steps for one rev of external shaft

#define S0 22

#define S1 24

#define S2 26

#define S3 28

#define sensorOut 7

int PrevPos=0; //Last position

int Steps2take=0; //How much to move stepper

//RGB-sensor (GY-31)

int red; //The value of the red colour

int blue; //The value of the blue colour

int green; //The value of the green colour

int steps_SW=512; //Move sorting wheel 90 degrees

Stepper dispenser(STEPS, 8, 10, 9, 11);

Stepper sorting_wheel(STEPS, 2,3,4,5);

void setup() {

pinMode(S0, OUTPUT);

pinMode(S1, OUTPUT);

pinMode(S2, OUTPUT);

pinMode(S3, OUTPUT);

pinMode(sensorOut, INPUT);

digitalWrite(S0,HIGH); // Setting frequency-scaling to 20%

digitalWrite(S1,LOW);

Serial.begin(9600);

}

void loop() {

digitalWrite(S2,LOW); //Setting red filtered photodiodes to be read

digitalWrite(S3,LOW);

red = 1.1*(pulseIn(sensorOut, LOW)); //Reading and modifying the output frequency

Serial.print("R="); // Printing the value on the serial monitor

Serial.print(red);

Serial.print(" ");

delay(300);

digitalWrite(S2,HIGH); // Setting Green filteres photodiodes to be read

digitalWrite(S3,HIGH);

green = (pulseIn(sensorOut, LOW)); //Reading the output frequency

Serial.print("G="); // Printing the value on the serial monitor

Serial.print(green); Serial.print(" ");

delay(300);

digitalWrite(S2,LOW); // Setting Blue filteres photodiodes to be read

digitalWrite(S3,HIGH);

blue = 1.3*(pulseIn(sensorOut, LOW)); //Reading and modifying the output frequency Serial.print("B="); // Printing the value on the serial monitor

Serial.print(blue);

Serial.println(" ");

delay(300);

//Positioning of the dispenser according to the colour read

if((red < blue && red < green) && (PrevPos==0)){(Steps2take = 684);}

if((red < blue && red < green) && (PrevPos==342)){(Steps2take = 684-342);}

if((red < blue && red < green) && (PrevPos==684)){(Steps2take = 0);}

if((red < blue && red < green) && (PrevPos==1024)){(Steps2take = 1708-1024);}

if((red < blue && red < green) && (PrevPos==1366)){(Steps2take = 1708-1366);}

if((red < blue && red < green) && (PrevPos==1708)){(Steps2take = 0);}

if((blue < red && blue < green) && (PrevPos==0)){(Steps2take = 0);}

if((blue < red && blue < green) && (PrevPos==342)){(Steps2take = 1024-342);}

if((blue < red && blue < green) && (PrevPos==684)){(Steps2take = 1024-684);}

if((blue < red && blue < green) && (PrevPos==1024)){(Steps2take = 0);}

if((blue < red && blue < green) && (PrevPos==1366)){(Steps2take = 2048-1366);}

if((blue < red && blue < green) && (PrevPos==1708)){(Steps2take = 2048-1708);}

if((green < red && green < blue) && (PrevPos==0)){(Steps2take=342);}

if((green < red && green < blue) && (PrevPos==342)){(Steps2take = 0);}

if((green < red && green < blue) && (PrevPos==684)){(Steps2take = 1366-684);}

if((green < red && green < blue) && (PrevPos==1024)){(Steps2take = 342);}

if((green < red && green < blue) && (PrevPos==1366)){(Steps2take = 0);}

if((green < red && green < blue) && (PrevPos==1708)){(Steps2take = 2048-1366);}

Serial.print(Steps2take); //Monitoor values "Steps2take" & "PrevPos"

Serial.println(" steps ");

Serial.println(" ");

Serial.print(PrevPos);

Serial.println(" prevpos ");

dispenser.setSpeed(600);

if((blue < red && blue < green) || (green < red && green < blue) || (red < blue && red < green)){ dispenser.step(Steps2take);} //Go to correct position

PrevPos=(PrevPos+Steps2take); //New position

if (PrevPos==2048){(PrevPos=0);} //Reset after one revolution

if (PrevPos==2390){(PrevPos=342);} //Reset after one revolution

Steps2take=0; //Reset Steps2take

if(Steps2take==0){ //Dispense M&M and get a new one

sorting_wheel.setSpeed(300);

sorting_wheel.step(-steps_SW);

delay(500);

}

}

Step 4: Video

The total cost of this machine is about 100€-150€. You might be able to do it for less if you, unlike me, are ready to wait for everything to arrive from china. I bought it all from various online shops in Europe.