1. Introduction The quartz crystal wafer is the core of a quartz crystal oscillator. After being encapsulated, it becomes a quartz crystal oscillator, a widely used electronic component in electronic devices. During the production of quartz crystal wafers, processes such as cutting and grinding are involved. Some wafers will develop physical defects such as chipping, splintering, cracks, fractures, and scratches. These defective wafers must be screened out. Currently, production mainly relies on manual labor, visually inspecting the quartz crystal wafers for defects. This method has the following drawbacks: 1) Inconsistent inspection results. The shapes of defects vary greatly. Due to differences in human vision and perception, it is difficult to accurately describe defects graphically and in words. Therefore, there are no industry standards, and companies lack quantifiable inspection standards. Even the same operator may produce inconsistent results from two inspections. 2) Inspection results are easily affected by human factors. Due to the monotonous nature of the work, long hours, and eye fatigue, inspection relies entirely on intuition. The results largely depend on the operator's mental and physical condition. With the rapid development of computer, image processing, communication, and control technologies, the technological foundation for changing the current state of quartz crystal wafer inspection has been established. This paper presents a photoelectric-mechatronics quartz crystal wafer sorting system based on image processing, opening up new avenues for quartz crystal wafer quality screening. 2. System Working Principle The quartz crystal wafer quality sorting system is a high-tech photoelectric-mechatronics device. It uses computer image processing technology to identify the quality of quartz crystal wafers. After completing the manual modeling of the samples, it can automatically complete the conveying, detection, and classification of quartz crystal wafers, completely replacing manual inspection and greatly improving product quality. The quartz crystal wafer quality sorting system consists of six hardware components: material conveying, sorting mechanism, image acquisition, controller, power supply, and computer host, as shown in Figure 1. Software-wise, it consists of two parts: controller software and image processing software package. The material conveying unit consists of a vibrating conveyor and a drive circuit to achieve automatic material conveying. The vibrating conveyor includes both circular and linear vibrators; the circular vibrator also functions as a barrel-shaped hopper. A spiral-shaped feed channel runs from bottom to top on the barrel wall. Quartz crystal slices, under vibration, move continuously and neatly along the channel, eventually reaching the hopper outlet. The inlet of the linear vibrator is connected to the outlet of the circular vibrator. A photoelectric switch at the outlet of the linear vibrator detects the presence of a quartz crystal slice at that location. The quartz crystal slices move linearly under vibration, stopping when the vibration ceases. The sorting mechanism unit consists of a stepper motor, a detection plate, a sorting plate, and an electromagnet. The detection plate and sorting plate are mounted on the stepper motor shaft and driven to rotate synchronously. Both the detection plate and the sorting plate have eight evenly distributed holes. The bottom of the sorting plate has a flip cover with a permanent magnet installed on it. When the detection plate is stationary, the outlet of the linear vibrator aligns with one of the holes on the detection plate, allowing a quartz crystal slice to fall into it. Simultaneously, the next hole on the detection plate corresponds to the optical microscope lens. An electromagnet is installed in the stationary position of the sorting plate. When the electromagnet is energized, the flip cover opens, allowing the quartz crystal slice in that hole to fall into the hopper. The image acquisition unit consists of an optical microscope, a CCD camera, and a high-resolution image capture card. The image capture card is installed in the computer host. Satisfactory images can be obtained by adjusting the focal length and magnification of the optical microscope. The CCD camera performs photoelectric conversion of the image, and the image capture card ultimately completes the computer imaging. The controller unit consists of a microcontroller, a USB communication interface, a stepper motor drive circuit, an electromagnet drive circuit, and a vibratory feeder control circuit. The microcontroller receives control commands and classification commands through the USB communication interface, thereby controlling the mechanical actions of the entire system. The image processing software is an application software based on the Windows operating system, featuring human-computer interaction, sample generation, digital image processing, classification, and overall system management functions. This software is written in VC++. Taking the sorting process of a quartz crystal slice as an example, the system's working principle is as follows: The material conveying unit consists of a vibratory feeder and a drive circuit to achieve automatic material conveying. The vibratory feeder includes both circular and linear vibrators; the circular vibrator also functions as a barrel-shaped hopper. A spiral-shaped feed channel runs from bottom to top on the barrel wall. Quartz crystal slices, under vibration, move continuously and neatly along the channel, eventually reaching the hopper outlet. The inlet of the linear vibrator is connected to the outlet of the circular vibrator. A photoelectric switch at the outlet of the linear vibrator detects the presence of a quartz crystal slice at that location. The quartz crystal slices move linearly under vibration, stopping when the vibration ceases. The sorting mechanism unit consists of a stepper motor, a detection plate, a sorting plate, and an electromagnet. The detection plate and sorting plate are mounted on the stepper motor shaft and driven to rotate synchronously. Both the detection plate and the sorting plate have eight evenly distributed holes. The bottom of the sorting plate has a flip cover with a permanent magnet installed on it. When the detection plate is stationary, the outlet of the linear vibrator aligns with one of the holes on the detection plate, allowing a quartz crystal slice to fall into it. Simultaneously, the next hole on the detection plate corresponds to the optical microscope lens. An electromagnet is installed in the stationary position of the sorting plate. When the electromagnet is energized, the flip cover opens, allowing the quartz crystal slice in that hole to fall into the hopper. The image acquisition unit consists of an optical microscope, a CCD camera, and a high-resolution image capture card. The image capture card is installed in the computer host. By adjusting the focal length and magnification of the optical microscope, satisfactory images can be obtained. The CCD camera realizes photoelectric conversion of the image, and the image capture card finally completes computer imaging. The controller unit consists of a microcontroller and a USB communication interface, a stepper motor drive circuit, an electromagnet drive circuit, and a vibratory feeder control circuit. The microcontroller receives control commands and classification commands through the USB communication interface, thereby controlling the mechanical actions of the entire system. The image processing software is an application software based on the Windows operating system, which has the functions of human-computer interaction, sample generation, digital image processing, classification, and management of the entire system. The software is written in VC++. Taking the sorting process of a quartz crystal slice as an example, the system working principle is as follows: 1) The controller controls the vertical vibration to start, and a quartz crystal slice falls into the hole of the detection disk. The photoelectric signal on the vertical vibration is fed back to the controller, and the vertical vibration stops. 2) The stepper motor rotates 45°, and the quartz crystal slice that has just fallen into the detection tray is brought under the lens of the optical microscope. The controller sends a positioning completion signal to the computer host. 3) After receiving the positioning completion signal, the image processing software package on the computer host first calls the camera program to take an image of the quartz crystal slice. Then the computer host sends a camera completion signal to the controller, calls the image processing program to classify the slice according to the pre-defined rules, and sends the classification results to the controller. 4) After receiving the camera completion signal, the controller starts the stepper motor. 5) During the rotation of the stepper motor, the quartz crystal slice falls from the detection tray into the sorting tray. 6) After receiving the classification results, the controller drives the corresponding electromagnet according to the classification results when the stepper motor stops, causing the flip cover to open and the quartz crystal slice to fall into the corresponding container. In production, the above steps are continuous and synchronous. Because the detection tray runs slowly, image processing is basically performed during the operation of the detection tray. The actions of loading quartz crystal slices, taking images, and unloading are realized during each stop of the stepper motor. 3. System Hardware Circuit Design The hardware circuit includes a USB interface device CH375, an electromagnet drive bridge, a direct-vibration photoelectric signal comparator, a P89LPC932 microcontroller, and a stepper motor driver. The stepper motor driver is used in conjunction with the stepper motor. The rotation direction of the driver selected in this system can be set once and will not be changed. The drive signal is a pulse train; the number of pulses determines the rotation angle, and the pulse frequency determines the rotation speed. Pulse input simplifies the connection between the microcontroller and the driver. In this system, the microcontroller's T2 timer is set to an auto-reload time constant timer to control the pulse frequency. P1.0 is set to auto-load pulse output and connected to T0. T0 is set to counter mode, loading the output pulse count. When the count reaches 0, the pulse output of P1.0 stops. A permanent magnet is embedded in the flip cover of the sorting disk. The flip cover itself is closed by spring pressure. Energizing the electromagnet to generate a magnetism opposite to that of the electromagnet allows the flip cover to be opened. The electromagnet is then reverse-energized to produce the same magnetism as the magnet, accelerating the flip-top closing speed. The electromagnet drive circuit uses a full-bridge structure, with the coil connected at the midpoint of the two bridge arms. When either input is high, the upper half of one bridge arm and the lower half of the other bridge arm conduct, causing the electromagnet to operate. When both inputs are low, the bridge circuit remains open, and the electromagnet does not operate. Communication between the controller and the computer host is achieved through a CH375 USB interface circuit. To improve production efficiency, it is desirable to maximize the real-time performance of the communication between the controller and the computer host. Considering the relatively small amount of data transmitted, the response speed of USB communication is sufficient. The microcontroller's interrupt input INT0 receives the INT signal from the CH375, and the P1.1 output is connected to the CH375's A0 pin to select the data/command mode. T1 is set to timer mode to control the electromagnet's conduction time. The controller's interrupt sources are allocated as follows: INT0: USB port interrupt; INT1: DC photoelectric signal; T0: counter mode, pulse counting for driving the stepper motor; T1: timer mode, timing the electromagnet's conduction time. 4. Controller Software Design During the controller software design, considering the four interrupt sources, some time-consuming complex calculations and judgments are placed in the main loop program. The program is written and debugged using C51, with the INT0 interrupt (CH375) function flow and the T1 and T0 interrupt function flows. 5. Image Processing The image processing software package is used to detect, classify, statistically analyze, and display defects in quartz crystal wafers through digital image processing, and then send the classification results to the controller. This system can process both rectangular and circular quartz crystal wafers; the processing flow differs for these two shapes. Considering that image processing technology is a separate discipline, overly detailed descriptions would make the article lengthy and difficult to understand. This article only uses the example of a shattered core defect in a circular quartz crystal wafer to briefly introduce how the system performs digital image processing and classification. Defects in circular quartz crystal wafers mainly include: core breakage, scratches, shadows, edge chipping, nicks, and fractures. Each defect has a corresponding code. The system generates a sample template by modeling the images of qualified products. During the production process, the image of each quartz crystal wafer is compared with the template. If there is no difference, it is a qualified product. If there is a difference, it is judged one by one according to different judgment criteria until a defect code is generated. This system uses a monochrome camera, and the obtained image is 256 levels of grayscale. There is a certain difference in grayscale levels between the intact and defective parts of the quartz crystal wafer. Different types of defects also have different grayscale levels. Defects have a certain shape, area, and occupy a certain position. These characteristics are the basis for the detection and classification of quartz crystal wafers. The detection process of core breakage defects is as follows: 1) Edge detection. Using the Prewitt edge detection method, the actual target of the circular quartz crystal wafer is edge detected, and then binarized using the corresponding threshold value. The binarized image is compared with the template, and the areas where both are white are extracted. These white areas are marked, and the defect is retained in them. 2) Area discrimination. Tiny blemishes will also appear as white areas after edge detection. To avoid interference with the detection, the marked areas should be checked first, retaining those areas larger than the corresponding threshold value. 3) Location determination. The core defect is not connected to the edge, so location determination is also required. Determine whether the possible defect area is connected to the edge or is completely near the edge. If not, proceed with grayscale determination. Different defects have different grayscale levels. By measuring the grayscale of various defects, the corresponding grayscale value can be found as the threshold value for defect determination. The grayscale of the actual captured image is compared with the grayscale threshold value of the core defect. If it is higher than this value, it can be determined as a core defect. At this point, the core defect detection inside a circular quartz wafer is completed. The detection result of a defect (core defect) inside a circular quartz wafer is shown. In the right image, the white line is the detected shape and location of the core defect. In the corresponding position in the left image, a line with a larger grayscale value can be seen. This line is the image display of the core defect. 6. Conclusion Extensive testing of the quartz crystal wafer quality sorting system has shown that it achieves a 100% defect detection rate, a 0% error detection rate, and a 95% defect classification accuracy rate. The sorting speed is approaching that of manual sorting. Future work will focus on further promoting its application and, based on extensive use, gradually standardizing defect classification and establishing an industry-recognized, quantifiable defect classification standard.