LEGO Brick Sorter

LEGO Brick Sorter

Course: Measurements 2 (MEMS 1043) — University of Pittsburgh

Team 14: James Puzon, Kalinda Wagner, Kyleigh Motley

View on GitHub

LEGO Brick Sorter experimental set-up

Experimental set-up: ultrasonic distance sensors and webcam for LEGO block identification

Project Overview

This project developed a system to identify LEGO blocks based on geometric and visual data, with the potential application of sorting LEGO bricks automatically. The system combines two independent sensing approaches:

Error analysis was performed for each measurement method to assess the capabilities and limitations of the system.

Sensor Selections

Sensor Quantity Manufacturer Make / Model Sensitivity
Ultrasonic Distance Sensor 2 Adafruit 4007 ±0.5 cm
Ultrasonic Distance Sensor 1 Adafruit HC-SR04 ±0.5 cm
Web Camera 1 Logitech C920s Pro HD Webcam

Equipment List

Equipment Quantity Availability Cost
Distance Sensor (Adafruit 4007) 2 In lab $3.95 / sensor
Distance Sensor (HC-SR04) 1 In lab $3.95
Web Camera (Logitech C920s) 1 From group member $54.99
Arduino 1 In lab $19.99
Wire 1 ft In lab $5.00 (approx.)
Small Breadboard 3 In lab $4.95
Poster Board 1 From group member $5.00 (approx.)
Tape 8 in In lab $2.00
LEGOs (various sizes) 20 pieces From group member $50.00 (approx.)
Ruler 1 In lab $4.50
Total $162.19 (approx.)

Color Recognition

Calibration

Before detection, the system is calibrated to build a dictionary of color ranges for accurate color classification. This calibration process is run once (or whenever new colors need to be added):

  1. Center a LEGO block within the defined detection region on screen
  2. The script samples and outputs HSV pixel data from the detection box
  3. The user inputs the corresponding color label
  4. The script appends or replaces the HSV range and label in the color database

Detection Process

A custom Python script identifies each LEGO block's color in real time using the Logitech C920s webcam:

  1. LEGO piece is placed within the camera's field of view
  2. Script converts the image from BGR to HSV color space
  3. Measured HSV values are compared to the saved color dictionary
  4. The detected color label is output to the user

The script supports both vibrant colors (red, yellow, green, blue) and neutral colors (black, white, gray, brown). A secondary window displays the full color dictionary with HSV ranges, updated in real time. Tolerance values used: Hue ±3, Saturation ±15, Value ±3.

Distance Measurements

Calibration

Sensors are calibrated using an Arduino sketch run while the measurement area is empty of LEGOs:

  1. Sensors collect data for 5 seconds in each direction (X, Y, Z)
  2. Data is averaged to produce the nominal distance to the wall in each direction
  3. These nominal values are entered into the measurement script as reference baselines

Measurement Process

Ultrasonic sensors determine LEGO geometry by emitting and receiving sound pulses:

  1. LEGO block is placed within the sensing range of all three sensors
  2. Sensors fire a few milliseconds apart to avoid signal interference between them
  3. For 10 seconds, each sensor records its distance to the LEGO block
  4. Measured distances are subtracted from the nominal (empty-area) distance
  5. Final dimensions are obtained by averaging the X, Y, and Z measurements
  6. Final dimensions are recorded in Excel for analysis

Experimental Results

A sample of five LEGO blocks was measured using the test and calibration procedures. Percent and RMS errors were calculated for each block.

Average Length Error 23.24%
Average Width Error 16.19%
Average Height Error 113.65%
Global RMS Error 116.33%
Color Detection Accuracy 100%

In general, average absolute geometric error was high, indicating issues with sensor placement, set-up design, or signal interference — particularly for height measurements. Color detection accuracy was 100%, demonstrating that the Python/OpenCV-based approach is highly reliable for this task.

Design of Experiments

Research Question: How does lighting, background color, and camera quality affect how the camera reads the LEGO brick color?

Run Lighting Background Camera Quality
1DimWhiteLow
2BrightWhiteLow
3DimBlackLow
4BrightBlackLow
5DimWhiteHigh
6BrightWhiteHigh
7DimBlackHigh
8BrightBlackHigh

Experimental Strengths

Experimental Limitations

Future Improvements

Technologies Used

View on GitHub ← Back to Portfolio