Improving detection accuracy is a challenging task when detecting defects in PCBs. To conquer the issue of degradation of recognition performance of existing intelligent detection algorithms in different environments, we present an improved algorithm, IEMA-YOLOv8, based on YOLOv8. First, we design a novel Efficient Multi-Scale Attention (EMA) combined with the Inverted Residual Mobile Block (IRMB) to form a new attention mechanism called IEMA. The IEMA module is subsequently incorporated into the C2f module to boost the model's overall performance. Secondly, the Spatial Pyramid Pooling Enhanced with ELAN (SPPELAN) module is employed to improve the original Spatial Pyramid Pooling Fast (SPPF) module, thereby bolstering the model's capacity to recognize defective regions. Finally, the More Focused Intersection over Union (Focaler-IoU) loss function replaces the original Complete Intersection over Union (CIoU) loss function, aiming to compensate for the limitations of the current bounding box regression methodology, thus further enhancing the efficacy of detection in the task. The experimental results show that our proposed IEMA-YOLOv8 algorithm has precision, recall, mAP50, and mAP50:95 values of 88.8%, 96.8%, 94.6%, and 51%, respectively, provided that the model complexity is kept basically the same and the Frames Per Second (FPS) value reaches 116.2. These values are 1.1%, 3.6%, 4.9%, and 6% higher than the original YOLOv8n algorithm. Compared with the YOLO family of one-stage detection models, our proposed algorithm has significant advantages in defect detection.