Due to the high integration, complex circuits, and increasing parameters of printed circuit boards (PCBs), defects in PCBs directly affect production efficiency, making computer vision-based defect detection crucial for PCB manufacturing. A self-attention-based PCB defect detection algorithm was proposed based on the YOLO object detection algorithm. First, a polarized self-attention (PSA) mechanism was introduced in the feature extraction stage to separately extract spatial and semantic features of PCBs, which were combined with input raw features to enhance the network’s feature representation capability. Then, a small-object detection head was added in the decoding stage, which fully utilized low-resolution features from the YOLO network Backbone module to enable the network to focus on local details of PCBs and improve defect positioning accuracy. Experiments show that the proposed method achieves 95.5% accuracy on the PCB dataset, 4% higher than the original YOLOv8 method, with the mAP0.5∶0.95 metric increased by 2.8%.