ESP32 Quality Control System Document Complete Wiring Guide, Working Procedure, and C++ Code Reference 1. System Overview & Working Procedure This automated quality control system uses an ESP32 microcontroller paired with a VL53L1X Time-of-Flight Laser Sensor to measure product height in millimeters with high precision on a conveyor line. Step 1: Precision Distance Measurement The VL53L1X sensor emits invisible infrared laser pulses to accurately measure the height of each item passing underneath in millimeters. Step 2: Processing & Decision Logic The ESP32 evaluates the measured height against programmed thresholds (80 mm to 150 mm). The exact dimension is shown real-time on a 16x2 I2C LCD screen. Step 3: Automated Sorting & Visual Feedback PASSED (Valid Height): RGB LED turns GREEN , LCD displays "Status: PASSED", and the Servo Motor holds at 45° to keep the item on the main belt. REJECTED (Out of Spec): RGB LED turns RED , LCD displays "Status: REJECT", and the Servo Motor pivots to 135° acting as a barrier to divert the item to the rejection chute. 2. Circuit Wiring & Connections Guide Component Component Pin ESP32 Connection Wire Color / Note VL53L1X Laser Sensor VCC / GND / SDA / SCL 3.3V / GND / GPIO 21 / GPIO 22 I2C Interface 16x2 I2C LCD Display VCC / GND / SDA / SCL 5V / GND / GPIO 21 / GPIO 22 Shared I2C Bus (Address 0x27) Servo Motor VCC / GND / Signal (PWM) 5V / GND / GPIO 13 Orange PWM Signal Wire RGB LED (Common Cathode) Red (Anode) GPIO 4 Via 220Ω Resistor Cathode (GND) GND Rail Longest Pin Green (Anode) GPIO 2 Via 220Ω Resistor Blue (Anode) GPIO 15 Via 220Ω Resistor • • 3. Complete C++ Source Code (sketch.ino) #include <Wire.h> #include <LiquidCrystal_I2C.h> #include <ESP32Servo.h> #include <Adafruit_VL53L1X.h> #define SERVO_PIN 13 #define RGB_RED 4 #define RGB_GREEN 2 #define RGB_BLUE 15 LiquidCrystal_I2C lcd(0x27, 16, 2); Servo sorterServo; Adafruit_VL53L1X vl53; const int MIN_VALID_MM = 80; // Minimum valid height in mm const int MAX_VALID_MM = 150; // Maximum valid height in mm void setRGBColor(bool red, bool green, bool blue) { digitalWrite(RGB_RED, red ? HIGH : LOW); digitalWrite(RGB_GREEN, green ? HIGH : LOW); digitalWrite(RGB_BLUE, blue ? HIGH : LOW); } void setup() { Serial.begin(115200); pinMode(RGB_RED, OUTPUT); pinMode(RGB_GREEN, OUTPUT); pinMode(RGB_BLUE, OUTPUT); setRGBColor(false, false, true); // Blue LED during Boot Wire.begin(21, 22); lcd.init(); lcd.backlight(); sorterServo.attach(SERVO_PIN); sorterServo.write(90); if (!vl53.begin(0x29, &Wire)) { lcd.setCursor(0, 0); lcd.print("Laser Error!"); while (1); } vl53.startRanging(); lcd.setCursor(0, 0); lcd.print(" PRECISION QC "); lcd.setCursor(0, 1); lcd.print(" ACCURACY: 1mm "); delay(1200); lcd.clear(); } void loop() { if (vl53.dataReady()) { int distanceMm = vl53.distance(); vl53.clearInterrupt(); lcd.setCursor(0, 0); lcd.print("Height: "); lcd.print(distanceMm); lcd.print(" mm "); if (distanceMm >= MIN_VALID_MM && distanceMm <= MAX_VALID_MM) { lcd.setCursor(0, 1); lcd.print("Status: PASSED "); setRGBColor(false, true, false); // Green sorterServo.write(45); } else { lcd.setCursor(0, 1); lcd.print("Status: REJECT "); setRGBColor(true, false, false); // Red sorterServo.write(135); } } delay(150); }