ESP32 Quality Control System - Full Project Guide Complete Working Explanation, Component Details, Circuit Wiring, Code, and Practical Use Cases 1. Component Breakdown & How They Work ( பயன்படுத்தப்பட்ட பாகங்கள் ) ESP32 Microcontroller: The main brain of the system. It processes timing signals from the ultrasonic sensor, calculates distance, updates the LCD, controls the RGB LED, and drives the Servo Motor. HC-SR04 Ultrasonic Sensor: Measures product height/distance by sending high-frequency sound waves (40 kHz) through the TRIG pin and measuring the echo return time on the ECHO pin. 16x2 I2C LCD Display: Displays live height readings and status (PASSED/REJECT). Uses I2C communication (SDA/SCL) to minimize wiring. RGB LED (Common Cathode): Visual indicator showing status: Blue for Boot, Green for PASSED, and Red for REJECT. Servo Motor (SG90): Acts as a physical sorting gate/barrier. Rotates to 45° for passed products and 135° to block/divert rejected items. 2. System Working Principle ( ஒட்டு.மாத்த இயக்கம் ) Step 1: Ultrasonic Distance Measurement The HC-SR04 sensor sends ultrasonic sound waves downward toward the product on the conveyor and measures the bounce-back time to calculate exact height in centimeters. Step 2: Processing & Threshold Checking The ESP32 processes the duration formula ( distance = duration * 0.034 / 2 ) and compares it against upper and lower height limits (e.g., 8 cm to 65 cm). Step 3: Actuation & Visual Output PASSED: If within tolerance range ➔ LCD shows "Status: PASSED", RGB LED glows Green, Servo remains at 45° (open lane). REJECTED: If outside tolerance range ➔ LCD shows "Status: REJECT", RGB LED glows Red, Servo moves to 135° (barrier gate blocks/diverts item). • • • • • • • 3. Circuit Wiring & Connections Guide ( சர்க்யூட் இ0ணைப்பு ) Component Component Pin ESP32 Connection Pin Notes HC-SR04 Ultrasonic Sensor VCC / GND / TRIG / ECHO 5V / GND / GPIO 5 / GPIO 18 Distance Measuring Sensor 16x2 I2C LCD Display VCC / GND / SDA / SCL 5V / GND / GPIO 21 / GPIO 22 Shared I2C Bus (Address 0x27) Servo Motor VCC / GND / PWM Signal 5V / GND / GPIO 13 Sorting Barrier Mechanism RGB LED (Common Cathode) Red (Anode) / GND / Green / Blue GPIO 4 / GND / GPIO 2 / GPIO 15 Connect Anodes via 220Ω Resistors 4. Real-World Practical Use Cases ( .தாழிற்சா0லை பயன்பாடுகள் ) Automated Packaging & Bottling Lines: Sorting bottles by fill level/height before capping to reject underfilled items. Logistics & Warehousing: Filtering out-of-spec packages or boxes on high-speed conveyor belts. Automotive Component Assembly: Ensuring precise physical dimensional height tolerance before final component installation. 5. C++ Source Code (sketch.ino) #include <Wire.h> #include <LiquidCrystal_I2C.h> #include <ESP32Servo.h> #define TRIG_PIN 5 #define ECHO_PIN 18 #define SERVO_PIN 13 #define RGB_RED 4 #define RGB_GREEN 2 #define RGB_BLUE 15 LiquidCrystal_I2C lcd(0x27, 16, 2); Servo sorterServo; const int MIN_VALID_DIST = 8; // Minimum height limit in cm const int MAX_VALID_DIST = 65; // Maximum height limit in cm 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(TRIG_PIN, OUTPUT); pinMode(ECHO_PIN, INPUT); pinMode(RGB_RED, OUTPUT); pinMode(RGB_GREEN, OUTPUT); pinMode(RGB_BLUE, OUTPUT); setRGBColor(false, false, true); // Blue on boot Wire.begin(21, 22); lcd.init(); lcd.backlight(); sorterServo.attach(SERVO_PIN); sorterServo.write(90); lcd.setCursor(0, 0); lcd.print(" QUALITY CONTROL"); lcd.setCursor(0, 1); lcd.print(" SYSTEM READY "); delay(1200); lcd.clear(); } void loop() { digitalWrite(TRIG_PIN, LOW); delayMicroseconds(2); digitalWrite(TRIG_PIN, HIGH); delayMicroseconds(10); digitalWrite(TRIG_PIN, LOW); long duration = pulseIn(ECHO_PIN, HIGH, 30000); int distanceCm = (duration > 0) ? (duration * 0.034 / 2) : 0; lcd.setCursor(0, 0); lcd.print("Height: "); lcd.print(distanceCm); lcd.print(" cm "); if (distanceCm >= MIN_VALID_DIST && distanceCm <= MAX_VALID_DIST) { lcd.setCursor(0, 1); lcd.print("Status: PASSED "); setRGBColor(false, true, false); // Green LED sorterServo.write(45); // Open path } else { lcd.setCursor(0, 1); lcd.print("Status: REJECT "); setRGBColor(true, false, false); // Red LED sorterServo.write(135); // Barrier active } delay(200); }