top of page

Automatic

Sorter

This project is an Automatic Sorting Machine built using an Arduino Uno. It detects and sorts objects based on colour using a colour sensor. Once an object is detected, the Arduino activates a servo motor and positions it at a certain angle based on colour. This type of project is commonly used for educational purposes and DIY recycling or automation systems. 

​

How It Works

Objects are placed one at a time on a conveyor or slide where a sensor reads a specific property — for example, a colour sensor (TCS3200), an IR distance sensor, or a metal detector coil. Based on the sensor’s output, the Arduino classifies the item and moves a servo motor or actuator to direct it to the correct destination

Materials Anchor

Materials

To build an Automatic Sorter project with an Arduino Uno, you'll need the following materials:

Components:

​

1) Arduino Uno

The microcontroller that reads sensor data and controls sorting

​

2) TCS3200 Colour Sensor

Detects object colour (can be replaced with IR or metal sensor)

​

3) IR Sensor or Limit Switch

Detects when an object is present

 

4) Servo Motor (e.g. SG90 or MG996R)

Moves a flap or arm to direct the item​

​

5)Breadboard and jumper wires

  • Connect components to the Arduino

​​​​

6) USB Cable (Type-A to Type-B)

  • To connect Arduino to a computer for programming and power.

Setup Anchor

Basic Setup

TCS3200 Colour Sensor Module:

  • VCC → Connect to 5V on the Arduino

  • GND → Connect to GND on the Arduino

  • OUT → Connect to digital pin 2

  • S0 → Connect to digital pin 3

  • S1 → Connect to digital pin 4

  • S2 → Connect to digital pin 5

  • S3 → Connect to digital pin 6

  • LED → Connect to digital pin 8 (so Arduino can control when the lights turn on)

​

IR Object Detector (or IR Sensor Module):

  • VCC → Connect to 5V on the Arduino

  • GND → Connect to GND on the Arduino

  • OUT → Connect to digital pin 7

​

Servo Motor:

  • Signal → Connect to digital pin 9

  • VCC → Connect to 5V (or external power if needed)

  • GND → Connect to Arduino GND (shared ground)

​

Software Setup

Install Servo.h library, this is already native to the Arduino IDE and doesn't need to be installed manually.

Code

CODE BREAK-DOWN

Code Break Down

const int leds[4] = {4, 5, 6, 7};

  • Defines an array containing the digital pin numbers for the 4 LEDs. Each index corresponds to one LED.

​

const int buttons[4] = {8, 9, 10, 11};

  • Defines an array with the pin numbers for the 4 buttons, each paired with an LED by index.

​

const int buzzer = 3;

  • Assigns pin 3 to the buzzer. This will be used to output tones during the game.

​

int sequence[100];

  • Creates an array to store up to 100 values representing the LED sequence the player must remember.

​

int level = 0;

  • Keeps track of the current game level (or sequence length). Starts at 0 and increases as the player succeeds.

​

void setup() {

  • Starts the setup section, which runs once when the Arduino powers on or resets.

​

for (int i = 0; i < 4; i++) { pinMode(leds[i], OUTPUT); pinMode(buttons[i], INPUT); }

  • Loops through all 4 LEDs and buttons, setting the LED pins as OUTPUT and button pins as INPUT (for reading presses).

​

pinMode(buzzer, OUTPUT);

  • Sets the buzzer pin as an output so tones can be played.

​

randomSeed(analogRead(0));
Seeds the random number generator using noise from analog pin 0 for better randomness.

​

nextRound();

  • Calls the function to start the first round and generate the first LED in the sequence.

​

void loop() {

  • Begins the main game loop, which runs continuously.

​

if (!playSequence()) { gameOver(); nextRound(); }

  • Plays the current sequence. If the player gets it wrong, the game over sequence plays and a new game starts.

​

delay(500);

  • Pauses for half a second between rounds.

​

level++;

  • Increases the level after a successful round.

​

sequence[level] = random(0, 4);

  • Adds a new random LED to the sequence, choosing a number from 0 to 3 (representing the 4 LEDs).

​

bool playSequence() {

  • Starts a function that plays the sequence and checks player input.

​

for (int i = 0; i <= level; i++) { lightLED(sequence[i]); delay(250); clearLEDs(); delay(250); }

  • Loops through the sequence, lights each LED briefly, and clears it to show the pattern to the player.

​

for (int i = 0; i <= level; i++) { int guess = waitForButton(); if (guess != sequence[i]) { return false; } }

  • Waits for player input, one button at a time. If any input doesn’t match the stored sequence, the function returns false (indicating a mistake).

​

return true;

  • If the player completed the whole sequence correctly, return true.

​

int waitForButton() {

  • Starts a function that waits until one of the buttons is pressed.

​

while (true) { for (int i = 0; i < 4; i++) { if (digitalRead(buttons[i]) == HIGH) {

  • Loops forever until any one of the 4 buttons reads HIGH (pressed).

​

lightLED(i); delay(200); clearLEDs();

  • Lights the matching LED for feedback and clears it shortly after.

​

while (digitalRead(buttons[i]) == HIGH);

  • Waits until the player releases the button (prevents multiple counts from one press).

​

return i;

  • Returns the index of the button that was pressed.

​

void lightLED(int i) {

  • Lights up the LED at index i and plays a tone based on its index.

​

digitalWrite(leds[i], HIGH);

  • Turns on the selected LED.

​

tone(buzzer, 200 + i * 100, 150);

  • Plays a short tone using the buzzer. The frequency depends on which LED is lit.

​

void clearLEDs() {

  • Turns off all the LEDs.

​

for (int i = 0; i < 4; i++) { digitalWrite(leds[i], LOW); }

  • Loops through all LED pins and turns them off.

​

void gameOver() {

  • Plays a game over pattern of lights and sound.

​

for (int i = 0; i < 3; i++) { for (int j = 0; j < 4; j++) { digitalWrite(leds[j], HIGH); }

  • Flashes all LEDs on.

​

tone(buzzer, 200, 300); delay(300); clearLEDs(); delay(300); }

  • Plays a low tone, waits, turns off LEDs, and pauses briefly — repeated 3 times.

​

level = 0;

  • Resets the level to start a new game.

​

void nextRound() {

  • Prepares for the next game session or restarts the first round.

​

sequence[0] = random(0, 4);

  • Creates the first item in the new LED sequence.

​

level = 0;

  • Sets the level back to zero.

bottom of page